#!/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//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// 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()