mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-09 20:26:33 +00:00
Stops every direct `yaml.safe_load`/`safe_load_all`/`safe_dump`/`dump`
call across the test corpus from re-parsing files the production cache
already holds. Each migrated test loads role meta, group_vars, inventory
fixtures, etc. through the shared cache, so a single `make test` run
parses every YAML file at most once across all suites.
Cache module changes (utils/cache/yaml.py):
- Unify single- and multi-doc cache: one `_CACHE: Dict[sig, list]`
holding the `safe_load_all` payload. Single-doc helpers
(load_yaml / load_yaml_any) unwrap docs[0] and raise on >1
document, preserving the old strict semantics. A file consumed
both as single-doc and multi-doc is now parsed exactly once.
- Add load_yaml_all (path-cached) and load_yaml_all_str (string,
no cache) so multi-doc consumers no longer need a direct
`yaml.safe_load_all` plus a separate file read.
- dump_yaml now accepts any safe-dumpable structure (dict, list,
scalar) so test fixtures writing list-of-tasks YAML can use it.
Lint guard (tests/lint/repository/test_no_direct_yaml_calls.py):
- Extend the scanned tree to include `tests/` so future test code
is forced through the cache, with the existing
`# noqa: direct-yaml` per-call opt-out for genuine raw-yaml
fixture needs.
Test corpus:
- 89 test files migrated to load_yaml / load_yaml_any /
load_yaml_str / load_yaml_all / load_yaml_all_str / dump_yaml /
dump_yaml_str. No `# noqa` shortcuts.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
import unittest
|
|
import tempfile
|
|
import shutil
|
|
import os
|
|
from cli.build import graph
|
|
|
|
from utils.cache.yaml import dump_yaml
|
|
|
|
|
|
class TestGraphLogic(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp_dir = tempfile.mkdtemp()
|
|
self.role_name = "role_a"
|
|
self.role_path = os.path.join(self.temp_dir, self.role_name)
|
|
os.makedirs(os.path.join(self.role_path, "meta"))
|
|
os.makedirs(os.path.join(self.role_path, "tasks"))
|
|
|
|
# Write meta/main.yml
|
|
dump_yaml(
|
|
os.path.join(self.role_path, "meta", "main.yml"),
|
|
{
|
|
"galaxy_info": {"author": "tester", "run_after": []},
|
|
"dependencies": [],
|
|
},
|
|
)
|
|
|
|
# Write tasks/main.yml
|
|
dump_yaml(
|
|
os.path.join(self.role_path, "tasks", "main.yml"),
|
|
[
|
|
{"include_role": "some_other_role"},
|
|
{"import_role": {"name": "another_role"}},
|
|
],
|
|
)
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
def test_load_meta_returns_dict(self):
|
|
meta_path = graph.find_role_meta(self.temp_dir, self.role_name)
|
|
meta = graph.load_meta(meta_path)
|
|
self.assertIsInstance(meta, dict)
|
|
self.assertIn("galaxy_info", meta)
|
|
|
|
def test_load_tasks_include_role(self):
|
|
task_path = graph.find_role_tasks(self.temp_dir, self.role_name)
|
|
includes = graph.load_tasks(task_path, "include_role")
|
|
self.assertIn("some_other_role", includes)
|
|
|
|
def test_build_mappings_structure(self):
|
|
result = graph.build_mappings(self.role_name, self.temp_dir, max_depth=1)
|
|
self.assertIsInstance(result, dict)
|
|
for key in graph.ALL_KEYS:
|
|
self.assertIn(key, result)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|