Files
computer-playbook/tests/unit/utils/cache/test_yaml.py
T
kevinveenbirkenbach 12dba01156 refactor(utils): consolidate cache modules into utils/cache/ package
Both in-process cache layers — the cached YAML loader and the cached
runtime-data getters — now live next to each other under
`utils/cache/`. The package gains a README that documents the
"orthogonal layers, no third cache" rule so future contributors hit
the right primitive on the first look.

Moves:
- `utils/yaml_cache.py` -> `utils/cache/yaml.py`
- `utils/runtime_data.py` -> `utils/cache/data.py`
- `tests/unit/utils/test_yaml_cache.py` -> `tests/unit/utils/cache/test_yaml.py`
- `tests/unit/utils/test_runtime_data.py` -> `tests/unit/utils/cache/test_data.py`
- `tests/unit/utils/test_variants.py` -> `tests/unit/utils/cache/test_variants.py`

New:
- `utils/cache/__init__.py` (empty per project convention)
- `utils/cache/README.md` describing the two layers, when to reach for
  which, lifetime, test-only helpers, and design guidelines (RFC 2119
  keywords, emoji-after-headline, no self-SPOT declaration, no
  slash-in-link-text).
- `tests/unit/utils/cache/__init__.py`

Mechanical updates:
- All imports across cli/, plugins/lookup/, tests/, and roles/ rewritten
  from `utils.yaml_cache` -> `utils.cache.yaml` and
  `utils.runtime_data` -> `utils.cache.data` (40+ files).
- `REPO_ROOT` parents-index in the moved tests bumped from `parents[3]`
  to `parents[4]` to reflect the deeper nesting.
- Six broken markdown links in docs/ updated to the new module paths
  (test_markdown_links lint test stays green).
- Two test files that used the `from utils import runtime_data` form
  now use `from utils.cache import data as runtime_data` so the local
  alias remains.

Tests: 848 passed across unit + lint suites (1 pre-existing collection
error in test_host_vars.py unrelated to this change).
2026-04-25 17:17:14 +02:00

175 lines
6.1 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)
# Same instance proves the cache hit (not just structural equal).
self.assertIs(first, second)
def test_resolved_paths_share_cache_entry(self):
# Two different spellings of the same absolute path MUST hit the
# same cache entry. We use "./<name>" vs the bare name from the
# tmp dir as the second spelling.
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_value_error(self):
path = self._write("list.yml", "- 1\n- 2\n")
with self.assertRaisesRegex(ValueError, "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):
# If the file appears later, the next read MUST see it instead
# of the synthetic default.
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):
# The two entry points MUST hit the same cache so a hot path
# that mixes them does not pay the parse cost twice.
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(ValueError, "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)
# External writer would normally bypass the cache; dump_yaml
# MUST clear the entry so the next read sees the new content.
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)
# Simulate an external rewrite then explicit invalidation.
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):
# Nothing in the cache for this path, so the call must not raise.
invalidate(self.tmp / "never-loaded.yml")
if __name__ == "__main__":
unittest.main()