varar-config 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- varar_config-0.5.0/.gitignore +32 -0
- varar_config-0.5.0/PKG-INFO +6 -0
- varar_config-0.5.0/pyproject.toml +14 -0
- varar_config-0.5.0/src/varar_config/__init__.py +3 -0
- varar_config-0.5.0/src/varar_config/config.py +70 -0
- varar_config-0.5.0/tests/test_config.py +62 -0
- varar_config-0.5.0/tests/test_conformance.py +34 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
node_modules
|
|
2
|
+
dist
|
|
3
|
+
.DS_Store
|
|
4
|
+
*.tsbuildinfo
|
|
5
|
+
coverage
|
|
6
|
+
*.timestamp-*
|
|
7
|
+
.superpowers
|
|
8
|
+
.var/
|
|
9
|
+
|
|
10
|
+
# varar.lock.json baselines written by var-cli integration tests (temp fixtures)
|
|
11
|
+
typescript/packages/cli/tests/fixtures/run-basic/varar.lock.json
|
|
12
|
+
typescript/packages/cli/tests/fixtures/drift-tmp-*/
|
|
13
|
+
# varar.lock.json written by the Python conformance dogfood command
|
|
14
|
+
python/varar.lock.json
|
|
15
|
+
# varar.lock.json written into the Kotest fixture roots on every test run
|
|
16
|
+
# (the drift fixture's baseline IS committed and is not listed here)
|
|
17
|
+
java/kotest/src/test/resources/kotest-smoke/varar.lock.json
|
|
18
|
+
java/kotest/src/test/resources/kotest-failing/varar.lock.json
|
|
19
|
+
.claude/*
|
|
20
|
+
!.claude/skills/
|
|
21
|
+
|
|
22
|
+
# Python
|
|
23
|
+
.venv/
|
|
24
|
+
__pycache__/
|
|
25
|
+
*.pyc
|
|
26
|
+
.pytest_cache/
|
|
27
|
+
.ruff_cache/
|
|
28
|
+
.coverage
|
|
29
|
+
htmlcov/
|
|
30
|
+
coverage.lcov
|
|
31
|
+
|
|
32
|
+
release/dist/
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "varar-config"
|
|
3
|
+
version = "0.5.0"
|
|
4
|
+
description = "Reads varar.config.json — the shared config file for all var tools"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
dependencies = []
|
|
8
|
+
|
|
9
|
+
[build-system]
|
|
10
|
+
requires = ["hatchling"]
|
|
11
|
+
build-backend = "hatchling.build"
|
|
12
|
+
|
|
13
|
+
[tool.hatch.build.targets.wheel]
|
|
14
|
+
packages = ["src/varar_config"]
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Mapping
|
|
7
|
+
|
|
8
|
+
_KNOWN_KEYS = {"$schema", "docs", "steps", "snippets", "scannerPlugins"}
|
|
9
|
+
_KNOWN_DOCS_KEYS = {"include", "exclude"}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True, slots=True)
|
|
13
|
+
class VarConfig:
|
|
14
|
+
docs_include: tuple[str, ...] = ()
|
|
15
|
+
docs_exclude: tuple[str, ...] = ()
|
|
16
|
+
steps: tuple[str, ...] = ()
|
|
17
|
+
snippets: Mapping[str, str] = field(default_factory=dict)
|
|
18
|
+
scanner_plugins: tuple[str, ...] = ()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _string_tuple(value: object, key: str, path: Path) -> tuple[str, ...]:
|
|
22
|
+
if value is None:
|
|
23
|
+
return ()
|
|
24
|
+
if not isinstance(value, list) or not all(isinstance(v, str) for v in value):
|
|
25
|
+
raise ValueError(f"{path}: '{key}' must be an array of strings")
|
|
26
|
+
return tuple(value)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def read_varar_config(root: str | Path) -> VarConfig:
|
|
30
|
+
"""Read ``<root>/varar.config.json``.
|
|
31
|
+
|
|
32
|
+
Missing file -> empty config (tools no-op; matches every other port).
|
|
33
|
+
Malformed JSON, wrong types, or unknown keys -> ``ValueError`` starting
|
|
34
|
+
with the file path — a typo'd config must fail loudly, never silently
|
|
35
|
+
discover nothing. See conformance/config/README.md for the shared rules.
|
|
36
|
+
"""
|
|
37
|
+
path = Path(root) / "varar.config.json"
|
|
38
|
+
if not path.is_file():
|
|
39
|
+
return VarConfig()
|
|
40
|
+
try:
|
|
41
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
42
|
+
except json.JSONDecodeError as e:
|
|
43
|
+
raise ValueError(f"{path}: invalid JSON: {e}") from e
|
|
44
|
+
if not isinstance(data, dict):
|
|
45
|
+
raise ValueError(f"{path}: top level must be an object")
|
|
46
|
+
unknown = set(data) - _KNOWN_KEYS
|
|
47
|
+
if unknown:
|
|
48
|
+
raise ValueError(f"{path}: unknown key(s): {', '.join(sorted(unknown))}")
|
|
49
|
+
docs = data.get("docs")
|
|
50
|
+
if docs is None:
|
|
51
|
+
docs = {}
|
|
52
|
+
if not isinstance(docs, dict):
|
|
53
|
+
raise ValueError(f"{path}: 'docs' must be an object")
|
|
54
|
+
unknown_docs = set(docs) - _KNOWN_DOCS_KEYS
|
|
55
|
+
if unknown_docs:
|
|
56
|
+
raise ValueError(f"{path}: unknown docs key(s): {', '.join(sorted(unknown_docs))}")
|
|
57
|
+
snippets = data.get("snippets")
|
|
58
|
+
if snippets is None:
|
|
59
|
+
snippets = {}
|
|
60
|
+
if not isinstance(snippets, dict) or not all(
|
|
61
|
+
isinstance(k, str) and isinstance(v, str) for k, v in snippets.items()
|
|
62
|
+
):
|
|
63
|
+
raise ValueError(f"{path}: 'snippets' must be an object of strings")
|
|
64
|
+
return VarConfig(
|
|
65
|
+
docs_include=_string_tuple(docs.get("include"), "docs.include", path),
|
|
66
|
+
docs_exclude=_string_tuple(docs.get("exclude"), "docs.exclude", path),
|
|
67
|
+
steps=_string_tuple(data.get("steps"), "steps", path),
|
|
68
|
+
snippets=dict(snippets),
|
|
69
|
+
scanner_plugins=_string_tuple(data.get("scannerPlugins"), "scannerPlugins", path),
|
|
70
|
+
)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from varar_config import VarConfig, read_varar_config
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def _write(tmp_path, body: str):
|
|
7
|
+
(tmp_path / "varar.config.json").write_text(body, encoding="utf-8")
|
|
8
|
+
return tmp_path
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_reads_all_keys(tmp_path):
|
|
12
|
+
root = _write(
|
|
13
|
+
tmp_path,
|
|
14
|
+
'{"docs": {"include": ["a/**/*.md"], "exclude": ["a/wip/**"]},'
|
|
15
|
+
' "steps": ["**/*_steps.py"], "snippets": {"python": "P"},'
|
|
16
|
+
' "scannerPlugins": ["gherkinTables"]}',
|
|
17
|
+
)
|
|
18
|
+
cfg = read_varar_config(root)
|
|
19
|
+
assert cfg.docs_include == ("a/**/*.md",)
|
|
20
|
+
assert cfg.docs_exclude == ("a/wip/**",)
|
|
21
|
+
assert cfg.steps == ("**/*_steps.py",)
|
|
22
|
+
assert cfg.snippets == {"python": "P"}
|
|
23
|
+
assert cfg.scanner_plugins == ("gherkinTables",)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_missing_file_is_empty_config(tmp_path):
|
|
27
|
+
assert read_varar_config(tmp_path / "nowhere") == VarConfig()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_all_keys_optional_and_schema_key_ignored(tmp_path):
|
|
31
|
+
root = _write(tmp_path, '{"$schema": "https://x/y.json"}')
|
|
32
|
+
assert read_varar_config(root) == VarConfig()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_malformed_json_raises_with_path(tmp_path):
|
|
36
|
+
root = _write(tmp_path, "{ nope")
|
|
37
|
+
with pytest.raises(ValueError, match=r"varar\.config\.json.*invalid JSON"):
|
|
38
|
+
read_varar_config(root)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_unknown_key_raises(tmp_path):
|
|
42
|
+
root = _write(tmp_path, '{"vars": {}}')
|
|
43
|
+
with pytest.raises(ValueError, match="unknown key"):
|
|
44
|
+
read_varar_config(root)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_wrong_type_raises(tmp_path):
|
|
48
|
+
root = _write(tmp_path, '{"steps": "x"}')
|
|
49
|
+
with pytest.raises(ValueError, match="steps"):
|
|
50
|
+
read_varar_config(root)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_falsy_wrong_type_docs_raises(tmp_path):
|
|
54
|
+
root = _write(tmp_path, '{"docs": false}')
|
|
55
|
+
with pytest.raises(ValueError, match="docs"):
|
|
56
|
+
read_varar_config(root)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_falsy_wrong_type_snippets_raises(tmp_path):
|
|
60
|
+
root = _write(tmp_path, '{"snippets": []}')
|
|
61
|
+
with pytest.raises(ValueError, match="snippets"):
|
|
62
|
+
read_varar_config(root)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Config conformance: every case in conformance/config/cases must parse to
|
|
2
|
+
the shared golden (byte-for-byte via canonical JSON) or fail if the case has
|
|
3
|
+
an expect-error.txt marker. See conformance/config/README.md."""
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from varar_core.canonical_json import canonical_stringify
|
|
9
|
+
|
|
10
|
+
from varar_config import read_varar_config
|
|
11
|
+
|
|
12
|
+
# python/packages/config/tests -> parents[4] = repo root
|
|
13
|
+
CASES_DIR = Path(__file__).resolve().parents[4] / "conformance" / "config" / "cases"
|
|
14
|
+
CASES = sorted(p for p in CASES_DIR.iterdir() if p.is_dir())
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _artifact(cfg) -> dict:
|
|
18
|
+
return {
|
|
19
|
+
"docs": {"include": list(cfg.docs_include), "exclude": list(cfg.docs_exclude)},
|
|
20
|
+
"steps": list(cfg.steps),
|
|
21
|
+
"snippets": dict(cfg.snippets),
|
|
22
|
+
"scannerPlugins": list(cfg.scanner_plugins),
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@pytest.mark.parametrize("case", CASES, ids=lambda c: c.name)
|
|
27
|
+
def test_config_case(case: Path) -> None:
|
|
28
|
+
if (case / "expect-error.txt").exists():
|
|
29
|
+
with pytest.raises(ValueError):
|
|
30
|
+
read_varar_config(case)
|
|
31
|
+
else:
|
|
32
|
+
actual = canonical_stringify(_artifact(read_varar_config(case)))
|
|
33
|
+
expected = (case / "golden.json").read_text(encoding="utf-8")
|
|
34
|
+
assert actual == expected
|