#!/usr/bin/env python3 """ Test script to verify XZ archive support in autusm. This script tests that .tar.xz archives can be properly detected and extracted. """ import os import sys import tempfile import tarfile from pathlib import Path # Add the src directory to the path so we can import autusm modules sys.path.insert(0, str(Path(__file__).parent / "src")) from autusm.extractor import ArchiveExtractor from autusm.generator import ScriptGenerator from autusm.models import PackageInfo, BuildSystem, BuildSystemType def create_test_xz_archive(archive_path: Path) -> None: """Create a test .tar.xz archive with some sample files. Args: archive_path: Path where to create the archive """ # Create a temporary directory with test files with tempfile.TemporaryDirectory() as temp_dir: temp_path = Path(temp_dir) test_dir = temp_path / "test_package" test_dir.mkdir() # Create some test files (test_dir / "README.md").write_text("# Test Package\nThis is a test package.") (test_dir / "configure").write_text("#!/bin/sh\necho 'Configuring...'") (test_dir / "Makefile").write_text("all:\n\techo 'Building...'") # Create the tar.xz archive with tarfile.open(archive_path, 'w:xz') as tar: tar.add(test_dir, arcname="test_package") def test_xz_detection(): """Test that .tar.xz archives are properly detected.""" print("Testing XZ archive detection...") extractor = ArchiveExtractor() # Test with .tar.xz extension assert extractor._detect_format(Path("test.tar.xz")) == ".tar.xz", "Failed to detect .tar.xz" # Test with .txz extension assert extractor._detect_format(Path("test.txz")) == ".txz", "Failed to detect .txz" print("✓ XZ archive detection test passed") def test_xz_extraction(): """Test that .tar.xz archives can be properly extracted.""" print("Testing XZ archive extraction...") extractor = ArchiveExtractor() with tempfile.TemporaryDirectory() as temp_dir: temp_path = Path(temp_dir) archive_path = temp_path / "test.tar.xz" extract_dir = temp_path / "extracted" # Create a test XZ archive create_test_xz_archive(archive_path) # Extract the archive extractor.extract(archive_path, extract_dir) # Verify extraction assert extract_dir.exists(), "Extraction directory was not created" assert (extract_dir / "README.md").exists(), "README.md was not extracted" assert (extract_dir / "configure").exists(), "configure was not extracted" assert (extract_dir / "Makefile").exists(), "Makefile was not extracted" # Verify content readme_content = (extract_dir / "README.md").read_text() assert "Test Package" in readme_content, "README content is incorrect" print("✓ XZ archive extraction test passed") def test_generator_xz_support(): """Test that the generator includes XZ support in acquire scripts.""" print("Testing generator XZ support...") generator = ScriptGenerator() package_info = PackageInfo(name="test_package", url="http://example.com/test.tar.xz") build_system = BuildSystem(type=BuildSystemType.AUTOTOOLS) # Generate the acquire script acquire_script = generator._common_acquire_template(package_info, build_system) # Verify that XZ is included in the detect_archive_type function assert "*.tar.xz|*.txz" in acquire_script, "XZ format not included in detect_archive_type" assert 'echo "tar.xz"' in acquire_script, "XZ case not in detect_archive_type" # Verify that XZ extraction is included in extract_archive function assert "tar.xz|txz)" in acquire_script, "XZ case not in extract_archive" assert "tar -xJf" in acquire_script, "XZ extraction command not found" print("✓ Generator XZ support test passed") def test_list_contents(): """Test that we can list contents of a .tar.xz archive.""" print("Testing list contents for XZ archives...") extractor = ArchiveExtractor() with tempfile.TemporaryDirectory() as temp_dir: temp_path = Path(temp_dir) archive_path = temp_path / "test.tar.xz" # Create a test XZ archive create_test_xz_archive(archive_path) # List contents contents = extractor.list_contents(archive_path) # Verify contents assert len(contents) > 0, "No contents found in archive" assert any("README.md" in item for item in contents), "README.md not found in contents" assert any("configure" in item for item in contents), "configure not found in contents" assert any("Makefile" in item for item in contents), "Makefile not found in contents" print("✓ List contents test passed") def main(): """Run all tests.""" print("Running XZ support tests...\n") try: test_xz_detection() test_xz_extraction() test_generator_xz_support() test_list_contents() print("\n✅ All XZ support tests passed!") return 0 except Exception as e: print(f"\n❌ Test failed: {e}") import traceback traceback.print_exc() return 1 if __name__ == "__main__": sys.exit(main())