Files
computer-playbook/tests/lint/filesystem/test_scripts_folder_files.py
kevinveenbirkenbachandClaude Opus 4.7 7dc6d6e491 chore(ruff): enable TC + N rules
TC (flake8-type-checking): typing-only imports moved into
``if TYPE_CHECKING:`` blocks across 8 source files via auto-fix
(117 hits). Reduces runtime import surface and rules out circular
imports for type-only deps. Project-wide ``from __future__ import
annotations`` (FA, already enabled) keeps annotations as strings,
so the moved imports never need to load at runtime.

N (pep8-naming): 32 hits handled.

Renames (semantic + lint):
- ``isSmallerThenMaximumBackupSize`` -> ``is_larger_than_maximum_backup_size``
  (snake_case + fixes both the typo "Then -> Than" and the
  semantic mismatch: the function returns ``current > maximum``)
- ``isDirectoryDeletable`` / ``deleteVersion`` /
  ``getAmountOfIteration`` / ``deleteIteration`` -> snake_case
- 8 test functions with ALL-CAPS env-var fragments
  (``test_..._DOMAIN_PRIMARY_...`` etc.) -> snake_case
- Mock alias ``VH`` -> ``vh`` in test_host_vars.py

Exception class renames (``N818`` Error suffix):
- ``TimeLimitExceededException`` -> ``TimeLimitExceededError``
- ``AttemptException`` -> ``AttemptError``
- ``PathNotFound`` -> ``PathNotFoundError`` (propagated across
  4 callers under tests/integration/lookups/config/)
- ``_ExitJson`` / ``_FailJson`` -> ``*_Error``

Refactor:
- ``METADATA = {...}`` local constant in two functions of
  ``plugins/filter/invokable_paths.py`` lifted to module-level
  ``_METADATA_KEYS``.
- ``from typing import TYPE_CHECKING`` in
  ``tasks/utils/migrate_meta_layout/__main__.py`` moved above
  the ``sys.path.insert`` hack to satisfy E402 (collateral from
  the TC auto-fix that landed it after the path mutation).

Per-file ignores in pyproject.toml for the legitimate cases the
rule cannot accept:
- ``pull-specific-host.py`` -> ``N999`` (Ansible-invoked role-level
  script, hyphenated filename pinned by the surrounding stack).
- 4 matrix-deploy files -> ``N806`` for the ``include_R`` /
  ``raw_include_R`` round-index notation; the uppercase ``R``
  here mirrors the formula in the docstring rather than naming
  a constant.
- ``plugins/filter/value_generator.py`` -> ``N806`` for the
  lazy-loaded class binding ``ValueGenerator =
  _value_generator_cls()``, which retains the imported class
  identifier per PEP 8.

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

60 lines
1.6 KiB
Python

"""Lint check: tracked files under scripts/ must be shell scripts.
README.md files are ignored because they are documentation-only exceptions.
"""
from __future__ import annotations
import subprocess
import unittest
from typing import TYPE_CHECKING
from . import PROJECT_ROOT
if TYPE_CHECKING:
from pathlib import Path
def tracked_files(root: Path) -> list[Path]:
try:
output = subprocess.check_output(
["git", "-C", str(root), "ls-files", "-z"],
stderr=subprocess.STDOUT,
)
rel_paths = [path for path in output.decode("utf-8").split("\0") if path]
return [root / rel_path for rel_path in rel_paths]
except Exception:
return [
path
for path in root.rglob("*") # nocheck: project-walk
if path.is_file()
and ".git" not in path.parts
and "__pycache__" not in path.parts
]
class TestScriptsFolderFiles(unittest.TestCase):
def test_scripts_contains_only_sh_files(self) -> None:
root = PROJECT_ROOT
scripts_root = root / "scripts"
violations = [
path.relative_to(root).as_posix()
for path in tracked_files(root)
if path.is_relative_to(scripts_root)
and path.name != "README.md"
and path.suffix != ".sh"
]
self.assertEqual(
violations,
[],
"Tracked files under scripts/ must end with .sh "
"(README.md is allowed).\nFound:\n"
+ "\n".join(f" {path}" for path in sorted(violations)),
)
if __name__ == "__main__":
unittest.main()