mirror of https://github.com/Chizi123/Arch-autobuild-repo.git

Joel Grunbaum
2 days ago d68e2f2d0be6ac49e547775ece8655a720f6292f
tests/integration_test.py
@@ -47,13 +47,27 @@
class IntegrationTest:
    """Integration test runner."""
    def __init__(self, keep_temp: bool = False):
    def __init__(self, keep_temp: bool = False, use_cli: bool = False):
        self.keep_temp = keep_temp
        self.use_cli = use_cli
        self.temp_dir: Path | None = None
        self.config: Config | None = None
        self.config_path: Path | None = None
        self.passed = 0
        self.failed = 0
    def _run_cli(self, command: str, *args: str) -> subprocess.CompletedProcess:
        """Run archbuild CLI command via subprocess."""
        if not self.config_path:
            raise RuntimeError("Config path not set")
        env = os.environ.copy()
        # Add src to PYTHONPATH so the CLI can find the package
        env["PYTHONPATH"] = str(Path(__file__).parent.parent / "src")
        cmd = [sys.executable, "-m", "archbuild.cli", "-c", str(self.config_path), command] + list(args)
        return subprocess.run(cmd, capture_output=True, text=True, env=env)
    def setup(self) -> None:
        """Set up temporary test environment."""
        console.print("\n[bold blue]═══ Setting up test environment ═══[/]")
@@ -122,6 +136,11 @@
            },
        )
        # Save config to disk for CLI to use
        from archbuild.config import save_config
        self.config_path = self.temp_dir / "config.yaml"
        save_config(self.config, self.config_path)
        setup_logging(self.config.log_level)
        console.print("  [green]✓[/] Configuration created")
@@ -149,6 +168,10 @@
        """Test repository initialization."""
        console.print("\n[bold blue]═══ Test: Repository Initialization ═══[/]")
        if self.use_cli:
            result = self._run_cli("init")
            self.check(result.returncode == 0, "CLI: init command success")
        else:
        repo = RepoManager(self.config)
        repo.ensure_repo_exists()
@@ -195,6 +218,24 @@
        results: dict[str, BuildStatus] = {}
        if self.use_cli:
            for pkg_name in packages:
                console.print(f"\n  Building {pkg_name} via CLI...")
                result = self._run_cli("build", pkg_name, "-f")
                if result.returncode == 0:
                    results[pkg_name] = BuildStatus.SUCCESS
                    self.check(True, f"CLI: Built {pkg_name} successfully")
                    # Check for created artifacts
                    pkg_dir = self.config.repository.build_dir / pkg_name
                    artifacts = list(pkg_dir.glob("*.pkg.tar.*"))
                    artifacts = [a for a in artifacts if not a.name.endswith(".sig")]
                    self.check(len(artifacts) > 0, f"  Created {len(artifacts)} artifact(s)")
                else:
                    results[pkg_name] = BuildStatus.FAILED
                    self.check(False, f"CLI: Failed to build {pkg_name}\nError: {result.stderr}")
        else:
        async with AURClient() as aur:
            async with Builder(self.config, aur) as builder:
                for pkg_name in packages:
@@ -219,8 +260,12 @@
        """Test adding packages to repository."""
        console.print("\n[bold blue]═══ Test: Repository Management ═══[/]")
        if self.use_cli:
            # In CLI mode, 'add' or 'build' already adds to repo, but we can test 'remake'
            result = self._run_cli("remake")
            self.check(result.returncode == 0, "CLI: remake command success")
        else:
        repo = RepoManager(self.config)
        async with AURClient() as aur:
            async with Builder(self.config, aur) as builder:
                for pkg_name in packages:
@@ -245,6 +290,7 @@
                        self.check(success, f"Added {pkg_name} to repository")
        # List packages in repo
        repo = RepoManager(self.config)
        pkg_list = repo.list_packages()
        self.check(len(pkg_list) > 0, f"Repository contains {len(pkg_list)} package(s)")
@@ -255,6 +301,10 @@
        """Test repository database integrity."""
        console.print("\n[bold blue]═══ Test: Database Integrity ═══[/]")
        if self.use_cli:
            # We already tested remake, let's just check the DB file
            pass
        db_path = self.config.repository.path / f"{self.config.repository.name}.db.tar.zst"
        
        self.check(db_path.exists(), f"Database file exists: {db_path.name}")
@@ -285,15 +335,19 @@
        """Test package cleanup functionality."""
        console.print("\n[bold blue]═══ Test: Cleanup ═══[/]")
        if self.use_cli:
            result = self._run_cli("cleanup")
            self.check(result.returncode == 0, "CLI: cleanup command success")
        else:
        repo = RepoManager(self.config)
        removed = repo.cleanup()
        self.check(True, f"Cleanup removed {removed} old version(s)")
    async def run(self) -> bool:
        """Run all integration tests."""
        mode_str = " (CLI Mode)" if self.use_cli else " (Python Mode)"
        console.print("[bold magenta]╔════════════════════════════════════════════╗[/]")
        console.print("[bold magenta]║     Archbuild Integration Test Suite       ║[/]")
        console.print(f"[bold magenta]║     Archbuild Integration Test Suite{mode_str:<10}║[/]")
        console.print("[bold magenta]╚════════════════════════════════════════════╝[/]")
        try:
@@ -346,6 +400,7 @@
async def main() -> int:
    """Main entry point."""
    keep_temp = "--keep-temp" in sys.argv
    use_cli = "--use-cli" in sys.argv
    # Check if running on Arch Linux
    if not Path("/etc/arch-release").exists():
@@ -359,7 +414,7 @@
            console.print(f"[red]Required tool not found: {tool}[/]")
            return 1
    test = IntegrationTest(keep_temp=keep_temp)
    test = IntegrationTest(keep_temp=keep_temp, use_cli=use_cli)
    success = await test.run()
    return 0 if success else 1