Files
computer-playbook/tests/lint/filesystem/test_yaml_syntax.py
kevinveenbirkenbachandClaude Opus 4.8 2ac6c0878d fix(yaml): tolerate ansible-vault tags in the shared loader
The cached YAML loader crashed on the !vault tag with a ConstructorError,
so any lint test that walks the tree and parses every .yml (yaml-syntax,
role/group-vars usage, ...) failed as soon as a vault-encrypted inventory
was present. Register a !vault constructor on SafeLoader that loads the
ciphertext as a plain scalar, mirroring the existing !override/!reset
handling.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-21 12:05:40 +02:00

34 lines
1023 B
Python

import unittest
import yaml
from utils.cache.files import iter_project_files, read_text
from utils.cache.yaml import load_yaml_str
class TestYamlSyntax(unittest.TestCase):
def test_all_yml_files_are_valid_yaml(self):
"""
Walk the entire repository, find all *.yml files and try to parse them
through utils.cache.yaml. Fail the test if any file contains invalid YAML.
"""
invalid = []
for full in iter_project_files(extensions=(".yml",)):
try:
load_yaml_str(read_text(full))
except yaml.YAMLError as e:
invalid.append((full, str(e)))
except Exception as e:
invalid.append((full, f"Unexpected error: {e}"))
if invalid:
msg_lines = [f"{path}: {err.splitlines()[0]}" for path, err in invalid]
self.fail(
"Found invalid YAML in the following files:\n" + "\n".join(msg_lines)
)
if __name__ == "__main__":
unittest.main()