mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 10:59:59 +00:00
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).
This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.
Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:
* 7 `src:` values built from a role variable holding a bare filename
(svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
The variable doubles as the destination name inside the container, so the
language segment belongs at the call site, not in the variable.
* `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
18 roles ship exactly that and have no template fallback: the play would
have died on each.
* `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
interface now reads `files/javascript/javascript.js`.
* `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
path through `path_absolute(... 'files', ...)`.
* 4 `ansible.builtin.script:` invocations naming a bare command
(pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
* `dev-python/files/shell/install.sh` pointed at a sibling role's script.
Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
163 lines
5.4 KiB
Python
163 lines
5.4 KiB
Python
"""Unit tests for `utils.cache.yaml` (in-process cached YAML loader)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from utils.cache.yaml import (
|
|
_reset_cache_for_tests,
|
|
dump_yaml,
|
|
invalidate,
|
|
load_yaml,
|
|
load_yaml_any,
|
|
)
|
|
|
|
|
|
class TestLoadYaml(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
_reset_cache_for_tests()
|
|
self._tmp = TemporaryDirectory()
|
|
self.addCleanup(self._tmp.cleanup)
|
|
self.tmp = Path(self._tmp.name)
|
|
|
|
def _write(self, name: str, content: str) -> Path:
|
|
path = self.tmp / name
|
|
path.write_text(content, encoding="utf-8")
|
|
return path
|
|
|
|
def test_returns_parsed_mapping(self):
|
|
path = self._write("a.yml", "foo: 1\nbar: [x, y]\n")
|
|
result = load_yaml(path)
|
|
self.assertEqual(result, {"foo": 1, "bar": ["x", "y"]})
|
|
|
|
def test_second_call_returns_same_instance(self):
|
|
path = self._write("a.yml", "foo: 1\n")
|
|
first = load_yaml(path)
|
|
second = load_yaml(path)
|
|
self.assertIs(first, second)
|
|
|
|
def test_resolved_paths_share_cache_entry(self):
|
|
path = self._write("a.yml", "foo: 1\n")
|
|
first = load_yaml(path)
|
|
sibling = path.parent / ("./" + path.name)
|
|
second = load_yaml(sibling)
|
|
self.assertIs(first, second)
|
|
|
|
def test_empty_file_returns_empty_dict(self):
|
|
path = self._write("empty.yml", "")
|
|
result = load_yaml(path)
|
|
self.assertEqual(result, {})
|
|
|
|
def test_non_mapping_root_raises_type_error(self):
|
|
path = self._write("list.yml", "- 1\n- 2\n")
|
|
with self.assertRaisesRegex(TypeError, "mapping at top-level"):
|
|
load_yaml(path)
|
|
|
|
def test_missing_file_raises_by_default(self):
|
|
with self.assertRaises(FileNotFoundError):
|
|
load_yaml(self.tmp / "missing.yml")
|
|
|
|
def test_missing_file_returns_default_when_provided(self):
|
|
result = load_yaml(self.tmp / "missing.yml", default_if_missing={})
|
|
self.assertEqual(result, {})
|
|
|
|
def test_missing_file_default_is_not_cached(self):
|
|
path = self.tmp / "appears.yml"
|
|
first = load_yaml(path, default_if_missing={"placeholder": True})
|
|
self.assertEqual(first, {"placeholder": True})
|
|
path.write_text("real: yes\n", encoding="utf-8")
|
|
second = load_yaml(path)
|
|
self.assertEqual(second, {"real": True})
|
|
|
|
|
|
class TestLoadYamlAny(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
_reset_cache_for_tests()
|
|
self._tmp = TemporaryDirectory()
|
|
self.addCleanup(self._tmp.cleanup)
|
|
self.tmp = Path(self._tmp.name)
|
|
|
|
def _write(self, name: str, content: str) -> Path:
|
|
path = self.tmp / name
|
|
path.write_text(content, encoding="utf-8")
|
|
return path
|
|
|
|
def test_list_root_returned_unchanged(self):
|
|
path = self._write("tasks.yml", "- name: a\n debug:\n msg: hi\n")
|
|
result = load_yaml_any(path)
|
|
self.assertEqual(result, [{"name": "a", "debug": {"msg": "hi"}}])
|
|
|
|
def test_dict_root_returned_unchanged(self):
|
|
path = self._write("config.yml", "foo: 1\n")
|
|
self.assertEqual(load_yaml_any(path), {"foo": 1})
|
|
|
|
def test_empty_file_returns_empty_dict(self):
|
|
path = self._write("empty.yml", "")
|
|
self.assertEqual(load_yaml_any(path), {})
|
|
|
|
def test_load_yaml_any_and_load_yaml_share_cache(self):
|
|
path = self._write("config.yml", "foo: 1\n")
|
|
first = load_yaml_any(path)
|
|
second = load_yaml(path)
|
|
self.assertIs(first, second)
|
|
|
|
def test_load_yaml_strict_rejects_list_root(self):
|
|
path = self._write("tasks.yml", "- a\n- b\n")
|
|
with self.assertRaisesRegex(TypeError, "mapping at top-level"):
|
|
load_yaml(path)
|
|
|
|
|
|
class TestDumpYaml(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
_reset_cache_for_tests()
|
|
self._tmp = TemporaryDirectory()
|
|
self.addCleanup(self._tmp.cleanup)
|
|
self.tmp = Path(self._tmp.name)
|
|
|
|
def test_dump_writes_file(self):
|
|
path = self.tmp / "out.yml"
|
|
dump_yaml(path, {"a": 1, "b": [2, 3]})
|
|
self.assertEqual(load_yaml(path), {"a": 1, "b": [2, 3]})
|
|
|
|
def test_dump_creates_parent_dirs(self):
|
|
path = self.tmp / "deep" / "nested" / "out.yml"
|
|
dump_yaml(path, {"k": "v"})
|
|
self.assertTrue(path.exists())
|
|
|
|
def test_dump_evicts_cached_entry(self):
|
|
path = self.tmp / "x.yml"
|
|
path.write_text("v: 1\n", encoding="utf-8")
|
|
cached = load_yaml(path)
|
|
self.assertEqual(cached["v"], 1)
|
|
dump_yaml(path, {"v": 2})
|
|
refreshed = load_yaml(path)
|
|
self.assertEqual(refreshed["v"], 2)
|
|
self.assertIsNot(cached, refreshed)
|
|
|
|
|
|
class TestInvalidate(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
_reset_cache_for_tests()
|
|
self._tmp = TemporaryDirectory()
|
|
self.addCleanup(self._tmp.cleanup)
|
|
self.tmp = Path(self._tmp.name)
|
|
|
|
def test_invalidate_drops_entry(self):
|
|
path = self.tmp / "x.yml"
|
|
path.write_text("v: 1\n", encoding="utf-8")
|
|
first = load_yaml(path)
|
|
path.write_text("v: 2\n", encoding="utf-8")
|
|
invalidate(path)
|
|
second = load_yaml(path)
|
|
self.assertEqual(second["v"], 2)
|
|
self.assertIsNot(first, second)
|
|
|
|
def test_invalidate_unknown_path_is_noop(self):
|
|
invalidate(self.tmp / "never-loaded.yml")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|