Skip to content

Commit 9936ede

Browse files
authored
refactor(tests): Streamline fixtures, use pathlib
2 parents 9b5a8c9 + 733d0ed commit 9936ede

11 files changed

+123
-127
lines changed

CHANGES

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
```yaml
1212
session_name: Should not execute
1313
windows:
14-
- panes:
15-
- shell_command: echo "___$((1 + 3))___"
16-
enter: false
14+
- panes:
15+
- shell_command: echo "___$((1 + 3))___"
16+
enter: false
1717
```
1818
1919
- #701: `tmuxp freeze` now accepts `--quiet` and `--yes` along with the

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
PY_FILES= find . -type f -not -path '*/\.*' | grep -i '.*[.]py$$' 2> /dev/null
2+
TEST_FILES= find . -type f -not -path '*/\.*' | grep -i '.*[.]\(yaml\|py\)$$' 2> /dev/null
23
DOC_FILES= find . -type f -not -path '*/\.*' | grep -i '.*[.]rst\$\|.*[.]md\$\|.*[.]css\$\|.*[.]py\$\|mkdocs\.yml\|CHANGES\|TODO\|.*conf\.py' 2> /dev/null
34
SHELL := /bin/bash
45

@@ -23,7 +24,7 @@ start:
2324
$(MAKE) test; poetry run ptw .
2425

2526
watch_test:
26-
if command -v entr > /dev/null; then ${PY_FILES} | entr -c $(MAKE) test; else $(MAKE) test entr_warn; fi
27+
if command -v entr > /dev/null; then ${TEST_FILES} | entr -c $(MAKE) test; else $(MAKE) test entr_warn; fi
2728

2829
build_docs:
2930
$(MAKE) -C docs html

docs/cli.md

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ $ tmuxp load /path/to/folder/
188188
```
189189

190190
Name of the config, assume `$HOME/.tmuxp/myconfig.yaml`:
191+
191192
```console
192193
$ tmuxp load myconfig
193194
```
@@ -236,11 +237,13 @@ All of these options can be preselected to skip the prompt:
236237
```console
237238
$ tmuxp load -y config
238239
```
240+
239241
- Detached / open in background:
240242

241243
```console
242244
$ tmuxp load -d config
243245
```
246+
244247
- Append windows to existing session
245248

246249
```console

setup.cfg

+2-36
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,15 @@ exclude = .*/,.tox,*.egg,tmuxp/_compat.py,tmuxp/__*__.py,
33
select = E,W,F,N
44
max-line-length = 88
55
# Stuff we ignore thanks to black: https://github.com/ambv/black/issues/429
6-
ignore = E111,
7-
E121,
8-
E122,
9-
E123,
10-
E124,
11-
E125,
12-
E126,
13-
E201,
14-
E202,
15-
E203,
16-
E221,
17-
E222,
18-
E225,
19-
E226,
20-
E227,
21-
E231,
22-
E241,
23-
E251,
24-
E261,
25-
E262,
26-
E265,
27-
E271,
28-
E272,
29-
E302,
30-
E303,
31-
E306,
32-
E502,
33-
E701,
34-
E702,
35-
E703,
36-
E704,
37-
W291,
38-
W292,
39-
W293,
40-
W391,
41-
W503
6+
extend-ignore = E203
427

438
[tool:pytest]
449
addopts = --reruns=0
4510
filterwarnings =
4611
ignore:distutils Version classes are deprecated. Use packaging.version instead.
4712

4813
[isort]
14+
profile = black
4915
combine_as_imports= true
5016
default_section = THIRDPARTY
5117
include_trailing_comma = true

