#!/usr/bin/env python3 """ Debug script to test USM autoprovides functionality. """ import sys import tempfile from pathlib import Path # Add the src directory to the path sys.path.insert(0, 'src') from autusm.usm_integration import USMIntegration from autusm.manifest import ManifestGenerator from autusm.models import PackageInfo, BuildSystem, BuildSystemType, License, LicenseCategory def test_usm_integration(): """Test USM integration directly.""" print("Testing USM integration...") # Create USM integration instance usm_integration = USMIntegration() # Check if USM is available if not usm_integration.is_available(): print("USM is not available on this system") return False # Create a temporary directory with a simple manifest with tempfile.TemporaryDirectory() as temp_dir: temp_path = Path(temp_dir) # Create a simple manifest file manifest_content = """{ "name": "test", "version": "1.0.0", "summary": "Test package", "licences": [ { "name": "MIT", "text": "LICENSE", "category": "open-source" } ], "provides": { "bin:test": "as-expected" }, "depends": { "runtime": [], "build": [], "manage": [] }, "flags": [], "execs": { "build": "scripts/build" } }""" manifest_path = temp_path / "MANIFEST.usm" with open(manifest_path, "w") as f: f.write(manifest_content) print(f"Created test manifest at: {manifest_path}") # Try to get autoprovides try: autoprovides = usm_integration.get_autoprovides(temp_path) print(f"Got autoprovides: {autoprovides}") return True except Exception as e: print(f"Error getting autoprovides: {e}") import traceback traceback.print_exc() return False def test_parsing_with_garbage(): """Test parsing with garbage output similar to what we're seeing.""" print("\nTesting parsing with garbage output...") usm_integration = USMIntegration() # Test with the garbage pattern we're seeing garbage_output = '"provides": {\n "type": "reg",\n "path": "{",\n "pathBase": "source"\n }' print(f"Testing with garbage output: {repr(garbage_output)}") try: parsed = usm_integration._parse_autoprovides(garbage_output) print(f"Parsed result: {parsed}") return True except Exception as e: print(f"Error parsing garbage: {e}") import traceback traceback.print_exc() return False def test_manifest_update(): """Test manifest update with problematic data.""" print("\nTesting manifest update...") # 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) print(f"Initial manifest provides: {manifest.provides}") # Try to update with garbage data garbage_autoprovides = { '"provides":': { "type": "reg", "path": "{", "pathBase": "source" } } try: updated_manifest = manifest_generator.update_with_autoprovides(manifest, garbage_autoprovides) print(f"Updated manifest provides: {updated_manifest.provides}") # Test JSON serialization json_str = updated_manifest.to_json() print("JSON serialization result:") print(json_str) return True except Exception as e: print(f"Error updating manifest: {e}") import traceback traceback.print_exc() return False if __name__ == "__main__": print("Running debug tests...\n") try: test_usm_integration() test_parsing_with_garbage() test_manifest_update() print("\n✅ All debug tests completed") except Exception as e: print(f"\n❌ Debug test failed: {e}") import traceback traceback.print_exc() sys.exit(1)