Skip to content

Commit 2570cc2

Browse files
committed
tests/discover(add[config-scope]): parametrize classify helper
1 parent a8b1547 commit 2570cc2

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

tests/cli/test_discover.py

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
from __future__ import annotations
44

5+
import pathlib
56
import re
67
import subprocess
78
import typing as t
89

910
import pytest
1011

1112
from vcspull._internal.private_path import PrivatePath
12-
from vcspull.cli.discover import discover_repos
13+
from vcspull.cli.discover import ConfigScope, _classify_config_scope, discover_repos
1314

1415
if t.TYPE_CHECKING:
1516
import pathlib
@@ -202,6 +203,55 @@ class DiscoverFixture(t.NamedTuple):
202203
]
203204

204205

206+
class ConfigScopeFixture(t.NamedTuple):
207+
"""Fixture describing config scope classification scenarios."""
208+
209+
test_id: str
210+
config_template: str
211+
cwd_template: str
212+
env: dict[str, str]
213+
expected_scope: ConfigScope
214+
215+
216+
CONFIG_SCOPE_FIXTURES: list[ConfigScopeFixture] = [
217+
ConfigScopeFixture(
218+
test_id="scope-user-home-default",
219+
config_template="{home}/.vcspull.yaml",
220+
cwd_template="{home}",
221+
env={},
222+
expected_scope="user",
223+
),
224+
ConfigScopeFixture(
225+
test_id="scope-user-xdg-home",
226+
config_template="{xdg_home}/vcspull/personal.yaml",
227+
cwd_template="{home}",
228+
env={"XDG_CONFIG_HOME": "{xdg_home}"},
229+
expected_scope="user",
230+
),
231+
ConfigScopeFixture(
232+
test_id="scope-system-xdg-dirs",
233+
config_template="{xdg_system}/vcspull/system.yaml",
234+
cwd_template="{home}",
235+
env={"XDG_CONFIG_DIRS": "{xdg_system}"},
236+
expected_scope="system",
237+
),
238+
ConfigScopeFixture(
239+
test_id="scope-project-relative",
240+
config_template="{project}/.vcspull.yaml",
241+
cwd_template="{project}",
242+
env={},
243+
expected_scope="project",
244+
),
245+
ConfigScopeFixture(
246+
test_id="scope-external-file",
247+
config_template="{external}/configs/custom.yaml",
248+
cwd_template="{project}",
249+
env={},
250+
expected_scope="external",
251+
),
252+
]
253+
254+
205255
class DiscoverLoadEdgeFixture(t.NamedTuple):
206256
"""Fixture describing discover configuration loading edge cases."""
207257

@@ -401,6 +451,66 @@ def test_discover_repos(
401451
)
402452

403453

454+
@pytest.mark.parametrize(
455+
list(ConfigScopeFixture._fields),
456+
CONFIG_SCOPE_FIXTURES,
457+
ids=[fixture.test_id for fixture in CONFIG_SCOPE_FIXTURES],
458+
)
459+
def test_classify_config_scope(
460+
test_id: str,
461+
config_template: str,
462+
cwd_template: str,
463+
env: dict[str, str],
464+
expected_scope: ConfigScope,
465+
tmp_path: pathlib.Path,
466+
monkeypatch: MonkeyPatch,
467+
) -> None:
468+
base_home = tmp_path / "home"
469+
base_home.mkdir()
470+
project = tmp_path / "project"
471+
project.mkdir()
472+
xdg_home = tmp_path / "xdg-home"
473+
(xdg_home / "vcspull").mkdir(parents=True)
474+
xdg_system = tmp_path / "xdg-system"
475+
(xdg_system / "vcspull").mkdir(parents=True)
476+
external = tmp_path / "external"
477+
(external / "configs").mkdir(parents=True)
478+
479+
replacements = {
480+
"home": base_home,
481+
"project": project,
482+
"xdg_home": xdg_home,
483+
"xdg_system": xdg_system,
484+
"external": external,
485+
}
486+
487+
def _expand(template: str) -> pathlib.Path:
488+
expanded = template
489+
for key, path in replacements.items():
490+
expanded = expanded.replace(f"{{{key}}}", str(path))
491+
return pathlib.Path(expanded)
492+
493+
config_path = _expand(config_template)
494+
config_path.parent.mkdir(parents=True, exist_ok=True)
495+
config_path.touch()
496+
497+
cwd_path = _expand(cwd_template)
498+
cwd_path.mkdir(parents=True, exist_ok=True)
499+
500+
monkeypatch.chdir(cwd_path)
501+
monkeypatch.setenv("HOME", str(base_home))
502+
for var in ["XDG_CONFIG_HOME", "XDG_CONFIG_DIRS"]:
503+
monkeypatch.delenv(var, raising=False)
504+
for key, value in env.items():
505+
expanded_value = value
506+
for name, path in replacements.items():
507+
expanded_value = expanded_value.replace(f"{{{name}}}", str(path))
508+
monkeypatch.setenv(key, expanded_value)
509+
510+
scope = _classify_config_scope(config_path, cwd=cwd_path, home=base_home)
511+
assert scope == expected_scope
512+
513+
404514
@pytest.mark.parametrize(
405515
list(DiscoverLoadEdgeFixture._fields),
406516
DISCOVER_LOAD_EDGE_FIXTURES,

0 commit comments

Comments
 (0)