#!/usr/bin/env python3 """Verify strict POSIX Makefile compliance and POSIX /bin/sh syntax across all scripts.""" import os import re import subprocess import sys ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) def check_makefile() -> None: mk_path = os.path.join(ROOT, "Makefile") with open(mk_path, "r", encoding="utf-8") as f: lines = f.read().splitlines() assert any(l.strip() == ".POSIX:" for l in lines), "Makefile must begin with .POSIX:" # Check for GNU-only or BSD-only constructs forbidden = [ (r"^\s*(ifeq|ifneq|ifdef|ifndef|else|endif)\b", "GNU conditional"), (r"\$\(shell\b", "$(shell ...) GNU function"), (r"\$\(wildcard\b", "$(wildcard ...) GNU function"), (r"\$\(patsubst\b", "$(patsubst ...) GNU function"), (r"\$\(subst\b", "$(subst ...) GNU function"), (r":=", "non-POSIX immediate assignment :="), (r"\+=", "non-POSIX append assignment +="), (r"\?=", "non-POSIX conditional assignment ?="), (r"\$\^", "non-POSIX automatic variable $^"), (r"^[^#]*%[^:]*:", "GNU pattern rule %.o"), (r"^\s*include\b|^\s*-include\b", "non-POSIX include directive"), (r"^\s*export\b", "GNU export directive"), (r"\bmake\s+-C\b|\$\(MAKE\)\s+-C\b", "GNU make -C option"), ] for lineno, line in enumerate(lines, 1): stripped = line.strip() if stripped.startswith("#") or not stripped: continue for pat, desc in forbidden: assert not re.search(pat, line), f"Makefile:{lineno}: {desc}: {line}" # $< is only allowed in inference rules (e.g. .c.o:), not explicit target rules if "$<" in line: raise AssertionError(f"Makefile:{lineno}: avoid $< in explicit rules: {line}") # Verify Makefile parses cleanly under make -n (no syntax errors) res = subprocess.run(["make", "-n", "-f", mk_path], cwd=ROOT, capture_output=True, text=True) assert res.returncode == 0, f"make -n failed: {res.stderr}" def check_scripts() -> None: script_files = [ "scripts/detect-env.sh", "scripts/anode-apply-scale", "scripts/anode-apply-theme", "scripts/anode-sync-gtk-theme", "scripts/anode-sync-dunst-theme", "scripts/anode-session", "scripts/anode-replace", "scripts/anode-autostart", "scripts/anode-launcher", "data/env.sh", "src/dmenu/dmenu_path", "src/dmenu/dmenu_run", ] bad_patterns = [ (r"\[\[\s", "bash [[ conditional"), (r"\becho\s+-e\b", "non-portable echo -e"), (r"\blocal\s+", "non-POSIX local keyword"), (r"\bpidof\b", "Linux-only pidof"), (r"\bsed\s+-i\b", "non-POSIX sed -i"), (r"\bxargs\s+-r\b", "GNU xargs -r"), (r"\bcp\s+-a\b", "GNU cp -a"), (r"\bln\s+-sfn\b", "GNU ln -sfn"), (r"\bmktemp\s+-p\b", "GNU mktemp -p"), ] for rel in script_files: p = os.path.join(ROOT, rel) assert os.path.isfile(p), f"Missing script: {rel}" st = subprocess.run(["sh", "-n", p], capture_output=True, text=True) assert st.returncode == 0, f"sh -n failed on {rel}: {st.stderr}" with open(p, "r", encoding="utf-8") as f: content = f.read() for lineno, line in enumerate(content.splitlines(), 1): # Skip comments code = line.split("#", 1)[0] if not code.strip(): continue # Allow '#!/bin/sh' for pat, desc in bad_patterns: assert not re.search(pat, code), f"{rel}:{lineno}: {desc}: {line}" if __name__ == "__main__": check_makefile() check_scripts() print("test_posix_make: OK")