tests/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
~~~~~~~~~~~
55
66
"""
7-
import os
7+
import pathlib
88

9-
current_dir = os.path.abspath(os.path.dirname(__file__))
10-
example_dir = os.path.abspath(os.path.join(current_dir, "..", "examples"))
11-
fixtures_dir = os.path.realpath(os.path.join(current_dir, "fixtures"))
9+
TESTS_PATH = pathlib.Path(__file__).parent
10+
EXAMPLE_PATH = TESTS_PATH.parent / "examples"
11+
FIXTURES_PATH = TESTS_PATH / "fixtures"

tests/conftest.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import getpass
12
import logging
23
import os
4+
import pathlib
35

46
import pytest
57

@@ -10,6 +12,23 @@
1012
logger = logging.getLogger(__name__)
1113

1214

15+
@pytest.fixture(autouse=True, scope="session")
16+
def home_path(tmp_path_factory: pytest.TempPathFactory):
17+
return tmp_path_factory.mktemp("home")
18+
19+
20+
@pytest.fixture(autouse=True, scope="session")
21+
def user_path(home_path: pathlib.Path):
22+
p = home_path / getpass.getuser()
23+
p.mkdir()
24+
return p
25+
26+
27+
@pytest.fixture(autouse=True)
28+
def home_path_default(user_path: pathlib.Path):
29+
os.environ["HOME"] = str(user_path)
30+
31+
1332
@pytest.fixture(scope="function")
1433
def monkeypatch_plugin_test_packages(monkeypatch):
1534
paths = [

tests/test_config.py

+73-51
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,118 @@
11
"""Test for tmuxp configuration import, inlining, expanding and export."""
22
import os
3+
import pathlib
4+
import typing
5+
from typing import Union
36

47
import pytest
58

69
import kaptan
710

811
from tmuxp import config, exc
912

10-
from . import example_dir
11-
from .fixtures import config as fixtures
13+
from . import EXAMPLE_PATH
1214

13-
TMUXP_DIR = os.path.join(os.path.dirname(__file__), ".tmuxp")
15+
if typing.TYPE_CHECKING:
16+
from .fixtures import config as ConfigFixture
1417

1518

16-
def load_yaml(yaml):
17-
return kaptan.Kaptan(handler="yaml").import_config(yaml).get()
19+
@pytest.fixture
20+
def config_fixture():
21+
"""Deferred import of tmuxp.tests.fixtures.*
22+
23+
pytest setup (conftest.py) patches os.environ["HOME"], delay execution of
24+
os.path.expanduser until here.
25+
"""
26+
from .fixtures import config as config_fixture
27+
28+
return config_fixture
1829

1930

20-
def load_config(_file):
21-
return kaptan.Kaptan().import_config(_file).get()
31+
def load_yaml(path: Union[str, pathlib.Path]) -> str:
32+
return (
33+
kaptan.Kaptan(handler="yaml")
34+
.import_config(str(path) if isinstance(path, pathlib.Path) else path)
35+
.get()
36+
)
37+
2238

39+
def load_config(path: Union[str, pathlib.Path]) -> str:
40+
return (
41+
kaptan.Kaptan()
42+
.import_config(str(path) if isinstance(path, pathlib.Path) else path)
43+
.get()
44+
)
2345

24-
def test_export_json(tmpdir):
25-
json_config_file = tmpdir.join("config.json")
46+
47+
def test_export_json(tmp_path: pathlib.Path, config_fixture: "ConfigFixture"):
48+
json_config_file = tmp_path / "config.json"
2649

2750
configparser = kaptan.Kaptan()
28-
configparser.import_config(fixtures.sampleconfig.sampleconfigdict)
51+
configparser.import_config(config_fixture.sampleconfig.sampleconfigdict)
2952

3053
json_config_data = configparser.export("json", indent=2)
3154

32-
json_config_file.write(json_config_data)
55+
json_config_file.write_text(json_config_data, encoding="utf-8")
3356

3457
new_config = kaptan.Kaptan()
3558
new_config_data = new_config.import_config(str(json_config_file)).get()
36-
assert fixtures.sampleconfig.sampleconfigdict == new_config_data
59+
assert config_fixture.sampleconfig.sampleconfigdict == new_config_data
3760

3861

39-
def test_export_yaml(tmpdir):
40-
yaml_config_file = tmpdir.join("config.yaml")
62+
def test_export_yaml(tmp_path: pathlib.Path, config_fixture: "ConfigFixture"):
63+
yaml_config_file = tmp_path / "config.yaml"
4164

4265
configparser = kaptan.Kaptan()
43-
sampleconfig = config.inline(fixtures.sampleconfig.sampleconfigdict)
66+
sampleconfig = config.inline(config_fixture.sampleconfig.sampleconfigdict)
4467
configparser.import_config(sampleconfig)
4568

4669
yaml_config_data = configparser.export("yaml", indent=2, default_flow_style=False)
4770

48-
yaml_config_file.write(yaml_config_data)
71+
yaml_config_file.write_text(yaml_config_data, encoding="utf-8")
4972

5073
new_config_data = load_config(str(yaml_config_file))
51-
assert fixtures.sampleconfig.sampleconfigdict == new_config_data
74+
assert config_fixture.sampleconfig.sampleconfigdict == new_config_data
5275

5376

54-
def test_scan_config(tmpdir):
77+
def test_scan_config(tmp_path: pathlib.Path):
5578
configs = []
5679

57-
garbage_file = tmpdir.join("config.psd")
58-
garbage_file.write("wat")
80+
garbage_file = tmp_path / "config.psd"
81+
garbage_file.write_text("wat", encoding="utf-8")
5982

60-
for r, d, f in os.walk(str(tmpdir)):
83+
for r, d, f in os.walk(str(tmp_path)):
6184
for filela in (x for x in f if x.endswith((".json", ".ini", "yaml"))):
62-
configs.append(str(tmpdir.join(filela)))
85+
configs.append(str(tmp_path / filela))
6386

6487
files = 0
65-
if tmpdir.join("config.json").check():
88+
config_json = tmp_path / "config.json"
89+
config_yaml = tmp_path / "config.yaml"
90+
config_ini = tmp_path / "config.ini"
91+
if config_json.exists():
6692
files += 1
67-
assert str(tmpdir.join("config.json")) in configs
93+
assert str(config_json) in configs
6894

69-
if tmpdir.join("config.yaml").check():
95+
if config_yaml.exists():
7096
files += 1
71-
assert str(tmpdir.join("config.yaml")) in configs
97+
assert str(config_yaml) in configs
7298

73-
if tmpdir.join("config.ini").check():
99+
if config_ini.exists():
74100
files += 1
75-
assert str(tmpdir.join("config.ini")) in configs
101+
assert str(config_ini) in configs
76102

77103
assert len(configs) == files
78104

79105

80-
def test_config_expand1():
106+
def test_config_expand1(config_fixture: "ConfigFixture"):
81107
"""Expand shell commands from string to list."""
82-
test_config = config.expand(fixtures.expand1.before_config)
83-
assert test_config == fixtures.expand1.after_config
108+
test_config = config.expand(config_fixture.expand1.before_config)
109+
assert test_config == config_fixture.expand1.after_config
84110

85111

86-
def test_config_expand2():
112+
def test_config_expand2(config_fixture: "ConfigFixture"):
87113
"""Expand shell commands from string to list."""
88-
89-
unexpanded_dict = load_yaml(fixtures.expand2.unexpanded_yaml)
90-
91-
expanded_dict = load_yaml(fixtures.expand2.expanded_yaml)
92-
114+
unexpanded_dict = load_yaml(config_fixture.expand2.unexpanded_yaml)
115+
expanded_dict = load_yaml(config_fixture.expand2.expanded_yaml)
93116
assert config.expand(unexpanded_dict) == expanded_dict
94117

95118

@@ -205,31 +228,31 @@ def test_inheritance_config():
205228
assert config == inheritance_config_after
206229

207230

208-
def test_shell_command_before():
231+
def test_shell_command_before(config_fixture: "ConfigFixture"):
209232
"""Config inheritance for the nested 'start_command'."""
210-
test_config = fixtures.shell_command_before.config_unexpanded
233+
test_config = config_fixture.shell_command_before.config_unexpanded
211234
test_config = config.expand(test_config)
212235

213-
assert test_config == fixtures.shell_command_before.config_expanded
236+
assert test_config == config_fixture.shell_command_before.config_expanded
214237

215238
test_config = config.trickle(test_config)
216-
assert test_config == fixtures.shell_command_before.config_after
239+
assert test_config == config_fixture.shell_command_before.config_after
217240

218241

219-
def test_in_session_scope():
220-
sconfig = load_yaml(fixtures.shell_command_before_session.before)
242+
def test_in_session_scope(config_fixture: "ConfigFixture"):
243+
sconfig = load_yaml(config_fixture.shell_command_before_session.before)
221244

222245
config.validate_schema(sconfig)
223246

224247
assert config.expand(sconfig) == sconfig
225248
assert config.expand(config.trickle(sconfig)) == load_yaml(
226-
fixtures.shell_command_before_session.expected
249+
config_fixture.shell_command_before_session.expected
227250
)
228251

229252

230-
def test_trickle_relative_start_directory():
231-
test_config = config.trickle(fixtures.trickle.before)
232-
assert test_config == fixtures.trickle.expected
253+
def test_trickle_relative_start_directory(config_fixture: "ConfigFixture"):
254+
test_config = config.trickle(config_fixture.trickle.before)
255+
assert test_config == config_fixture.trickle.expected
233256

234257

235258
def test_trickle_window_with_no_pane_config():
@@ -250,7 +273,7 @@ def test_trickle_window_with_no_pane_config():
250273
}
251274

252275

253-
def test_expands_blank_panes():
276+
def test_expands_blank_panes(config_fixture: "ConfigFixture"):
254277
"""Expand blank config into full form.
255278
256279
Handle ``NoneType`` and 'blank'::
@@ -277,10 +300,9 @@ def test_expands_blank_panes():
277300
'shell_command': ['']
278301
279302
"""
280-
281-
yaml_config_file = os.path.join(example_dir, "blank-panes.yaml")
303+
yaml_config_file = EXAMPLE_PATH / "blank-panes.yaml"
282304
test_config = load_config(yaml_config_file)
283-
assert config.expand(test_config) == fixtures.expand_blank.expected
305+
assert config.expand(test_config) == config_fixture.expand_blank.expected
284306

285307

286308
def test_no_session_name():

tests/test_config_teamocil.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
"""Test for tmuxp teamocil configuration."""
2-
import os
3-
42
import pytest
53

64
import kaptan
@@ -9,8 +7,6 @@
97

108
from .fixtures import config_teamocil as fixtures
119

12-
TMUXP_DIR = os.path.join(os.path.dirname(__file__), ".tmuxp")
13-
1410

1511
@pytest.mark.parametrize(
1612
"teamocil_yaml,teamocil_dict,tmuxp_dict",

tests/test_config_tmuxinator.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
"""Test for tmuxp tmuxinator configuration."""
2-
import os
3-
42
import pytest
53

64
import kaptan
@@ -9,8 +7,6 @@
97

108
from .fixtures import config_tmuxinator as fixtures
119

12-
TMUXP_DIR = os.path.join(os.path.dirname(__file__), ".tmuxp")
13-
1410

1511
@pytest.mark.parametrize(
1612
"tmuxinator_yaml,tmuxinator_dict,tmuxp_dict",

0 commit comments

Comments
 (0)