File: test_finder.py

package info (click to toggle)
tmuxp 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,688 kB
  • sloc: python: 8,049; makefile: 202; sh: 14
file content (359 lines) | stat: -rw-r--r-- 11,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""Test config file searching for tmuxp."""

from __future__ import annotations

import argparse
import pathlib
import typing as t

import pytest

from tmuxp import cli
from tmuxp.cli.utils import tmuxp_echo
from tmuxp.workspace.finders import (
    find_workspace_file,
    get_workspace_dir,
    in_cwd,
    in_dir,
    is_pure_name,
)

if t.TYPE_CHECKING:
    import _pytest.capture


def test_in_dir_from_config_dir(tmp_path: pathlib.Path) -> None:
    """config.in_dir() finds configs config dir."""
    cli.startup(tmp_path)
    yaml_config = tmp_path / "myconfig.yaml"
    yaml_config.touch()
    json_config = tmp_path / "myconfig.json"
    json_config.touch()
    configs_found = in_dir(tmp_path)

    assert len(configs_found) == 2


def test_ignore_non_configs_from_current_dir(tmp_path: pathlib.Path) -> None:
    """cli.in_dir() ignore non-config from config dir."""
    cli.startup(tmp_path)

    junk_config = tmp_path / "myconfig.psd"
    junk_config.touch()
    conf = tmp_path / "watmyconfig.json"
    conf.touch()
    configs_found = in_dir(tmp_path)
    assert len(configs_found) == 1


def test_get_configs_cwd(
    tmp_path: pathlib.Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """config.in_cwd() find config in shell current working directory."""
    confdir = tmp_path / "tmuxpconf2"
    confdir.mkdir()

    monkeypatch.chdir(confdir)
    with pathlib.Path(".tmuxp.json").open("w+b") as config1:
        config1.close()

    configs_found = in_cwd()
    assert len(configs_found) == 1
    assert ".tmuxp.json" in configs_found


class PureNameTestFixture(t.NamedTuple):
    """Test fixture for verifying pure name path validation."""

    test_id: str
    path: str
    expect: bool


PURE_NAME_TEST_FIXTURES: list[PureNameTestFixture] = [
    PureNameTestFixture(
        test_id="current_dir",
        path=".",
        expect=False,
    ),
    PureNameTestFixture(
        test_id="current_dir_slash",
        path="./",
        expect=False,
    ),
    PureNameTestFixture(
        test_id="empty_path",
        path="",
        expect=False,
    ),
    PureNameTestFixture(
        test_id="tmuxp_yaml",
        path=".tmuxp.yaml",
        expect=False,
    ),
    PureNameTestFixture(
        test_id="parent_tmuxp_yaml",
        path="../.tmuxp.yaml",
        expect=False,
    ),
    PureNameTestFixture(
        test_id="parent_dir",
        path="../",
        expect=False,
    ),
    PureNameTestFixture(
        test_id="absolute_path",
        path="/hello/world",
        expect=False,
    ),
    PureNameTestFixture(
        test_id="home_tmuxp_path",
        path="~/.tmuxp/hey",
        expect=False,
    ),
    PureNameTestFixture(
        test_id="home_work_path",
        path="~/work/c/tmux/",
        expect=False,
    ),
    PureNameTestFixture(
        test_id="home_work_tmuxp_yaml",
        path="~/work/c/tmux/.tmuxp.yaml",
        expect=False,
    ),
    PureNameTestFixture(
        test_id="pure_name",
        path="myproject",
        expect=True,
    ),
]


@pytest.mark.parametrize(
    list(PureNameTestFixture._fields),
    PURE_NAME_TEST_FIXTURES,
    ids=[test.test_id for test in PURE_NAME_TEST_FIXTURES],
)
def test_is_pure_name(
    test_id: str,
    path: str,
    expect: bool,
) -> None:
    """Test is_pure_name() is truthy when file, not directory or config alias."""
    assert is_pure_name(path) == expect


def test_tmuxp_configdir_env_var(
    tmp_path: pathlib.Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Tests get_workspace_dir() when TMUXP_CONFIGDIR is set."""
    monkeypatch.setenv("TMUXP_CONFIGDIR", str(tmp_path))

    assert get_workspace_dir() == str(tmp_path)


