Skip to content

Commit 1cbaab3

Browse files
authored
Test cleanup (#359)
- Refactor params for write_config - Add import_config shortcut to access kaptanKaptan - Use import_raw/load_raw helpers for test_config.py
2 parents 71dfaa9 + 0c0044b commit 1cbaab3

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

tests/helpers.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"""Helpers for vcspull."""
22
import os
33
import pathlib
4+
import textwrap
5+
from typing import Literal
6+
7+
import kaptan
48

59

610
class EnvironmentVarGuard:
@@ -39,9 +43,14 @@ def __exit__(self, *ignore_exc):
3943
del self._environ[unset]
4044

4145

42-
def write_config(
43-
config_path: pathlib.Path, filename: str, content: str
44-
) -> pathlib.Path:
45-
config = config_path / filename
46-
config.write_text(content, encoding="utf-8")
47-
return config
46+
def write_config(config_path: pathlib.Path, content: str) -> pathlib.Path:
47+
config_path.write_text(content, encoding="utf-8")
48+
return config_path
49+
50+
51+
def import_raw(data: str, format: Literal["yaml", "json"]) -> kaptan.Kaptan:
52+
return kaptan.Kaptan(handler=format).import_config(textwrap.dedent(data))
53+
54+
55+
def load_raw(data: str, format: Literal["yaml", "json"]) -> dict:
56+
return import_raw(data=data, format=format).export("dict")

tests/test_config_file.py

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from vcspull.config import expand_dir, extract_repos
1212

1313
from .fixtures import example as fixtures
14-
from .helpers import EnvironmentVarGuard, write_config
14+
from .helpers import EnvironmentVarGuard, import_raw, load_raw, write_config
1515

1616

1717
@pytest.fixture(scope="function")
@@ -30,9 +30,8 @@ def json_config(config_path: pathlib.Path):
3030

3131
def test_dict_equals_yaml():
3232
# Verify that example YAML is returning expected dict format.
33-
config = kaptan.Kaptan(handler="yaml").import_config(
34-
textwrap.dedent(
35-
"""\
33+
config = import_raw(
34+
"""\
3635
/home/me/myproject/study/:
3736
linux: git+git://git.kernel.org/linux/torvalds/linux.git
3837
freebsd: git+https://github.com/freebsd/freebsd.git
@@ -52,8 +51,8 @@ def test_dict_equals_yaml():
5251
url: [email protected]:tony/tmux-config.git
5352
shell_command_after:
5453
- ln -sf /home/me/.tmux/.tmux.conf /home/me/.tmux.conf
55-
"""
56-
)
54+
""",
55+
format="yaml",
5756
)
5857
assert fixtures.config_dict == config.export("dict")
5958

@@ -121,11 +120,8 @@ def test_expand_shell_command_after():
121120

122121
def test_expandenv_and_homevars():
123122
# Assure ~ tildes and environment template vars expand.
124-
config1 = (
125-
kaptan.Kaptan(handler="yaml")
126-
.import_config(
127-
textwrap.dedent(
128-
"""\
123+
config1 = load_raw(
124+
"""\
129125
'~/study/':
130126
sphinx: hg+file://{hg_repo_path}
131127
docutils: svn+file://{svn_repo_path}
@@ -140,16 +136,11 @@ def test_expandenv_and_homevars():
140136
url: git+file://{git_repo_path}
141137
.tmux:
142138
url: git+file://{git_repo_path}
143-
"""
144-
)
145-
)
146-
.export("dict")
139+
""",
140+
format="yaml",
147141
)
148-
config2 = (
149-
kaptan.Kaptan(handler="json")
150-
.import_config(
151-
textwrap.dedent(
152-
"""\
142+
config2 = load_raw(
143+
"""\
153144
{
154145
"~/study/": {
155146
"sphinx": "hg+file://${hg_repo_path}",
@@ -165,10 +156,8 @@ def test_expandenv_and_homevars():
165156
}
166157
}
167158
}
168-
"""
169-
)
170-
)
171-
.export("dict")
159+
""",
160+
format="json",
172161
)
173162

174163
config1_expanded = extract_repos(config1)
@@ -191,7 +180,7 @@ def test_find_config_files(tmp_path: pathlib.Path):
191180
pull_config.touch()
192181
with EnvironmentVarGuard() as env:
193182
env.set("HOME", str(tmp_path))
194-
os.environ.get("HOME") == str(tmp_path)
183+
assert pathlib.Path.home() == tmp_path
195184
expectedIn = str(tmp_path / ".vcspull.yaml")
196185
results = config.find_home_config_files()
197186

@@ -206,7 +195,7 @@ def test_multiple_config_files_raises_exception(tmp_path: pathlib.Path):
206195
with EnvironmentVarGuard() as env:
207196
with pytest.raises(exc.MultipleConfigWarning):
208197
env.set("HOME", str(tmp_path))
209-
os.environ.get("HOME") == str(tmp_path)
198+
assert pathlib.Path.home() == tmp_path
210199

211200
config.find_home_config_files()
212201

@@ -370,8 +359,7 @@ def test_find_config_include_home_config_files(
370359

371360
def test_merge_nested_dict(tmp_path: pathlib.Path, config_path: pathlib.Path):
372361
config1 = write_config(
373-
config_path=config_path,
374-
filename="repoduplicate1.yaml",
362+
config_path=config_path / "repoduplicate1.yaml",
375363
content=textwrap.dedent(
376364
"""\
377365
/path/to/test/:
@@ -383,8 +371,7 @@ def test_merge_nested_dict(tmp_path: pathlib.Path, config_path: pathlib.Path):
383371
),
384372
)
385373
config2 = write_config(
386-
config_path=config_path,
387-
filename="repoduplicate2.yaml",
374+
config_path=config_path / "repoduplicate2.yaml",
388375
content=textwrap.dedent(
389376
"""\
390377
/path/to/test/:

tests/test_sync.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ def test_makes_recursive(
3636
repo.obtain()
3737

3838

39-
def write_config_remote(tmp_path, config_tpl, repo_dir, clone_name):
39+
def write_config_remote(
40+
config_path: pathlib.Path, tmp_path: pathlib.Path, config_tpl, repo_dir, clone_name
41+
):
4042
return write_config(
41-
tmp_path,
42-
"myrepos.yaml",
43-
config_tpl.format(
44-
tmp_path=str(tmp_path), repo_dir=repo_dir, CLONE_NAME=clone_name
43+
config_path=config_path,
44+
content=config_tpl.format(
45+
tmp_path=str(tmp_path.parent), repo_dir=repo_dir, CLONE_NAME=clone_name
4546
),
4647
)
4748

@@ -88,6 +89,7 @@ def test_config_variations(
8889
dummy_repo = create_git_dummy_repo(dummy_repo_name)
8990

9091
config_file = write_config_remote(
92+
config_path=tmp_path / "myrepos.yaml",
9193
tmp_path=tmp_path,
9294
config_tpl=config_tpl,
9395
repo_dir=dummy_repo,

0 commit comments

Comments
 (0)