Files
RedBear-OS/local/scripts/tests/test_repair_cook.py
T
kellito ae749ffb23 build: ship build-system hardening arc (5 of 10 improvements)
The v6.0 build-system hardening arc lands 5 of the 10 improvements
proposed in local/docs/BUILD-SYSTEM-IMPROVEMENTS.md. All scripts
have unit tests (62 -> 86, all pass in <1s) and the new 'lint-recipe'
Gitea Actions job runs on every PR.

Per-recipe audit & lint scripts (catch R1/R2 violations BEFORE cook):
  * audit-patch-idempotency.py  — verifies external patches in
    local/patches/ still apply against the upstream pinned rev.
    Caught 1 real bug on first run: libdrm/02-redox-dispatch.patch
    hunk at xf86drm.c:321 no longer matches libdrm-2.4.125.
  * audit-kf6-deps.py           — fetches upstream, scans for
    find_package(KF6Xxx REQUIRED), compares to recipe deps. Catches
    missing + dead dependencies in every kf6-* and qt* recipe.
  * classify-cook-failure.py    — 17-rule cook-failure classifier.
    10-30s diagnosis vs 5-10min manual. exit code is intentionally
    inverted (0=novel failure, 1=known fix) for CI signal.
  * lint-recipe.py              — 7-rule recipe lint: R1-NO-PATCH-FILE,
    R1-PATH-SOURCE, R2-INLINE-SED, R2-PATCHES-DIR-UNUSED,
    NO-LEGACY-MAKE, R1-LEGACY-APPLY-PATCHES, DEP-NOT-FOUND.
    1.1s for 171 recipes (down from 60s+ in v1 via recipe-index
    precomputation). Strict mode promotes warnings to errors.

Build-system convenience:
  * repair-cook.sh              — incremental-build optimizer.
    Equivalent to 'repo cook <pkg>' but with a fast-path that
    skips configure when CMakeCache.txt is newer than source AND
    external patches haven't changed. 30-60s vs 5-10min on KF6
    recipes. make repair.<pkg> / make clean-repair.<pkg> targets.
  * migrate-kf6-seds-to-patches.sh — migration skeleton for
    converting 56 inline 'sed -i' chains across the KF6 recipes
    to durable external patches in local/patches/<name>/.

Gitea Actions (host-execution, no Docker):
  * .gitea/workflows/build-system.yml — 8-job pipeline:
    unit-tests, lint-offline, lint-network (nightly),
    lint-recipe (NEW), lint-docs, build-mini, build-full,
    smoke (QEMU boot).
  * .gitea/RUNNER-SETUP.md — one-time Manjaro/Arch host setup.

Build script hardening:
  * build-redbear.sh            — when a low-level source (relibc,
    kernel, base, bootloader, installer) is newer than its pkgar,
    clean build/ and sysroot/ across all recipes too. Low-level
    package changes leave autotools packages (pcre2, gettext,
    libiconv, ...) with stale configure/libtool scripts referencing
    the old runtime, causing 'libtool version mismatch' and
    'not a valid libtool object' errors. Cleaning forces
    re-configuration; stage/ and source/ are preserved so the
    cookbook skips unchanged packages that don't use autotools.
  * Makefile                    — wire lint-cook-failure,
    lint-cook-failure-explain, lint-recipe, lint-recipe.%,
    lint-recipe.strict, lint-recipe.%.strict, repair.%,
    clean-repair.%, test-lint-scripts[-quiet]. Replace the
    legacy 'validate-patches' target with a deprecation notice
    pointing at validate-sources.

Documentation:
  * BUILD-SYSTEM-IMPROVEMENTS.md   — mark #2 and #5 DONE; full
    implementation notes; updated Make-targets table.
  * BUILD-SYSTEM-V6-HARDENING-POSTMORTEM.md (NEW) — 226-line durable
    record of the 8-session arc: 32 findings categorized, 5 P0
    audit-script bugs fixed, 6 over-broad multi-pattern rules
    discovered + fixed, test coverage 86/86 in <1s, 7/10
    improvements DONE.
  * SCRIPT-BEHAVIOR-MATRIX.md   — apply-patches.sh row marked
    LEGACY/ARCHIVED; build-redbear.sh row no longer claims to
    call it.
  * boot-logs/README.md (NEW)   — frozen-evidence policy:
    'do not edit' rule for REDBEAR-FULL-BOOT-*-RESULTS.md files.
  * libdrm/02-redox-dispatch.patch.README (NEW) — 8-step regen
    procedure for the broken hunk.

Cleanup:
  * local/cache/README.md deleted (1-line placeholder).
  * legacy 'make validate-patches' target removed.

Per build-system improvement #5: lint-recipe.py's first run on
the live tree surfaced 1 broken-patch reference (redbear-sessiond),
1 dangling cookbook_apply_patches call (tc), 19 sed -i calls in
sddm (warning — cookbook_apply_patches present, drop-x11.py
migration in progress), 4 sed -i calls in qt6-wayland-smoke
(uncovers the same bug class the libwayland fix prevented).
2026-06-12 13:37:39 +03:00

