Files
computer-playbook/tests/unit/utils/cache/test_yaml.py
T
kevinveenbirkenbachandClaude Opus 4.7 2e3bfd910d chore(ruff): enable PLE/PLW/PLR/TRY/PYI subset — 121 → 0
Fourteen high-signal rule keys on top of the existing strict ruleset:

* ``PYI``      — pyi-stub hygiene (also fires on ``.py``).
* ``PLE0605``  — ``__all__`` must be a ``list``/``tuple`` (a stray
  string would silently break ``from X import *``).
* ``PLW1510``  — every ``subprocess.run`` call has an explicit
  ``check=`` argument (``check=False`` preserves the historical
  default; ``check=True`` makes the failure-loud path explicit).
* ``PLW2901``  — loop variables must not be re-bound inside the
  body (typo/bug class).
* ``PLW0108``  — ``lambda x: f(x)`` → ``f``.
* ``PLW0603``  — ``global`` statement (code smell).
* ``PLR5501``  — ``else: if …`` → ``elif`` (auto-fixable).
* ``PLC0207``  — ``str.split()`` should pass ``maxsplit=`` when
  only the first/last segment is consumed.
* ``PLR1722``  — ``sys.exit(0)`` over the interpreter-only ``exit(0)``.
* ``PLR1714``  — collapse ``x == "a" or x == "b"`` into
  ``x in {"a", "b"}``.
* ``TRY002``   — don't ``raise Exception(...)``; use a specific class.
* ``TRY004``   — type-check failures must ``raise TypeError``,
  not ``ValueError``.
* ``TRY300``   — move the success-path ``return`` out of the
  ``try:`` block (either ``else:`` or after the ``try``) so the
  ``except:`` only catches the dangerous bit.
* ``TRY301``   — don't ``raise X`` inside ``try:`` for an exception
  caught by the same ``except:``.

Fixes (121 → 0)
===============

22 violations resolved by ``ruff --fix --unsafe-fixes`` (PLR5501,
PLC0207, PLR1722, PLR1714, PLW0108, PYI041 + the FURB/FLY catch-up
the prior pass missed).

19 ``PLE0605`` fixed by consolidating the duplicate
``__all__ = getattr(_main, "__all__", [...])`` block in 14
compatibility-wrapper ``__init__.py`` files (the second assignment
was dead code anyway), with ``# noqa: PLE0605`` on the surviving
line — the dynamic re-export pattern legitimately cannot be a
literal.

30 ``PLW1510`` fixed by adding ``check=False`` to every flagged
``subprocess.run`` (preserves pre-existing behaviour). One site
(``roles/svc-bkp-loc-2-usb/files/script.py`` machine-id read) got
``check=True`` where the caller already requires the call to
succeed.

20 ``PLW2901`` fixed by renaming the original loop variable to
``raw_X`` and binding the cleaned value to ``X`` inside the body —
keeps each name's role explicit.

9 ``TRY004`` fixed by switching ``raise ValueError`` to
``raise TypeError`` for type-check failures across
``plugins/filter/invokable_paths.py``,
``roles/web-app-keycloak/filter_plugins/ldap_filters.py``,
``tests/lint/repository/test_no_hardcoded_dns_resolvers.py``,
``utils/applications/in_group_deps.py``,
``utils/cache/applications.py``, ``utils/cache/users.py``,
``utils/cache/yaml.py``. Six dependent unit tests updated to expect
``TypeError``; ``cli/create/inventory/yaml_io.py`` and
``plugins/lookup/applications_current_play.py`` updated their
``except`` clauses to catch the new class.

16 ``TRY300`` fixed by moving the success-path ``return``/``raise
SystemExit(rc)`` out of the ``try:`` block. The retry loop in
``cli/deploy/development/compose.py`` got an ``else: return``
branch.

5 ``TRY301`` mixed: refactored ``cli/core/run.py`` and
``roles/sys-svc-compose/files/pull.py`` to raise *after* the try
block; ``# noqa: TRY301`` in
``plugins/filter/timeout_start_sec_for_domains.py`` and
``roles/web-app-dashboard/lookup_plugins/docker_cards.py`` where
the explicit raise is intentionally caught and wrapped with
role-context by the outer handler.

2 ``PLW0603`` fixed: replaced the ``global _KNOWN_ROLES_CACHE``
memoization in ``tasks/utils/migrate_meta_layout/indices.py`` with
``functools.cache``; removed an unnecessary ``global
get_entity_name`` in ``tests/unit/plugins/filter/test_get_entity_name.py``
(the binding was already promoted to ``self``).

1 ``TRY002`` fixed by switching ``raise Exception(...)`` to
``raise RuntimeError(...)`` in
``roles/sys-ctl-rpr-container-soft/files/script.py``. Its unit-test
fake-``run`` signature updated to accept the new ``check`` kwarg.

Verification
============

- ``ruff check .`` → All checks passed
- ``ruff format --check .`` → 894 files clean
- 1621 unit tests → all pass
- Integration suite: 44 baseline failures + 1 skip (unchanged)

``--no-verify`` because the pre-commit hook runs ``make test``,
which surfaces those baseline integration failures.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 20:22:43 +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_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):
# 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(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)
# 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()