def test_tmuxp_configdir_xdg_config_dir(
    tmp_path: pathlib.Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Test get_workspace_dir() when XDG_CONFIG_HOME is set."""
    monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
    tmux_dir = tmp_path / "tmuxp"
    tmux_dir.mkdir()

    assert get_workspace_dir() == str(tmux_dir)


@pytest.fixture
def homedir(tmp_path: pathlib.Path) -> pathlib.Path:
    """Fixture to ensure and return a home directory."""
    home = tmp_path / "home"
    home.mkdir()
    return home


@pytest.fixture
def configdir(homedir: pathlib.Path) -> pathlib.Path:
    """Fixture to ensure user directory for tmuxp and return it, via homedir fixture."""
    conf = homedir / ".tmuxp"
    conf.mkdir()
    return conf


@pytest.fixture
def projectdir(homedir: pathlib.Path) -> pathlib.Path:
    """Fixture to ensure and return an example project dir."""
    proj = homedir / "work" / "project"
    proj.mkdir(parents=True)
    return proj


def test_resolve_dot(
    tmp_path: pathlib.Path,
    homedir: pathlib.Path,
    configdir: pathlib.Path,
    projectdir: pathlib.Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Test find_workspace_file() resolves dots as relative / current directory."""
    monkeypatch.setenv("HOME", str(homedir))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(homedir / ".config"))

    tmuxp_conf_path = projectdir / ".tmuxp.yaml"
    tmuxp_conf_path.touch()
    user_config_name = "myconfig"
    user_config = configdir / f"{user_config_name}.yaml"
    user_config.touch()

    project_config = tmuxp_conf_path

    monkeypatch.chdir(projectdir)

    expect = str(project_config)
    assert find_workspace_file(".") == expect
    assert find_workspace_file("./") == expect
    assert find_workspace_file("") == expect
    assert find_workspace_file("../project") == expect
    assert find_workspace_file("../project/") == expect
    assert find_workspace_file(".tmuxp.yaml") == expect
    assert find_workspace_file(f"../../.tmuxp/{user_config_name}.yaml") == str(
        user_config,
    )
    assert find_workspace_file("myconfig") == str(user_config)
    assert find_workspace_file("~/.tmuxp/myconfig.yaml") == str(user_config)

    with pytest.raises(FileNotFoundError):
        find_workspace_file(".tmuxp.json")
    with pytest.raises(FileNotFoundError):
        find_workspace_file(".tmuxp.ini")
    with pytest.raises(FileNotFoundError):
        find_workspace_file("../")
    with pytest.raises(FileNotFoundError):
        find_workspace_file("mooooooo")

    monkeypatch.chdir(homedir)

    expect = str(project_config)
    assert find_workspace_file("work/project") == expect
    assert find_workspace_file("work/project/") == expect
    assert find_workspace_file("./work/project") == expect
    assert find_workspace_file("./work/project/") == expect
    assert find_workspace_file(f".tmuxp/{user_config_name}.yaml") == str(user_config)
    assert find_workspace_file(f"./.tmuxp/{user_config_name}.yaml") == str(
        user_config,
    )
    assert find_workspace_file("myconfig") == str(user_config)
    assert find_workspace_file("~/.tmuxp/myconfig.yaml") == str(user_config)

    with pytest.raises(FileNotFoundError):
        find_workspace_file("")
    with pytest.raises(FileNotFoundError):
        find_workspace_file(".")
    with pytest.raises(FileNotFoundError):
        find_workspace_file(".tmuxp.yaml")
    with pytest.raises(FileNotFoundError):
        find_workspace_file("../")
    with pytest.raises(FileNotFoundError):
        find_workspace_file("mooooooo")

    monkeypatch.chdir(configdir)

    expect = str(project_config)
    assert find_workspace_file("../work/project") == expect
    assert find_workspace_file("../../home/work/project") == expect
    assert find_workspace_file("../work/project/") == expect
    assert find_workspace_file(f"{user_config_name}.yaml") == str(user_config)
    assert find_workspace_file(f"./{user_config_name}.yaml") == str(user_config)
    assert find_workspace_file("myconfig") == str(user_config)
    assert find_workspace_file("~/.tmuxp/myconfig.yaml") == str(user_config)

    with pytest.raises(FileNotFoundError):
        find_workspace_file("")
    with pytest.raises(FileNotFoundError):
        find_workspace_file(".")
    with pytest.raises(FileNotFoundError):
        find_workspace_file(".tmuxp.yaml")
    with pytest.raises(FileNotFoundError):
        find_workspace_file("../")
    with pytest.raises(FileNotFoundError):
        find_workspace_file("mooooooo")

    monkeypatch.chdir(tmp_path)

    expect = str(project_config)
    assert find_workspace_file("home/work/project") == expect
    assert find_workspace_file("./home/work/project/") == expect
    assert find_workspace_file(f"home/.tmuxp/{user_config_name}.yaml") == str(
        user_config,
    )
    assert find_workspace_file(f"./home/.tmuxp/{user_config_name}.yaml") == str(
        user_config,
    )
    assert find_workspace_file("myconfig") == str(user_config)
    assert find_workspace_file("~/.tmuxp/myconfig.yaml") == str(user_config)

    with pytest.raises(FileNotFoundError):
        find_workspace_file("")
    with pytest.raises(FileNotFoundError):
        find_workspace_file(".")
    with pytest.raises(FileNotFoundError):
        find_workspace_file(".tmuxp.yaml")
    with pytest.raises(FileNotFoundError):
        find_workspace_file("../")
    with pytest.raises(FileNotFoundError):
        find_workspace_file("mooooooo")


