11"""Test for tmuxp configuration import, inlining, expanding and export."""
22import os
3+ import pathlib
4+ import typing
5+ from typing import Union
36
47import pytest
58
69import kaptan
710
811from 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
235258def 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
286308def test_no_session_name ():
0 commit comments