Validated use of correct applications in group_names

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-11 01:53:38 +02:00
parent d43fdc63ea
commit 80d26ca068
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E
3 changed files with 47 additions and 2 deletions

View File

@ -8,7 +8,6 @@
label: "{{ item }}-roles.yml"
- name: general host setup
when: ("personal_computers" in group_names)
include_role:
name: "{{ item }}"
loop:

View File

@ -1,6 +1,5 @@
---
- name: Setup server base
when: ("servers" in group_names)
include_role:
name: "{{ item }}"
loop:

View File

@ -0,0 +1,47 @@
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 your CLI code
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
sys.path.insert(0, ROOT)
class TestGroupApplications(unittest.TestCase):
# regex to capture any literal check in group_names: 'name' in/not in group_names
GROUP_CHECK_RE = re.compile(r"['\"](?P<name>[^'\"]+)['\"]\s*(?:in|not in)\s*group_names")
def test_group_name_checks_use_valid_application_ids(self):
"""
Ensures that any string checked against group_names corresponds to a valid application ID.
"""
valid_apps = find_application_ids()
# walk the entire project tree
for dirpath, _, filenames in os.walk(ROOT):
for filename in filenames:
if not filename.lower().endswith(('.yml', '.yaml')):
continue
filepath = os.path.join(dirpath, filename)
try:
with open(filepath, 'r', encoding='utf-8') as f:
text = f.read()
except Exception:
continue
# find all group_names checks in the file
for match in self.GROUP_CHECK_RE.finditer(text):
name = match.group('name')
# the checked name must be one of the valid application IDs
self.assertIn(
name,
valid_apps,
msg=(
f"{filepath}: group_names check uses '{name}', "
f"which is not a known application ID {valid_apps}"
)
)
if __name__ == '__main__':
unittest.main()