Files
computer-playbook/tests/unit/cli/meta/test_tree.py
T
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

44 lines
1.3 KiB
Python

import shutil
import tempfile
import unittest
from pathlib import Path
from cli.build import tree
from utils.roles.mapping import ROLE_FILE_META_MAIN
class TestTreeMain(unittest.TestCase):
def setUp(self):
# Create a temporary roles directory with a fake role
self.temp_dir = tempfile.mkdtemp()
self.role_name = "testrole"
self.role_path = str(Path(self.temp_dir) / self.role_name)
Path(str(Path(self.role_path) / "meta")).mkdir(parents=True)
meta_path = str(Path(self.role_path) / ROLE_FILE_META_MAIN)
with Path(meta_path).open("w") as f:
f.write("galaxy_info:\n author: test\n run_after: []\ndependencies: []\n")
def tearDown(self):
shutil.rmtree(self.temp_dir)
def test_find_roles(self):
roles = list(tree.find_roles(self.temp_dir))
self.assertEqual(len(roles), 1)
self.assertEqual(roles[0][0], self.role_name)
def test_main_execution_does_not_raise(self):
# Mocking sys.argv and running main should not raise
import sys
old_argv = sys.argv
sys.argv = ["tree/__main__.py", "-d", self.temp_dir, "-p"]
try:
tree.main()
finally:
sys.argv = old_argv
if __name__ == "__main__":
unittest.main()