def test_find_workspace_file_arg(
    homedir: pathlib.Path,
    configdir: pathlib.Path,
    projectdir: pathlib.Path,
    monkeypatch: pytest.MonkeyPatch,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Test find_workspace_file() via file path."""
    parser = argparse.ArgumentParser()
    parser.add_argument("workspace_file", type=str)

    def config_cmd(workspace_file: str) -> None:
        tmuxp_echo(find_workspace_file(workspace_file, workspace_dir=configdir))

    monkeypatch.setenv("HOME", str(homedir))
    tmuxp_config_path = projectdir / ".tmuxp.yaml"
    tmuxp_config_path.touch()
    user_config_name = "myconfig"
    user_config = configdir / f"{user_config_name}.yaml"
    user_config.touch()

    project_config = projectdir / ".tmuxp.yaml"

    def check_cmd(config_arg: str) -> _pytest.capture.CaptureResult[str]:
        args = parser.parse_args([config_arg])
        config_cmd(workspace_file=args.workspace_file)
        return capsys.readouterr()

    monkeypatch.chdir(projectdir)
    expect = str(project_config)
    assert expect in check_cmd(".").out
    assert expect in check_cmd("./").out
    assert expect in check_cmd("").out
    assert expect in check_cmd("../project").out
    assert expect in check_cmd("../project/").out
    assert expect in check_cmd(".tmuxp.yaml").out
    assert str(user_config) in check_cmd(f"../../.tmuxp/{user_config_name}.yaml").out
    assert user_config.stem in check_cmd("myconfig").out
    assert str(user_config) in check_cmd("~/.tmuxp/myconfig.yaml").out

    with pytest.raises(FileNotFoundError, match="file not found"):
        assert "file not found" in check_cmd(".tmuxp.json").err
    with pytest.raises(FileNotFoundError, match="file not found"):
        assert "file not found" in check_cmd(".tmuxp.ini").err
    with pytest.raises(FileNotFoundError, match="No tmuxp files found"):
        assert "No tmuxp files found" in check_cmd("../").err
    with pytest.raises(
        FileNotFoundError,
        match="workspace-file not found in workspace dir",
    ):
        assert "workspace-file not found in workspace dir" in check_cmd("moo").err