1
1
"""Test for tmuxp configuration import, inlining, expanding and export."""
2
2
import os
3
+ import pathlib
4
+ import typing
5
+ from typing import Union
3
6
4
7
import pytest
5
8
6
9
import kaptan
7
10
8
11
from tmuxp import config , exc
9
12
10
- from . import example_dir
11
- from .fixtures import config as fixtures
13
+ from . import EXAMPLE_PATH
12
14
13
- TMUXP_DIR = os .path .join (os .path .dirname (__file__ ), ".tmuxp" )
15
+ if typing .TYPE_CHECKING :
16
+ from .fixtures import config as ConfigFixture
14
17
15
18
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
18
29
19
30
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
+
22
38
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
+ )
23
45
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"
26
49
27
50
configparser = kaptan .Kaptan ()
28
- configparser .import_config (fixtures .sampleconfig .sampleconfigdict )
51
+ configparser .import_config (config_fixture .sampleconfig .sampleconfigdict )
29
52
30
53
json_config_data = configparser .export ("json" , indent = 2 )
31
54
32
- json_config_file .write (json_config_data )
55
+ json_config_file .write_text (json_config_data , encoding = "utf-8" )
33
56
34
57
new_config = kaptan .Kaptan ()
35
58
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
37
60
38
61
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"
41
64
42
65
configparser = kaptan .Kaptan ()
43
- sampleconfig = config .inline (fixtures .sampleconfig .sampleconfigdict )
66
+ sampleconfig = config .inline (config_fixture .sampleconfig .sampleconfigdict )
44
67
configparser .import_config (sampleconfig )
45
68
46
69
yaml_config_data = configparser .export ("yaml" , indent = 2 , default_flow_style = False )
47
70
48
- yaml_config_file .write (yaml_config_data )
71
+ yaml_config_file .write_text (yaml_config_data , encoding = "utf-8" )
49
72
50
73
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
52
75
53
76
54
- def test_scan_config (tmpdir ):
77
+ def test_scan_config (tmp_path : pathlib . Path ):
55
78
configs = []
56
79
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 " )
59
82
60
- for r , d , f in os .walk (str (tmpdir )):
83
+ for r , d , f in os .walk (str (tmp_path )):
61
84
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 ))
63
86
64
87
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 ():
66
92
files += 1
67
- assert str (tmpdir . join ( "config.json" ) ) in configs
93
+ assert str (config_json ) in configs
68
94
69
- if tmpdir . join ( "config.yaml" ). check ():
95
+ if config_yaml . exists ():
70
96
files += 1
71
- assert str (tmpdir . join ( "config.yaml" ) ) in configs
97
+ assert str (config_yaml ) in configs
72
98
73
- if tmpdir . join ( "config.ini" ). check ():
99
+ if config_ini . exists ():
74
100
files += 1
75
- assert str (tmpdir . join ( "config.ini" ) ) in configs
101
+ assert str (config_ini ) in configs
76
102
77
103
assert len (configs ) == files
78
104
79
105
80
- def test_config_expand1 ():
106
+ def test_config_expand1 (config_fixture : "ConfigFixture" ):
81
107
"""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
84
110
85
111
86
- def test_config_expand2 ():
112
+ def test_config_expand2 (config_fixture : "ConfigFixture" ):
87
113
"""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 )
93
116
assert config .expand (unexpanded_dict ) == expanded_dict
94
117
95
118
@@ -205,31 +228,31 @@ def test_inheritance_config():
205
228
assert config == inheritance_config_after
206
229
207
230
208
- def test_shell_command_before ():
231
+ def test_shell_command_before (config_fixture : "ConfigFixture" ):
209
232
"""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
211
234
test_config = config .expand (test_config )
212
235
213
- assert test_config == fixtures .shell_command_before .config_expanded
236
+ assert test_config == config_fixture .shell_command_before .config_expanded
214
237
215
238
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
217
240
218
241
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 )
221
244
222
245
config .validate_schema (sconfig )
223
246
224
247
assert config .expand (sconfig ) == sconfig
225
248
assert config .expand (config .trickle (sconfig )) == load_yaml (
226
- fixtures .shell_command_before_session .expected
249
+ config_fixture .shell_command_before_session .expected
227
250
)
228
251
229
252
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
233
256
234
257
235
258
def test_trickle_window_with_no_pane_config ():
@@ -250,7 +273,7 @@ def test_trickle_window_with_no_pane_config():
250
273
}
251
274
252
275
253
- def test_expands_blank_panes ():
276
+ def test_expands_blank_panes (config_fixture : "ConfigFixture" ):
254
277
"""Expand blank config into full form.
255
278
256
279
Handle ``NoneType`` and 'blank'::
@@ -277,10 +300,9 @@ def test_expands_blank_panes():
277
300
'shell_command': ['']
278
301
279
302
"""
280
-
281
- yaml_config_file = os .path .join (example_dir , "blank-panes.yaml" )
303
+ yaml_config_file = EXAMPLE_PATH / "blank-panes.yaml"
282
304
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
284
306
285
307
286
308
def test_no_session_name ():
0 commit comments