Files
daedalus/daedalus/tests/test_registry.py
T

126 lines
4.9 KiB
Python

"""Tests for daedalus.registry: Script records, categories, and SCRIPTS consistency.
Every registered script is valid, present, and runnable (SPEC.md section 4).
"""
import pytest
from daedalus import LIBRARY_ROOT
from daedalus import REPO_ROOT
from daedalus import registry
from daedalus import runners
ENTRIES = sorted(registry.SCRIPTS.items())
@pytest.mark.parametrize("name, script", ENTRIES)
def test_name_is_valid(name, script):
"""Names are lowercase, start with a letter, and never with '-'."""
assert registry.NAME_PATTERN.match(name), name
@pytest.mark.parametrize("name, script", ENTRIES)
def test_script_file_exists(name, script):
"""The registered path is a file in this checkout."""
assert script.absolute_path.is_file(), f"{name}: {script.path}"
@pytest.mark.parametrize("name, script", ENTRIES)
def test_path_is_repo_relative_posix(name, script):
"""Paths are relative and use forward slashes."""
assert "\\" not in script.path and not script.path.startswith("/"), f"{name}: {script.path}"
assert script.absolute_path.resolve().is_relative_to(LIBRARY_ROOT)
@pytest.mark.parametrize("name, script", ENTRIES)
def test_suffix_has_runner(name, script):
"""The file suffix maps to a runner."""
assert script.absolute_path.suffix.lower() in runners.RUNNERS, f"{name}: {script.path}"
@pytest.mark.parametrize("name, script", ENTRIES)
def test_listed_in_readme(name, script):
"""README "Available scripts" has a row for every registered script."""
readme = (REPO_ROOT / "README.md").read_text(encoding="utf-8")
assert f"| `{name}` |" in readme, f"add {name} to README.md Available scripts"
@pytest.mark.parametrize("name, script", ENTRIES)
def test_description_is_one_line(name, script):
"""Descriptions are non-empty single lines."""
assert script.description.strip() and "\n" not in script.description, name
@pytest.mark.parametrize("name, script", ENTRIES)
def test_categories_are_valid(name, script):
"""Categories are non-empty labels matching the name pattern (SPEC item 4.8.5)."""
assert script.categories, name
for label in script.categories:
assert registry.NAME_PATTERN.match(label), f"{name}: {label}"
assert name not in registry.used_categories(), f"{name} is also a category label"
def test_script_with_default_categories():
"""A script without categories lands in the default 'general' (SPEC item 4.1.3)."""
assert registry.Script("python/src/x/x.py", "X").categories == ("general",)
@pytest.mark.parametrize("value, expected", [
("media", ("media",)),
(["media", "linux"], ("media", "linux")),
(("media", "linux"), ("media", "linux")),
])
def test_categories_normalization(value, expected):
"""Strings become one-item tuples; lists become tuples (SPEC item 4.1.3)."""
assert registry.Script("python/src/x/x.py", "X", categories=value).categories == expected
@pytest.mark.parametrize("value", ["", [], ()])
def test_empty_categories_rejected(value):
"""Empty categories are a ValueError (SPEC item 4.1.3)."""
with pytest.raises(ValueError):
registry.Script("python/src/x/x.py", "X", categories=value)
def test_categories_display():
"""The display form joins labels with a comma and a space (SPEC item 4.1.5)."""
script = registry.Script("bash/x.sh", "X", categories=["media", "linux"])
assert script.categories_display == "media, linux"
@pytest.fixture
def fake_registry(monkeypatch):
"""A fixed registry exercising several scripts and labels."""
scripts = {
"alpha": registry.Script("python/src/alpha/alpha.py", "First", categories="general"),
"beta_tool": registry.Script("bash/beta_tool.sh", "Second", categories=("media", "linux")),
"gamma": registry.Script("powershell/gamma.ps1", "Third", categories=("media",)),
}
monkeypatch.setattr(registry, "SCRIPTS", scripts)
return scripts
def test_used_categories(fake_registry):
"""used_categories() is the set of every label (SPEC item 4.4.1)."""
assert registry.used_categories() == {"general", "media", "linux"}
@pytest.mark.parametrize("token, expected", [
("media", True),
("alpha", False), # a script name is never a category token (SPEC item 3.4)
("nope", False),
])
def test_is_category_label(fake_registry, token, expected):
"""is_category_label: false for script names, true for labels in use."""
assert registry.is_category_label(token) is expected
def test_scripts_matching_categories_is_and(fake_registry):
"""Multiple labels are ANDed; results are sorted by name (SPEC item 4.4.3)."""
assert list(registry.scripts_matching_categories(["media", "linux"])) == ["beta_tool"]
assert list(registry.scripts_matching_categories(["media"])) == ["beta_tool", "gamma"]
def test_scripts_matching_categories_empty_returns_all(fake_registry):
"""No labels means every script, sorted by name."""
assert list(registry.scripts_matching_categories([])) == ["alpha", "beta_tool", "gamma"]