Files
anode/tests/test_posix_make.py
T

147 lines
6.4 KiB
Python
Raw Normal View History

#!/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-display",
"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}"
def test_optional_touchd_staged_install() -> None:
import tempfile
with tempfile.TemporaryDirectory() as tmp:
dest0 = os.path.join(tmp, "d0")
res0 = subprocess.run(
["make", "install", f"DESTDIR={dest0}", "PREFIX=/usr/local", "XSESSIONSDIR=/usr/share/xsessions"],
cwd=ROOT, capture_output=True, text=True,
)
assert res0.returncode == 0, f"default install failed: {res0.stderr}"
assert not os.path.exists(os.path.join(dest0, "usr", "local", "bin", "anode-touchd"))
assert os.path.isfile(os.path.join(dest0, "usr", "local", "bin", "anode-vkbd"))
assert os.path.isfile(os.path.join(dest0, "usr", "local", "bin", "anode-apply-display"))
assert os.path.isfile(os.path.join(dest0, "usr", "local", "share", "anode", "themes", "Anodized.xth"))
assert not os.path.exists(os.path.join(dest0, "usr", "local", "share", "anode", "themes", "AnodeDefault.xth"))
dest1 = os.path.join(tmp, "d1")
res1 = subprocess.run(
["make", "WITH_TOUCHD=1", "install", f"DESTDIR={dest1}", "PREFIX=/usr/local", "XSESSIONSDIR=/usr/share/xsessions"],
cwd=ROOT, capture_output=True, text=True,
)
assert res1.returncode == 0, f"WITH_TOUCHD=1 install failed: {res1.stderr}"
assert os.path.isfile(os.path.join(dest1, "usr", "local", "bin", "anode-touchd"))
assert os.path.isfile(os.path.join(dest1, "usr", "local", "bin", "anode-vkbd"))
hres = subprocess.run(["make", "help"], cwd=ROOT, capture_output=True, text=True)
assert hres.returncode == 0, f"make help failed: {hres.stderr}"
for kw in ("rebuild", "install-touch", "install-system", "WITH_TOUCHD"):
assert kw in hres.stdout, f"make help missing {kw!r}: {hres.stdout}"
dest2 = os.path.join(tmp, "d2")
res2 = subprocess.run(
["make", "install-touch", f"DESTDIR={dest2}", "PREFIX=/usr/local"],
cwd=ROOT, capture_output=True, text=True,
)
assert res2.returncode == 0, f"install-touch failed: {res2.stderr}"
assert os.path.isfile(os.path.join(dest2, "usr", "local", "bin", "anode-touchd"))
assert os.path.isfile(os.path.join(dest2, "usr", "local", "bin", "anode-vkbd"))
dest3 = os.path.join(tmp, "d3")
res3 = subprocess.run(
["make", "install-system", f"DESTDIR={dest3}", "PREFIX=/usr/local"],
cwd=ROOT, capture_output=True, text=True,
)
assert res3.returncode == 0, f"install-system failed: {res3.stderr}"
assert os.path.isfile(os.path.join(dest3, "usr", "share", "xsessions", "anode.desktop"))
assert not os.path.exists(os.path.join(dest3, "usr", "local", "bin", "anode-touchd"))
if __name__ == "__main__":
check_makefile()
check_scripts()
test_optional_touchd_staged_install()
print("test_posix_make: OK")