135 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""Smoke tests for repair-cook.sh.
Run with:
python3 -m unittest local/scripts/tests/test_repair_cook.py
"""
import os
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
SCRIPTS_DIR = Path(__file__).resolve().parent.parent
REPAIR_COOK = SCRIPTS_DIR / "repair-cook.sh"
class TestRepairCook(unittest.TestCase):
"""Verify the fast/slow path logic of repair-cook.sh."""
def setUp(self):
"""Create a synthetic recipe tree with a fake build/ dir."""
self.tmp = tempfile.mkdtemp(prefix="repair-cook-test-")
self.recipe = Path(self.tmp) / "local" / "recipes" / "kde" / "kf6-fake"
self.recipe.mkdir(parents=True)
(self.recipe / "source").mkdir()
# Fake source file
(self.recipe / "source" / "main.c").write_text("int main() { return 0; }")
# Fake CMakeCache.txt (fresh) — placed at the cookbook's
# canonical build path: target/<target>/build/CMakeCache.txt
# (per src/cook/cook_build.rs:357: `get_sub_target_dir(target_dir, "build")`)
self.build_dir = (
self.recipe / "target" / "x86_64-unknown-redox" / "build"
)
self.build_dir.mkdir(parents=True)
self.cmake_cache = self.build_dir / "CMakeCache.txt"
self.cmake_cache.write_text("# fake cache\n")
# Patches dir (parent must be local/patches)
self.patches_dir = (
Path(self.tmp) / "local" / "patches" / "kf6-fake"
)
self.patches_dir.mkdir(parents=True)
def tearDown(self):
import shutil
shutil.rmtree(self.tmp, ignore_errors=True)
def _run(self, *args, env_extra=None):
env = os.environ.copy()
env["REPAIR_DRY_RUN"] = "1"
if env_extra:
env.update(env_extra)
return subprocess.run(
[str(REPAIR_COOK), str(self.recipe), *args],
capture_output=True, text=True, env=env,
)
def test_slow_path_when_no_build_dir(self):
"""With no build/ yet, must take the slow path."""
import shutil
shutil.rmtree(self.build_dir)
# Also remove the target/ parent so discovery finds nothing
shutil.rmtree(self.build_dir.parent)
rc = self._run()
self.assertIn("slow path", rc.stdout)
def test_slow_path_when_source_is_newer_than_cache(self):
"""When source files are newer than CMakeCache.txt, take slow path."""
# Touch source files to be newer than CMakeCache.txt
(self.recipe / "source" / "main.c").write_text("/* updated */\n")
rc = self._run()
self.assertIn("slow path", rc.stdout)
def test_fast_path_when_cache_is_fresh(self):
"""When CMakeCache.txt is newer than source/, take fast path.
The fast path invokes `repo cook` with --clean-build omitted,
so the cookbook skips configure + compile and only re-runs
the install/stage/package pipeline. We verify the wrapper
signals 'fast path' correctly.
"""
# Ensure source is older than cache (the default state)
cache_mtime = self.cmake_cache.stat().st_mtime
src_mtime = (self.recipe / "source" / "main.c").stat().st_mtime
self.assertLess(src_mtime, cache_mtime,
"precondition: source must be older than cache")
rc = self._run(env_extra={"REPAIR_VERBOSE": "1"})
# The fast path output mentions 'fast path' (dry-run prints
# "Would run: ... (fast path)"). Slow path prints "(slow path)".
self.assertIn("fast path", rc.stdout,
f"expected fast path, got: stdout={rc.stdout!r} "
f"stderr={rc.stderr!r}")
def test_slow_path_when_patches_are_newer(self):
"""When local/patches/<name>/ has newer .patch files, slow path."""
# Create a patch file with a future mtime
patch = self.patches_dir / "01-test.patch"
patch.write_text("# test patch\n")
# Touch it to be newer than CMakeCache.txt
import os as os_mod
future = self.cmake_cache.stat().st_mtime + 60
os_mod.utime(patch, (future, future))
rc = self._run(env_extra={"REPAIR_VERBOSE": "1"})
# The verbose flag should print why fast path was rejected
# (patches_are_newer=1). Check stderr for the rejection msg.
self.assertIn("patches_are_newer=1", rc.stderr,
f"expected patches_are_newer=1 in stderr, got: "
f"stderr={rc.stderr!r}")
self.assertIn("slow path", rc.stdout)
def test_clean_build_flag_forces_slow_path(self):
"""--clean-build always takes the slow path, even on a fresh build."""
rc = self._run("--clean-build")
self.assertIn("slow path", rc.stdout)
def test_repair_force_env_forces_slow_path(self):
"""REPAIR_FORCE=1 always takes the slow path."""
rc = self._run(env_extra={"REPAIR_FORCE": "1"})
self.assertIn("slow path", rc.stdout)
def test_recipe_path_must_be_provided(self):
"""Missing recipe arg → non-zero exit with usage message."""
rc = subprocess.run(
[str(REPAIR_COOK)],
capture_output=True, text=True,
)
self.assertNotEqual(rc.returncode, 0)
self.assertIn("usage", rc.stderr)
if __name__ == "__main__":
unittest.main()