Skip to content

Commit 6917872

Browse files
committed
refactor(tests): Move test dir constants to pathlib.Path
1 parent c251c1c commit 6917872

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

tests/__init__.py

Lines changed: 4 additions & 4 deletions
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_dir = pathlib.Path(__file__).parent
10+
example_dir = tests_dir.parent / "examples"
11+
fixtures_dir = tests_dir / "fixtures"

tests/test_config.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Test for tmuxp configuration import, inlining, expanding and export."""
22
import os
33
import pathlib
4+
from typing import Union
45

56
import pytest
67

@@ -14,12 +15,20 @@
1415
TMUXP_DIR = pathlib.Path(__file__).parent / ".tmuxp"
1516

1617

17-
def load_yaml(yaml):
18-
return kaptan.Kaptan(handler="yaml").import_config(yaml).get()
18+
def load_yaml(path: Union[str, pathlib.Path]) -> str:
19+
return (
20+
kaptan.Kaptan(handler="yaml")
21+
.import_config(str(path) if isinstance(path, pathlib.Path) else path)
22+
.get()
23+
)
1924

2025

21-
def load_config(_file):
22-
return kaptan.Kaptan().import_config(_file).get()
26+
def load_config(path: Union[str, pathlib.Path]) -> str:
27+
return (
28+
kaptan.Kaptan()
29+
.import_config(str(path) if isinstance(path, pathlib.Path) else path)
30+
.get()
31+
)
2332

2433

2534
def test_export_json(tmp_path: pathlib.Path):
@@ -282,7 +291,7 @@ def test_expands_blank_panes():
282291
283292
"""
284293

285-
yaml_config_file = os.path.join(example_dir, "blank-panes.yaml")
294+
yaml_config_file = example_dir / "blank-panes.yaml"
286295
test_config = load_config(yaml_config_file)
287296
assert config.expand(test_config) == fixtures.expand_blank.expected
288297

tests/test_util.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
"""Tests for utility functions in tmux."""
2-
import os
3-
42
import pytest
53

64
from tmuxp import exc
@@ -11,7 +9,7 @@
119

1210

1311
def test_raise_BeforeLoadScriptNotExists_if_not_exists():
14-
script_file = os.path.join(fixtures_dir, "script_noexists.sh")
12+
script_file = fixtures_dir / "script_noexists.sh"
1513

1614
with pytest.raises(BeforeLoadScriptNotExists):
1715
run_before_script(script_file)
@@ -21,30 +19,30 @@ def test_raise_BeforeLoadScriptNotExists_if_not_exists():
2119

2220

2321
def test_raise_BeforeLoadScriptError_if_retcode():
24-
script_file = os.path.join(fixtures_dir, "script_failed.sh")
22+
script_file = fixtures_dir / "script_failed.sh"
2523

2624
with pytest.raises(BeforeLoadScriptError):
2725
run_before_script(script_file)
2826

2927

3028
def test_return_stdout_if_ok(capsys):
31-
script_file = os.path.join(fixtures_dir, "script_complete.sh")
29+
script_file = fixtures_dir / "script_complete.sh"
3230

3331
run_before_script(script_file)
3432
out, err = capsys.readouterr()
3533
assert "hello" in out
3634

3735

3836
def test_beforeload_returncode():
39-
script_file = os.path.join(fixtures_dir, "script_failed.sh")
37+
script_file = fixtures_dir / "script_failed.sh"
4038

4139
with pytest.raises(exc.BeforeLoadScriptError) as excinfo:
4240
run_before_script(script_file)
4341
assert excinfo.match(r"113")
4442

4543

4644
def test_beforeload_returns_stderr_messages():
47-
script_file = os.path.join(fixtures_dir, "script_failed.sh")
45+
script_file = fixtures_dir / "script_failed.sh"
4846

4947
with pytest.raises(exc.BeforeLoadScriptError) as excinfo:
5048
run_before_script(script_file)

tests/test_workspacebuilder.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ def test_automatic_rename_option(session):
405405

406406
def test_blank_pane_count(session):
407407
""":todo: Verify blank panes of various types build into workspaces."""
408-
yaml_config_file = os.path.join(example_dir, "blank-panes.yaml")
409-
test_config = kaptan.Kaptan().import_config(yaml_config_file).get()
408+
yaml_config_file = example_dir / "blank-panes.yaml"
409+
test_config = kaptan.Kaptan().import_config(str(yaml_config_file)).get()
410410
test_config = config.expand(test_config)
411411
builder = WorkspaceBuilder(sconf=test_config)
412412
builder.build(session=session)
@@ -590,7 +590,7 @@ def test_before_load_throw_error_if_retcode_error(server):
590590
sconfig = kaptan.Kaptan(handler="yaml")
591591
yaml = config_script_fails.format(
592592
fixtures_dir=fixtures_dir,
593-
script_failed=os.path.join(fixtures_dir, "script_failed.sh"),
593+
script_failed=fixtures_dir / "script_failed.sh",
594594
)
595595

596596
sconfig = sconfig.import_config(yaml).get()
@@ -616,7 +616,7 @@ def test_before_load_throw_error_if_file_not_exists(server):
616616
sconfig = kaptan.Kaptan(handler="yaml")
617617
yaml = config_script_not_exists.format(
618618
fixtures_dir=fixtures_dir,
619-
script_not_exists=os.path.join(fixtures_dir, "script_not_exists.sh"),
619+
script_not_exists=fixtures_dir / "script_not_exists.sh",
620620
)
621621
sconfig = sconfig.import_config(yaml).get()
622622
sconfig = config.expand(sconfig)
@@ -639,11 +639,12 @@ def test_before_load_true_if_test_passes(server):
639639
config_script_completes = load_fixture(
640640
"workspacebuilder/config_script_completes.yaml"
641641
)
642-
assert os.path.exists(os.path.join(fixtures_dir, "script_complete.sh"))
642+
script_complete_sh = fixtures_dir / "script_complete.sh"
643+
assert script_complete_sh.exists()
643644
sconfig = kaptan.Kaptan(handler="yaml")
644645
yaml = config_script_completes.format(
645646
fixtures_dir=fixtures_dir,
646-
script_complete=os.path.join(fixtures_dir, "script_complete.sh"),
647+
script_complete=script_complete_sh,
647648
)
648649

649650
sconfig = sconfig.import_config(yaml).get()
@@ -660,12 +661,11 @@ def test_before_load_true_if_test_passes_with_args(server):
660661
config_script_completes = load_fixture(
661662
"workspacebuilder/config_script_completes.yaml"
662663
)
663-
664-
assert os.path.exists(os.path.join(fixtures_dir, "script_complete.sh"))
664+
script_complete_sh = fixtures_dir / "script_complete.sh"
665+
assert script_complete_sh.exists()
665666
sconfig = kaptan.Kaptan(handler="yaml")
666667
yaml = config_script_completes.format(
667-
fixtures_dir=fixtures_dir,
668-
script_complete=os.path.join(fixtures_dir, "script_complete.sh") + " -v",
668+
fixtures_dir=fixtures_dir, script_complete=script_complete_sh
669669
)
670670

671671
sconfig = sconfig.import_config(yaml).get()

0 commit comments

Comments
 (0)