From 664457b739b9699faeffadc6d497fa976f20f4bb Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 21 May 2026 22:35:26 +0200 Subject: [PATCH] test(lint): verify every `make ` reference resolves MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Catches refactor drift across `.sh`, `Makefile` recursive calls, and GitHub workflows. Includes fixes for the 5 bugs the new test surfaced on first run: `make -C ROOT down`/`up` in the network stack refresh helper (missed during the compose-* rename) and four `make compose-deploymode=…` / `compose-deployapps=…` typos in 07/08/09_deploy_*.sh plus three docstring/echo references (missing space after `compose-deploy`, introduced by the deploy → compose-deploy sed-rename). --- .../system/network/docker/stack_refresh.sh | 4 +- .../deploy/local/deploy/apps/update/all.sh | 4 +- .../deploy/local/deploy/bundles/update.sh | 2 +- .../local/utils/reuse-kept-app-deploy.sh | 2 +- .../tests/environment/07_deploy_minimal.sh | 2 +- .../environment/08_deploy_performance.sh | 2 +- scripts/tests/environment/09_deploy_reuse.sh | 2 +- .../makefile/test_referenced_targets_exist.py | 167 ++++++++++++++++++ 8 files changed, 176 insertions(+), 9 deletions(-) create mode 100644 tests/lint/filesystem/makefile/test_referenced_targets_exist.py diff --git a/scripts/system/network/docker/stack_refresh.sh b/scripts/system/network/docker/stack_refresh.sh index 160f1c552c..6b89f88d9a 100755 --- a/scripts/system/network/docker/stack_refresh.sh +++ b/scripts/system/network/docker/stack_refresh.sh @@ -15,5 +15,5 @@ if [[ "${stack_was_running}" -ne 1 ]]; then fi echo "[stack-refresh] recreating running Infinito dev stack" -make -C "${REPO_ROOT}" down -make -C "${REPO_ROOT}" up +make -C "${REPO_ROOT}" compose-down +make -C "${REPO_ROOT}" compose-up diff --git a/scripts/tests/deploy/local/deploy/apps/update/all.sh b/scripts/tests/deploy/local/deploy/apps/update/all.sh index 9d5e4d6cc1..0e53466921 100755 --- a/scripts/tests/deploy/local/deploy/apps/update/all.sh +++ b/scripts/tests/deploy/local/deploy/apps/update/all.sh @@ -13,7 +13,7 @@ set -euo pipefail # INFINITO_DEBUG (default: false) # # Notes: -# - This does NOT create the inventory. Run make compose-deployapps= first. +# - This does NOT create the inventory. Run make compose-deploy apps= first. # - We recompute the app list to keep behavior deterministic with filters. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -45,7 +45,7 @@ pw_file="${inv_dir}/.password" if [[ ! -f "${inv_file}" ]]; then echo "ERROR: inventory not found: ${inv_file}" >&2 - echo "Run: make compose-deployapps=" >&2 + echo "Run: make compose-deploy apps=" >&2 exit 2 fi diff --git a/scripts/tests/deploy/local/deploy/bundles/update.sh b/scripts/tests/deploy/local/deploy/bundles/update.sh index b35135a71f..58b85701c0 100755 --- a/scripts/tests/deploy/local/deploy/bundles/update.sh +++ b/scripts/tests/deploy/local/deploy/bundles/update.sh @@ -4,7 +4,7 @@ set -euo pipefail # Update all apps cumulated from one or more inventory bundles (reuses inventory, no down/up, no purge). # # Usage: -# INFINITO_BUNDLES="education-suite,startup-essentials" make compose-deploymode=update +# INFINITO_BUNDLES="education-suite,startup-essentials" make compose-deploy mode=update # # Behavior: # - Aggregates and deduplicates all role groups declared in each bundle's diff --git a/scripts/tests/deploy/local/utils/reuse-kept-app-deploy.sh b/scripts/tests/deploy/local/utils/reuse-kept-app-deploy.sh index 13488f1634..f0e7f10fbe 100755 --- a/scripts/tests/deploy/local/utils/reuse-kept-app-deploy.sh +++ b/scripts/tests/deploy/local/utils/reuse-kept-app-deploy.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# In-container helper for `make compose-deploymode=update apps=…`. +# In-container helper for `make compose-deploy mode=update apps=…`. # # Called from the host wrapper at # scripts/tests/deploy/local/deploy/apps/update/selection.sh via `docker exec`, diff --git a/scripts/tests/environment/07_deploy_minimal.sh b/scripts/tests/environment/07_deploy_minimal.sh index cf42ee3f55..6a3a343736 100755 --- a/scripts/tests/environment/07_deploy_minimal.sh +++ b/scripts/tests/environment/07_deploy_minimal.sh @@ -12,7 +12,7 @@ echo "Snapshotting cache counters before the deploy." CACHE_BEFORE="$(cache_snapshot)" echo "Deploying dashboard with matomo disabled to verify INFINITO_SERVICES_DISABLED suppresses the shared service in the inventory." -make compose-deploymode=reinstall apps="${DASHBOARD_APP}" INFINITO_SERVICES_DISABLED="matomo" +make compose-deploy mode=reinstall apps="${DASHBOARD_APP}" INFINITO_SERVICES_DISABLED="matomo" inspect echo "Actively probing both caches to confirm pull-through works end-to-end." diff --git a/scripts/tests/environment/08_deploy_performance.sh b/scripts/tests/environment/08_deploy_performance.sh index 1095d120af..41db1623fa 100755 --- a/scripts/tests/environment/08_deploy_performance.sh +++ b/scripts/tests/environment/08_deploy_performance.sh @@ -7,7 +7,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${SCRIPT_DIR}/utils/common.sh" echo "Deploying matomo (full cycle: deploy + update pass) so it becomes reachable via its dedicated inventory entry." -make compose-deploymode=reinstall apps="${MATOMO_APP}" INFINITO_FULL_CYCLE=true +make compose-deploy mode=reinstall apps="${MATOMO_APP}" INFINITO_FULL_CYCLE=true inspect echo "Re-trusting the CA after the fresh deploy rebuilt the certificates." diff --git a/scripts/tests/environment/09_deploy_reuse.sh b/scripts/tests/environment/09_deploy_reuse.sh index d71af1ee01..e69f6c1985 100755 --- a/scripts/tests/environment/09_deploy_reuse.sh +++ b/scripts/tests/environment/09_deploy_reuse.sh @@ -7,5 +7,5 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${SCRIPT_DIR}/utils/common.sh" echo "Redeploying matomo (variant 1) while keeping inventory and packages to validate state reuse." -make compose-deploymode=update apps="${MATOMO_APP}" INFINITO_VARIANT=1 +make compose-deploy mode=update apps="${MATOMO_APP}" INFINITO_VARIANT=1 inspect diff --git a/tests/lint/filesystem/makefile/test_referenced_targets_exist.py b/tests/lint/filesystem/makefile/test_referenced_targets_exist.py new file mode 100644 index 0000000000..11928f707d --- /dev/null +++ b/tests/lint/filesystem/makefile/test_referenced_targets_exist.py @@ -0,0 +1,167 @@ +"""Every `make ` invocation in scripts, the Makefile, and CI +workflows must reference a target defined in the project Makefile. + +Catches refactoring drift such as renaming `up`/`down` to `compose-up`/ +`compose-down` without updating callers, and missing-space typos like +``make compose-deploymode=…`` (intended ``make compose-deploy mode=…``). + +Scope: shell scripts (`*.sh`), the root Makefile (`$(MAKE) …` recursive +calls), and GitHub workflow YAML. Whole-line comments and matches inside +single/double-quoted strings (``echo "make X"``) are skipped — they are +prose, not invocations. + +Suppress a line with a trailing ``# nocheck:make-target`` comment when the +match is intentional (e.g. invoking a target from a *different* Makefile via +``make -C ``).""" + +from __future__ import annotations + +import re +import unittest +from dataclasses import dataclass +from pathlib import Path + +from utils.cache.files import iter_project_files, read_text + +from . import PROJECT_ROOT + +_TARGET_DEF_RE = re.compile(r"^(?P[A-Za-z][A-Za-z0-9_-]*)\s*:(?!=)") +_PHONY_RE = re.compile(r"^\.PHONY\s*:\s*(?P.+?)\s*$") + +# Match a `make` invocation and capture the first lowercase kebab-case token +# after it (the candidate target). Skips an optional `-C ` argument. +# The preceding context must be a line start or a shell separator so we do not +# match the word "make" embedded in identifiers. +_MAKE_INVOCATION_RE = re.compile( + r""" + (?:^|[\s;&|`(]) # start-of-line or shell word boundary + make\b # the make binary (case-sensitive) + (?:\s+-C\s+\S+)? # optional -C + \s+ + (?P[a-z][a-z0-9_-]*) # first kebab-case token + """, + re.VERBOSE, +) + +# Match $(MAKE) / ${MAKE} (optionally quoted) recursive invocations inside the +# Makefile, capturing the candidate target the same way. +_RECURSIVE_MAKE_RE = re.compile( + r""" + "?\$[\(\{]MAKE[\)\}]"? # $(MAKE), ${MAKE}, "$(MAKE)", "${MAKE}" + (?:\s+-C\s+\S+)? + \s+ + (?P[a-z][a-z0-9_-]*) + """, + re.VERBOSE, +) + +_NOCHECK_MARKER = "nocheck:make-target" +_COMMENT_LINE_RE = re.compile(r"^\s*#") + + +@dataclass(frozen=True) +class _Reference: + path: str + line_no: int + target: str + snippet: str + + +def _inside_string_literal(line: str, position: int) -> bool: + """True if *position* in *line* sits inside an unclosed ``'`` or ``"`` + quote opened earlier on the same line. Used to skip ``echo "… make X …"`` + occurrences where ``make`` is part of a literal string, not an invocation.""" + prefix = line[:position] + return prefix.count('"') % 2 == 1 or prefix.count("'") % 2 == 1 + + +def _project_targets(makefile: Path) -> set[str]: + targets: set[str] = set() + for line in read_text(str(makefile)).splitlines(): + match = _TARGET_DEF_RE.match(line) + if match is not None: + targets.add(match.group("name")) + continue + phony = _PHONY_RE.match(line) + if phony is not None: + targets.update(phony.group("names").split()) + return targets + + +def _scan_line(line: str, regex: re.Pattern[str]) -> list[tuple[str, str]]: + if _NOCHECK_MARKER in line: + return [] + if _COMMENT_LINE_RE.match(line): + return [] + out: list[tuple[str, str]] = [] + for match in regex.finditer(line): + if _inside_string_literal(line, match.start("target")): + continue + out.append((match.group("target"), line.strip())) + return out + + +def _scan_file(path: Path, regex: re.Pattern[str]) -> list[_Reference]: + try: + content = read_text(str(path)) + except (OSError, UnicodeDecodeError): + return [] + rel = str(path.relative_to(PROJECT_ROOT)) + out: list[_Reference] = [] + for index, line in enumerate(content.splitlines(), start=1): + for target, snippet in _scan_line(line, regex): + out.append(_Reference(rel, index, target, snippet)) + return out + + +def _candidate_files() -> list[Path]: + paths: list[Path] = [PROJECT_ROOT / "Makefile"] + paths.extend(Path(raw) for raw in iter_project_files(extensions=(".sh",))) + workflows_dir = PROJECT_ROOT / ".github" / "workflows" + if workflows_dir.is_dir(): + paths.extend(sorted(workflows_dir.glob("*.yml"))) + paths.extend(sorted(workflows_dir.glob("*.yaml"))) + return paths + + +class TestMakeReferencesResolve(unittest.TestCase): + def setUp(self) -> None: + self.makefile = PROJECT_ROOT / "Makefile" + self.assertTrue(self.makefile.is_file(), "Makefile not found at project root") + self.targets = _project_targets(self.makefile) + self.assertTrue(self.targets, "no targets parsed from Makefile") + + def test_every_referenced_target_is_defined(self) -> None: + references: list[_Reference] = [] + for path in _candidate_files(): + regex = ( + _RECURSIVE_MAKE_RE if path.name == "Makefile" else _MAKE_INVOCATION_RE + ) + references.extend(_scan_file(path, regex)) + + unknown = [ref for ref in references if ref.target not in self.targets] + if not unknown: + return + + lines = [ + f"{len(unknown)} `make ` invocation(s) reference a " + "target that is not defined in the Makefile:", + "", + ] + lines.extend( + f" {ref.path}:{ref.line_no}: '{ref.target}' -> {ref.snippet}" + for ref in unknown + ) + lines.extend( + [ + "", + "Fix the typo / rename, or — if the reference is intentional " + "(e.g. `make -C `) — append " + f"`# {_NOCHECK_MARKER}` to suppress the check on that line.", + ] + ) + self.fail("\n".join(lines)) + + +if __name__ == "__main__": + unittest.main()