test_xz_support.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env python3
  2. """
  3. Test script to verify XZ archive support in autusm.
  4. This script tests that .tar.xz archives can be properly detected and extracted.
  5. """
  6. import os
  7. import sys
  8. import tempfile
  9. import tarfile
  10. from pathlib import Path
  11. # Add the src directory to the path so we can import autusm modules
  12. sys.path.insert(0, str(Path(__file__).parent / "src"))
  13. from autusm.extractor import ArchiveExtractor
  14. from autusm.generator import ScriptGenerator
  15. from autusm.models import PackageInfo, BuildSystem, BuildSystemType
  16. def create_test_xz_archive(archive_path: Path) -> None:
  17. """Create a test .tar.xz archive with some sample files.
  18. Args:
  19. archive_path: Path where to create the archive
  20. """
  21. # Create a temporary directory with test files
  22. with tempfile.TemporaryDirectory() as temp_dir:
  23. temp_path = Path(temp_dir)
  24. test_dir = temp_path / "test_package"
  25. test_dir.mkdir()
  26. # Create some test files
  27. (test_dir / "README.md").write_text("# Test Package\nThis is a test package.")
  28. (test_dir / "configure").write_text("#!/bin/sh\necho 'Configuring...'")
  29. (test_dir / "Makefile").write_text("all:\n\techo 'Building...'")
  30. # Create the tar.xz archive
  31. with tarfile.open(archive_path, 'w:xz') as tar:
  32. tar.add(test_dir, arcname="test_package")
  33. def test_xz_detection():
  34. """Test that .tar.xz archives are properly detected."""
  35. print("Testing XZ archive detection...")
  36. extractor = ArchiveExtractor()
  37. # Test with .tar.xz extension
  38. assert extractor._detect_format(Path("test.tar.xz")) == ".tar.xz", "Failed to detect .tar.xz"
  39. # Test with .txz extension
  40. assert extractor._detect_format(Path("test.txz")) == ".txz", "Failed to detect .txz"
  41. print("✓ XZ archive detection test passed")
  42. def test_xz_extraction():
  43. """Test that .tar.xz archives can be properly extracted."""
  44. print("Testing XZ archive extraction...")
  45. extractor = ArchiveExtractor()
  46. with tempfile.TemporaryDirectory() as temp_dir:
  47. temp_path = Path(temp_dir)
  48. archive_path = temp_path / "test.tar.xz"
  49. extract_dir = temp_path / "extracted"
  50. # Create a test XZ archive
  51. create_test_xz_archive(archive_path)
  52. # Extract the archive
  53. extractor.extract(archive_path, extract_dir)
  54. # Verify extraction
  55. assert extract_dir.exists(), "Extraction directory was not created"
  56. assert (extract_dir / "README.md").exists(), "README.md was not extracted"
  57. assert (extract_dir / "configure").exists(), "configure was not extracted"
  58. assert (extract_dir / "Makefile").exists(), "Makefile was not extracted"
  59. # Verify content
  60. readme_content = (extract_dir / "README.md").read_text()
  61. assert "Test Package" in readme_content, "README content is incorrect"
  62. print("✓ XZ archive extraction test passed")
  63. def test_generator_xz_support():
  64. """Test that the generator includes XZ support in acquire scripts."""
  65. print("Testing generator XZ support...")
  66. generator = ScriptGenerator()
  67. package_info = PackageInfo(name="test_package", url="http://example.com/test.tar.xz")
  68. build_system = BuildSystem(type=BuildSystemType.AUTOTOOLS)
  69. # Generate the acquire script
  70. acquire_script = generator._common_acquire_template(package_info, build_system)
  71. # Verify that XZ is included in the detect_archive_type function
  72. assert "*.tar.xz|*.txz" in acquire_script, "XZ format not included in detect_archive_type"
  73. assert 'echo "tar.xz"' in acquire_script, "XZ case not in detect_archive_type"
  74. # Verify that XZ extraction is included in extract_archive function
  75. assert "tar.xz|txz)" in acquire_script, "XZ case not in extract_archive"
  76. assert "tar -xJf" in acquire_script, "XZ extraction command not found"
  77. print("✓ Generator XZ support test passed")
  78. def test_list_contents():
  79. """Test that we can list contents of a .tar.xz archive."""
  80. print("Testing list contents for XZ archives...")
  81. extractor = ArchiveExtractor()
  82. with tempfile.TemporaryDirectory() as temp_dir:
  83. temp_path = Path(temp_dir)
  84. archive_path = temp_path / "test.tar.xz"
  85. # Create a test XZ archive
  86. create_test_xz_archive(archive_path)
  87. # List contents
  88. contents = extractor.list_contents(archive_path)
  89. # Verify contents
  90. assert len(contents) > 0, "No contents found in archive"
  91. assert any("README.md" in item for item in contents), "README.md not found in contents"
  92. assert any("configure" in item for item in contents), "configure not found in contents"
  93. assert any("Makefile" in item for item in contents), "Makefile not found in contents"
  94. print("✓ List contents test passed")
  95. def main():
  96. """Run all tests."""
  97. print("Running XZ support tests...\n")
  98. try:
  99. test_xz_detection()
  100. test_xz_extraction()
  101. test_generator_xz_support()
  102. test_list_contents()
  103. print("\n✅ All XZ support tests passed!")
  104. return 0
  105. except Exception as e:
  106. print(f"\n❌ Test failed: {e}")
  107. import traceback
  108. traceback.print_exc()
  109. return 1
  110. if __name__ == "__main__":
  111. sys.exit(main())