#!/usr/bin/env python3 """ Test script to verify the autoprovides fix in autusm. This script tests that the autoprovides functionality correctly replaces template-based provides with actual USM output. """ import json import tempfile import os from pathlib import Path from unittest.mock import Mock, patch # Add the src directory to the path import sys sys.path.insert(0, 'src') from autusm.manifest import ManifestGenerator from autusm.models import PackageInfo, BuildSystem, BuildSystemType, USMManifest, License, LicenseCategory, Resource, PathBase, FileType from autusm.usm_integration import USMIntegration def test_autoprovides_integration(): """Test that autoprovides correctly replaces template provides.""" print("Testing autoprovides integration...") # Create test package info package_info = PackageInfo( name="test-package", version="1.0.0", summary="A test package", licenses=[License(name="MIT", text="LICENSE", category=LicenseCategory.OPEN_SOURCE)] ) # Create test build system build_system = BuildSystem(type=BuildSystemType.MAKE) # Create manifest generator manifest_generator = ManifestGenerator() # Generate initial manifest manifest = manifest_generator.generate(package_info, build_system) # Check that template provides are present print(f"Initial provides count: {len(manifest.provides)}") print("Initial provides:") for key, value in manifest.provides.items(): print(f" {key}: {value}") # Create mock autoprovides (simulating USM output) mock_autoprovides = { "bin:test-package": "as-expected", "lib:libtest.so": Resource(path="usr/lib/libtest.so", path_base=PathBase.INSTALL, type=FileType.REG), "inc:test.h": Resource(path="usr/include/test.h", path_base=PathBase.INSTALL, type=FileType.REG), "res:doc/test-package": "as-expected" } # Update manifest with autoprovides updated_manifest = manifest_generator.update_with_autoprovides(manifest, mock_autoprovides) # Check that autoprovides replaced the template provides print(f"\nUpdated provides count: {len(updated_manifest.provides)}") print("Updated provides:") for key, value in updated_manifest.provides.items(): print(f" {key}: {value}") # Verify the autoprovides are now in the manifest assert "bin:test-package" in updated_manifest.provides assert "lib:libtest.so" in updated_manifest.provides assert "inc:test.h" in updated_manifest.provides assert "res:doc/test-package" in updated_manifest.provides # Verify the autoprovides values are correct assert updated_manifest.provides["bin:test-package"] == "as-expected" assert isinstance(updated_manifest.provides["lib:libtest.so"], Resource) assert updated_manifest.provides["lib:libtest.so"].path == "usr/lib/libtest.so" print("\nāœ… Autoprovides integration test passed!") return True def test_usm_integration_parsing(): """Test that USM integration correctly parses autoprovides output.""" print("\nTesting USM integration parsing...") usm_integration = USMIntegration() # Test JSON format output (nested structure) json_output = """{ "provides": { "bin:test-package": "as-expected", "lib:libtest.so": { "path": "usr/lib/libtest.so", "pathBase": "install", "type": "reg" } } }""" parsed_json = usm_integration._parse_autoprovides(json_output) print(f"Parsed JSON autoprovides: {parsed_json}") assert "bin:test-package" in parsed_json assert "lib:libtest.so" in parsed_json assert isinstance(parsed_json["lib:libtest.so"], Resource) # Test line-by-line format output line_output = """bin:test-package as-expected lib:libtest.so install:usr/lib/libtest.so inc:test.h source:include/test.h # This is a comment res:doc/test-package as-expected""" parsed_lines = usm_integration._parse_autoprovides(line_output) print(f"Parsed line autoprovides: {parsed_lines}") assert "bin:test-package" in parsed_lines assert "lib:libtest.so" in parsed_lines assert "inc:test.h" in parsed_lines assert "res:doc/test-package" in parsed_lines print("āœ… USM integration parsing test passed!") return True def test_full_workflow(): """Test the full workflow with mocked USM integration.""" print("\nTesting full workflow...") # Create a temporary directory for testing with tempfile.TemporaryDirectory() as temp_dir: temp_path = Path(temp_dir) # Create test package info package_info = PackageInfo( name="full-test-package", version="1.0.0", summary="A full test package", licenses=[License(name="MIT", text="LICENSE", category=LicenseCategory.OPEN_SOURCE)] ) # Create test build system build_system = BuildSystem(type=BuildSystemType.MAKE) # Create manifest generator manifest_generator = ManifestGenerator() # Generate initial manifest manifest = manifest_generator.generate(package_info, build_system) # Write initial manifest to file manifest_path = temp_path / "MANIFEST.usm" with open(manifest_path, "w") as f: f.write(manifest.to_json()) print(f"Initial manifest written to: {manifest_path}") # Mock USM integration with patch.object(USMIntegration, 'is_available', return_value=True), \ patch.object(USMIntegration, 'get_autoprovides') as mock_autoprovides: # Setup mock autoprovides mock_autoprovides.return_value = { "bin:full-test-package": "as-expected", "lib:libfulltest.so": Resource(path="usr/lib/libfulltest.so", path_base=PathBase.INSTALL, type=FileType.REG) } # Create USM integration instance usm_integration = USMIntegration() # Simulate the workflow if usm_integration.is_available(): autoprovides = usm_integration.get_autoprovides(temp_path) if autoprovides: manifest = manifest_generator.update_with_autoprovides(manifest, autoprovides) # Rewrite the manifest file with open(manifest_path, "w") as f: f.write(manifest.to_json()) # Verify the final manifest with open(manifest_path, "r") as f: final_manifest_data = json.load(f) print("Final manifest provides section:") for key, value in final_manifest_data["provides"].items(): print(f" {key}: {value}") # Verify autoprovides are in the final manifest assert "bin:full-test-package" in final_manifest_data["provides"] assert "lib:libfulltest.so" in final_manifest_data["provides"] print("āœ… Full workflow test passed!") return True if __name__ == "__main__": print("Running autoprovides fix tests...\n") try: test_autoprovides_integration() test_usm_integration_parsing() test_full_workflow() print("\nšŸŽ‰ All tests passed! The autoprovides fix is working correctly.") except AssertionError as e: print(f"\nāŒ Test failed: {e}") sys.exit(1) except Exception as e: print(f"\nāŒ Unexpected error: {e}") import traceback traceback.print_exc() sys.exit(1)