mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Solved wrong namings
This commit is contained in:
67
tests/integration/test_valid_applications.py
Normal file
67
tests/integration/test_valid_applications.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import unittest
|
||||
from cli.meta.applications import find_application_ids
|
||||
|
||||
# ensure project root is on PYTHONPATH so we can import the CLI code
|
||||
# project root is two levels up from this file (tests/integration -> project root)
|
||||
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
|
||||
sys.path.insert(0, ROOT)
|
||||
|
||||
class TestValidApplicationUsage(unittest.TestCase):
|
||||
"""
|
||||
Integration test to ensure that only valid application IDs
|
||||
are used in all .yml, .yaml, .yml.j2, .yaml.j2, and .py files.
|
||||
Methods like applications.items() can be whitelisted and ignored.
|
||||
"""
|
||||
# regex patterns to capture applications['name'], applications.get('name'), and applications.name
|
||||
APPLICATION_SUBSCRIPT_RE = re.compile(r"applications\[['\"](?P<name>[^'\"]+)['\"]\]")
|
||||
APPLICATION_GET_RE = re.compile(r"applications\.get\(\s*['\"](?P<name>[^'\"]+)['\"]")
|
||||
APPLICATION_ATTR_RE = re.compile(r"applications\.(?P<name>[A-Za-z_]\w*)")
|
||||
|
||||
# methods and exceptions that should not be validated as application IDs
|
||||
WHITELIST = {'items', 'yml', 'get'}
|
||||
|
||||
def test_application_references_use_valid_ids(self):
|
||||
valid_apps = find_application_ids()
|
||||
|
||||
tests_dir = os.path.join(ROOT, 'tests')
|
||||
for dirpath, _, filenames in os.walk(ROOT):
|
||||
# skip the tests/ directory and all its subdirectories
|
||||
if dirpath == tests_dir or dirpath.startswith(tests_dir + os.sep):
|
||||
continue
|
||||
|
||||
for filename in filenames:
|
||||
if not filename.lower().endswith(('.yml', '.yaml', '.yml.j2', '.yaml.j2', '.py')):
|
||||
continue
|
||||
filepath = os.path.join(dirpath, filename)
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
except Exception:
|
||||
# skip files that cannot be opened
|
||||
continue
|
||||
|
||||
for pattern in (
|
||||
self.APPLICATION_SUBSCRIPT_RE,
|
||||
self.APPLICATION_GET_RE,
|
||||
self.APPLICATION_ATTR_RE,
|
||||
):
|
||||
for match in pattern.finditer(content):
|
||||
name = match.group('name')
|
||||
# skip whitelisted methods/exceptions
|
||||
if name in self.WHITELIST:
|
||||
continue
|
||||
# each found reference must be in valid_apps
|
||||
self.assertIn(
|
||||
name,
|
||||
valid_apps,
|
||||
msg=(
|
||||
f"{filepath}: reference to application '{name}' "
|
||||
f"is invalid. Known IDs: {sorted(valid_apps)}"
|
||||
)
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in New Issue
Block a user