Files
computer-playbook/tests/integration/roles/applications/id/test_deprecation.py
kevinveenbirkenbachandClaude Opus 4.7 f6c8c85b1b test(lint): role-file-spot guard + 139-file mapping migration
Add tests/lint/repository/test_role_file_spot.py: AST-walks every .py
file under the production trees and tests/, flags any reference to a
role file (standalone literal, f-string segment, pathlib `/` chain,
`+` concat chain, os.path.join / Path.joinpath / posixpath.join /
ntpath.join call) whose value or joined trailing operands match a
ROLE_FILE_* constant in utils.roles.mapping. The flag set is
auto-discovered from the module so adding a new constant extends
coverage without further edits. ROLE_FILE_README is exempt as too
generic. Per-line `# nocheck: role-file-spot` (same-or-above) is the
documented opt-out, catalog row added in
docs/contributing/actions/testing/suppression.md.

Migrate 139 .py files (377 occurrences) to use the constants:

- 135 files via direct constant import (`from utils.roles.mapping
  import ROLE_FILE_*`), with the symbol used in pathlib chains, in
  os.path.join / Path.joinpath calls, in f-string interpolation, and
  in glob patterns.
- 4 files via `# nocheck: role-file-spot` where the literal lives in
  argparse help text or in an allowlist tuple meant to declare the
  literal path.

Adjust utils/roles/dependency_resolver.py to use lazy-formatting
positional args in the logger.exception call (G003-clean) instead of
string concatenation against the constant.

Drop a redundant `if cond: return True; return False` guard in the
new test for `return cond` (SIM103-clean).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 13:21:22 +02:00

50 lines
1.6 KiB
Python

import os
import unittest
from pathlib import Path
from utils.cache.yaml import load_yaml_any
from utils.roles.mapping import ROLE_FILE_VARS_MAIN
from . import PROJECT_ROOT
ROLES_DIR = str(PROJECT_ROOT / "roles")
class TestApplicationIdDeprecation(unittest.TestCase):
def test_application_id_matches_role_name(self):
"""
Deprecation: application_id in vars/main.yml must match the role name.
This test fails if any role violates this rule, listing all violations.
"""
errors = []
for role in os.listdir(ROLES_DIR):
role_path = str(Path(ROLES_DIR) / role)
vars_main_yml = str(Path(role_path) / ROLE_FILE_VARS_MAIN)
if not Path(vars_main_yml).is_file():
continue
try:
data = load_yaml_any(vars_main_yml)
except Exception as e:
errors.append(f"Could not parse {vars_main_yml}: {e}")
continue
if not isinstance(data, dict):
continue
app_id = data.get("application_id")
if app_id is not None and app_id != role:
errors.append(
f"[DEPRECATION] application_id '{app_id}' in {vars_main_yml} "
f"does not match its role directory '{role}'."
)
if errors:
self.fail(
"application_id mismatch found in one or more roles:\n\n"
+ "\n".join(errors)
+ "\n\nPlease update 'application_id' to match the role name for future compatibility."
)
if __name__ == "__main__":
unittest.main()