-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_config_file.py
More file actions
396 lines (319 loc) · 12.6 KB
/
test_config_file.py
File metadata and controls
396 lines (319 loc) · 12.6 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""Tests for vcspull config loading."""
import os
import pathlib
import textwrap
import pytest
from vcspull import config, exc
from vcspull._internal.config_reader import ConfigReader
from vcspull.config import expand_dir, extract_repos
from vcspull.validator import is_valid_config
from .fixtures import example as fixtures
from .helpers import EnvironmentVarGuard, load_raw, write_config
@pytest.fixture()
def yaml_config(config_path: pathlib.Path) -> pathlib.Path:
yaml_file = config_path / "repos1.yaml"
yaml_file.touch()
return yaml_file
@pytest.fixture()
def json_config(config_path: pathlib.Path) -> pathlib.Path:
json_file = config_path / "repos2.json"
json_file.touch()
return json_file
def test_dict_equals_yaml() -> None:
# Verify that example YAML is returning expected dict format.
config = ConfigReader._load(
format="yaml",
content="""\
/home/me/myproject/study/:
linux: git+git://git.kernel.org/linux/torvalds/linux.git
freebsd: git+https://github.com/freebsd/freebsd.git
sphinx: hg+https://bitbucket.org/birkenfeld/sphinx
docutils: svn+http://svn.code.sf.net/p/docutils/code/trunk
/home/me/myproject/github_projects/:
kaptan:
url: git+git@github.com:tony/kaptan.git
remotes:
upstream: git+https://github.com/emre/kaptan
ms: git+https://github.com/ms/kaptan.git
/home/me/myproject:
.vim:
url: git+git@github.com:tony/vim-config.git
shell_command_after: ln -sf /home/me/.vim/.vimrc /home/me/.vimrc
.tmux:
url: git+git@github.com:tony/tmux-config.git
shell_command_after:
- ln -sf /home/me/.tmux/.tmux.conf /home/me/.tmux.conf
""",
)
assert fixtures.config_dict == config
def test_export_json(tmp_path: pathlib.Path) -> None:
json_config = tmp_path / ".vcspull.json"
config = ConfigReader(content=fixtures.config_dict)
json_config_data = config.dump("json", indent=2)
json_config.write_text(json_config_data, encoding="utf-8")
new_config = ConfigReader._from_file(json_config)
assert fixtures.config_dict == new_config
def test_export_yaml(tmp_path: pathlib.Path) -> None:
yaml_config = tmp_path / ".vcspull.yaml"
config = ConfigReader(content=fixtures.config_dict)
yaml_config_data = config.dump("yaml", indent=2)
yaml_config.write_text(yaml_config_data, encoding="utf-8")
new_config = ConfigReader._from_file(yaml_config)
assert fixtures.config_dict == new_config
def test_scan_config(tmp_path: pathlib.Path) -> None:
config_files: list[str] = []
exists = os.path.exists
garbage_file = tmp_path / ".vcspull.psd"
garbage_file.write_text("wat", encoding="utf-8")
for _r, _d, file in os.walk(str(tmp_path)):
config_files += [
str(tmp_path / scanned_file)
for scanned_file in file
if scanned_file.endswith((".json", "yaml"))
and scanned_file.startswith(".vcspull")
]
files = 0
if exists(str(tmp_path / ".vcspull.json")):
files += 1
assert str(tmp_path / ".vcspull.json") in config_files
if exists(str(tmp_path / ".vcspull.yaml")):
files += 1
assert str(tmp_path / ".vcspull.json") in config_files
assert len(config_files) == files
def test_expand_shell_command_after() -> None:
# Expand shell commands from string to list.
assert is_valid_config(fixtures.config_dict)
config = extract_repos(fixtures.config_dict)
assert config, fixtures.config_dict_expanded
def test_expandenv_and_homevars() -> None:
# Assure ~ tildes and environment template vars expand.
config1 = load_raw(
"""\
'~/study/':
sphinx: hg+file://{hg_repo_path}
docutils: svn+file://{svn_repo_path}
linux: git+file://{git_repo_path}
'${HOME}/github_projects/':
kaptan:
url: git+file://{git_repo_path}
remotes:
test_remote: git+file://{git_repo_path}
'~':
.vim:
url: git+file://{git_repo_path}
.tmux:
url: git+file://{git_repo_path}
""",
format="yaml",
)
config2 = load_raw(
"""\
{
"~/study/": {
"sphinx": "hg+file://${hg_repo_path}",
"docutils": "svn+file://${svn_repo_path}",
"linux": "git+file://${git_repo_path}"
},
"${HOME}/github_projects/": {
"kaptan": {
"url": "git+file://${git_repo_path}",
"remotes": {
"test_remote": "git+file://${git_repo_path}"
}
}
}
}
""",
format="json",
)
assert is_valid_config(config1)
assert is_valid_config(config2)
config1_expanded = extract_repos(config1)
config2_expanded = extract_repos(config2)
paths = [r["dir"].parent for r in config1_expanded]
assert expand_dir(pathlib.Path("${HOME}/github_projects/")) in paths
assert expand_dir(pathlib.Path("~/study/")) in paths
assert expand_dir(pathlib.Path("~")) in paths
paths = [r["dir"].parent for r in config2_expanded]
assert expand_dir(pathlib.Path("${HOME}/github_projects/")) in paths
assert expand_dir(pathlib.Path("~/study/")) in paths
def test_find_config_files(tmp_path: pathlib.Path) -> None:
# Test find_config_files in home directory.
pull_config = tmp_path / ".vcspull.yaml"
pull_config.touch()
with EnvironmentVarGuard() as env:
env.set("HOME", str(tmp_path))
assert pathlib.Path.home() == tmp_path
expected_in = tmp_path / ".vcspull.yaml"
results = config.find_home_config_files()
assert expected_in in results
def test_multiple_config_files_raises_exception(tmp_path: pathlib.Path) -> None:
json_conf_file = tmp_path / ".vcspull.json"
json_conf_file.touch()
yaml_conf_file = tmp_path / ".vcspull.yaml"
yaml_conf_file.touch()
with EnvironmentVarGuard() as env:
env.set("HOME", str(tmp_path))
with pytest.raises(exc.MultipleConfigWarning):
config.find_home_config_files()
def test_in_dir(
config_path: pathlib.Path,
yaml_config: pathlib.Path,
json_config: pathlib.Path,
) -> None:
expected = [yaml_config.stem, json_config.stem]
result = config.in_dir(config_path)
assert len(expected) == len(result)
def test_find_config_path_string(
config_path: pathlib.Path, yaml_config: pathlib.Path, json_config: pathlib.Path
) -> None:
config_files = config.find_config_files(path=config_path)
assert yaml_config in config_files
assert json_config in config_files
def test_find_config_path_list(
config_path: pathlib.Path,
yaml_config: pathlib.Path,
json_config: pathlib.Path,
) -> None:
config_files = config.find_config_files(path=[config_path])
assert yaml_config in config_files
assert json_config in config_files
def test_find_config_match_string(
config_path: pathlib.Path,
yaml_config: pathlib.Path,
json_config: pathlib.Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_files = config.find_config_files(path=config_path, match=yaml_config.stem)
assert yaml_config in config_files
assert json_config not in config_files
config_files = config.find_config_files(path=[config_path], match=json_config.stem)
assert yaml_config not in config_files
assert json_config in config_files
config_files = config.find_config_files(path=[config_path], match="randomstring")
assert yaml_config not in config_files
assert json_config not in config_files
config_files = config.find_config_files(path=[config_path], match="*")
assert yaml_config in config_files
assert json_config in config_files
config_files = config.find_config_files(path=[config_path], match="repos*")
assert yaml_config in config_files
assert json_config in config_files
config_files = config.find_config_files(path=[config_path], match="repos[1-9]*")
assert len([c for c in config_files if str(yaml_config) in str(c)]) == 1
assert yaml_config in config_files
assert json_config in config_files
def test_find_config_match_list(
config_path: pathlib.Path,
yaml_config: pathlib.Path,
json_config: pathlib.Path,
) -> None:
config_files = config.find_config_files(
path=[config_path],
match=[yaml_config.stem, json_config.stem],
)
assert yaml_config in config_files
assert json_config in config_files
config_files = config.find_config_files(
path=[config_path], match=[yaml_config.stem]
)
assert yaml_config in config_files
assert len([c for c in config_files if str(yaml_config) in str(c)]) == 1
assert json_config not in config_files
assert len([c for c in config_files if str(json_config) in str(c)]) == 0
def test_find_config_filetype_string(
config_path: pathlib.Path, yaml_config: pathlib.Path, json_config: pathlib.Path
) -> None:
config_files = config.find_config_files(
path=[config_path], match=yaml_config.stem, filetype="yaml"
)
assert yaml_config in config_files
assert json_config not in config_files
config_files = config.find_config_files(
path=[config_path], match=yaml_config.stem, filetype="json"
)
assert yaml_config not in config_files
assert json_config not in config_files
config_files = config.find_config_files(
path=[config_path], match="repos*", filetype="json"
)
assert yaml_config not in config_files
assert json_config in config_files
config_files = config.find_config_files(
path=[config_path], match="repos*", filetype="*"
)
assert yaml_config in config_files
assert json_config in config_files
def test_find_config_filetype_list(
config_path: pathlib.Path, yaml_config: pathlib.Path, json_config: pathlib.Path
) -> None:
config_files = config.find_config_files(
path=[config_path], match=["repos*"], filetype=["*"]
)
assert yaml_config in config_files
assert json_config in config_files
config_files = config.find_config_files(
path=[config_path], match=["repos*"], filetype=["json", "yaml"]
)
assert yaml_config in config_files
assert json_config in config_files
config_files = config.find_config_files(
path=[config_path], filetype=["json", "yaml"]
)
assert yaml_config in config_files
assert json_config in config_files
def test_find_config_include_home_config_files(
tmp_path: pathlib.Path,
config_path: pathlib.Path,
yaml_config: pathlib.Path,
json_config: pathlib.Path,
) -> None:
with EnvironmentVarGuard() as env:
env.set("HOME", str(tmp_path))
config_files = config.find_config_files(
path=[config_path], match="*", include_home=True
)
assert yaml_config in config_files
assert json_config in config_files
config_file3 = tmp_path / ".vcspull.json"
config_file3.touch()
results = config.find_config_files(
path=[config_path], match="*", include_home=True
)
expected_in = config_file3
assert expected_in in results
assert yaml_config in results
assert json_config in results
def test_merge_nested_dict(tmp_path: pathlib.Path, config_path: pathlib.Path) -> None:
config1 = write_config(
config_path=config_path / "repoduplicate1.yaml",
content=textwrap.dedent(
"""\
/path/to/test/:
subRepoDiffVCS:
url: svn+file:///path/to/svnrepo
subRepoSameVCS: git+file://path/to/gitrepo
vcsOn1: svn+file:///path/to/another/svn
"""
),
)
config2 = write_config(
config_path=config_path / "repoduplicate2.yaml",
content=textwrap.dedent(
"""\
/path/to/test/:
subRepoDiffVCS:
url: git+file:///path/to/diffrepo
subRepoSameVCS: git+file:///path/to/gitrepo
vcsOn2: svn+file:///path/to/another/svn
"""
),
)
# Duplicate path + name with different repo URL / remotes raises.
config_files = config.find_config_files(
path=config_path, match="repoduplicate[1-2]"
)
assert config1 in config_files
assert config2 in config_files
with pytest.raises(exc.VCSPullException):
config.load_configs(config_files)