Update RunOnceSchemaTest to skip files with deactivated run_once variables via comment https://chatgpt.com/share/6899d297-4bec-800f-a748-6816398d8c7e

This commit is contained in:
Kevin Veen-Birkenbach 2025-08-11 13:23:20 +02:00
parent 1ba50397db
commit 0e59d35129
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E

View File

@ -7,43 +7,47 @@ import unittest
class RunOnceSchemaTest(unittest.TestCase): class RunOnceSchemaTest(unittest.TestCase):
""" """
Ensure that any occurrence of 'run_once_' in roles/*/tasks/main.yml Ensure that any occurrence of 'run_once_' in roles/*/tasks/main.yml
matches the pattern 'run_once_' + (role_name with '-' replaced by '_'). matches the pattern 'run_once_' + (role_name with '-' replaced by '_'),
unless the file explicitly deactivates its own run_once var via:
# run_once_<role_suffix>: deactivated
""" """
RUN_ONCE_PATTERN = re.compile(r"run_once_([A-Za-z0-9_]+)") RUN_ONCE_PATTERN = re.compile(r"run_once_([A-Za-z0-9_]+)")
# Will be compiled per-file with the expected suffix:
# r"^\s*#\s*run_once_<suffix>\s*:\s*deactivated\s*$" (flags=MULTILINE|IGNORECASE)
def test_run_once_suffix_matches_role(self): def test_run_once_suffix_matches_role(self):
# Determine project root: two levels up from this test file (tests/integration -> tests -> project)
project_root = os.path.abspath( project_root = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', '..') os.path.join(os.path.dirname(__file__), '..', '..')
) )
violations = [] violations = []
# Find all roles/*/tasks/main.yml files
pattern = os.path.join(project_root, 'roles', '*', 'tasks', 'main.yml') pattern = os.path.join(project_root, 'roles', '*', 'tasks', 'main.yml')
for filepath in glob.glob(pattern): for filepath in glob.glob(pattern):
# Extract role name from path
parts = os.path.normpath(filepath).split(os.sep) parts = os.path.normpath(filepath).split(os.sep)
try: try:
role_index = parts.index('roles') + 1 role_index = parts.index('roles') + 1
role_name = parts[role_index] role_name = parts[role_index]
except ValueError: except ValueError:
continue # skip unexpected path continue
# Compute expected suffix
expected_suffix = role_name.lower().replace('-', '_') expected_suffix = role_name.lower().replace('-', '_')
# Read file content
with open(filepath, 'r', encoding='utf-8') as f: with open(filepath, 'r', encoding='utf-8') as f:
content = f.read() content = f.read()
# Find all run_once_ suffixes # Skip this file entirely if it explicitly deactivates its own run_once var
matches = self.RUN_ONCE_PATTERN.findall(content) deactivated_re = re.compile(
if not matches: rf"^\s*#\s*run_once_{re.escape(expected_suffix)}\s*:\s*deactivated\s*$",
# No run_once_ in this file, skip flags=re.IGNORECASE | re.MULTILINE,
)
if deactivated_re.search(content):
continue
matches = self.RUN_ONCE_PATTERN.findall(content)
if not matches:
continue continue
# Check each occurrence
for suffix in matches: for suffix in matches:
if suffix != expected_suffix: if suffix != expected_suffix:
violations.append( violations.append(