test_autoprovides_fix.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/env python3
  2. """
  3. Test script to verify the autoprovides fix in autusm.
  4. This script tests that the autoprovides functionality correctly
  5. replaces template-based provides with actual USM output.
  6. """
  7. import json
  8. import tempfile
  9. import os
  10. from pathlib import Path
  11. from unittest.mock import Mock, patch
  12. # Add the src directory to the path
  13. import sys
  14. sys.path.insert(0, 'src')
  15. from autusm.manifest import ManifestGenerator
  16. from autusm.models import PackageInfo, BuildSystem, BuildSystemType, USMManifest, License, LicenseCategory, Resource, PathBase, FileType
  17. from autusm.usm_integration import USMIntegration
  18. def test_autoprovides_integration():
  19. """Test that autoprovides correctly replaces template provides."""
  20. print("Testing autoprovides integration...")
  21. # Create test package info
  22. package_info = PackageInfo(
  23. name="test-package",
  24. version="1.0.0",
  25. summary="A test package",
  26. licenses=[License(name="MIT", text="LICENSE", category=LicenseCategory.OPEN_SOURCE)]
  27. )
  28. # Create test build system
  29. build_system = BuildSystem(type=BuildSystemType.MAKE)
  30. # Create manifest generator
  31. manifest_generator = ManifestGenerator()
  32. # Generate initial manifest
  33. manifest = manifest_generator.generate(package_info, build_system)
  34. # Check that template provides are present
  35. print(f"Initial provides count: {len(manifest.provides)}")
  36. print("Initial provides:")
  37. for key, value in manifest.provides.items():
  38. print(f" {key}: {value}")
  39. # Create mock autoprovides (simulating USM output)
  40. mock_autoprovides = {
  41. "bin:test-package": "as-expected",
  42. "lib:libtest.so": Resource(path="usr/lib/libtest.so", path_base=PathBase.INSTALL, type=FileType.REG),
  43. "inc:test.h": Resource(path="usr/include/test.h", path_base=PathBase.INSTALL, type=FileType.REG),
  44. "res:doc/test-package": "as-expected"
  45. }
  46. # Update manifest with autoprovides
  47. updated_manifest = manifest_generator.update_with_autoprovides(manifest, mock_autoprovides)
  48. # Check that autoprovides replaced the template provides
  49. print(f"\nUpdated provides count: {len(updated_manifest.provides)}")
  50. print("Updated provides:")
  51. for key, value in updated_manifest.provides.items():
  52. print(f" {key}: {value}")
  53. # Verify the autoprovides are now in the manifest
  54. assert "bin:test-package" in updated_manifest.provides
  55. assert "lib:libtest.so" in updated_manifest.provides
  56. assert "inc:test.h" in updated_manifest.provides
  57. assert "res:doc/test-package" in updated_manifest.provides
  58. # Verify the autoprovides values are correct
  59. assert updated_manifest.provides["bin:test-package"] == "as-expected"
  60. assert isinstance(updated_manifest.provides["lib:libtest.so"], Resource)
  61. assert updated_manifest.provides["lib:libtest.so"].path == "usr/lib/libtest.so"
  62. print("\n✅ Autoprovides integration test passed!")
  63. return True
  64. def test_usm_integration_parsing():
  65. """Test that USM integration correctly parses autoprovides output."""
  66. print("\nTesting USM integration parsing...")
  67. usm_integration = USMIntegration()
  68. # Test JSON format output (nested structure)
  69. json_output = """{
  70. "provides": {
  71. "bin:test-package": "as-expected",
  72. "lib:libtest.so": {
  73. "path": "usr/lib/libtest.so",
  74. "pathBase": "install",
  75. "type": "reg"
  76. }
  77. }
  78. }"""
  79. parsed_json = usm_integration._parse_autoprovides(json_output)
  80. print(f"Parsed JSON autoprovides: {parsed_json}")
  81. assert "bin:test-package" in parsed_json
  82. assert "lib:libtest.so" in parsed_json
  83. assert isinstance(parsed_json["lib:libtest.so"], Resource)
  84. # Test line-by-line format output
  85. line_output = """bin:test-package as-expected
  86. lib:libtest.so install:usr/lib/libtest.so
  87. inc:test.h source:include/test.h
  88. # This is a comment
  89. res:doc/test-package as-expected"""
  90. parsed_lines = usm_integration._parse_autoprovides(line_output)
  91. print(f"Parsed line autoprovides: {parsed_lines}")
  92. assert "bin:test-package" in parsed_lines
  93. assert "lib:libtest.so" in parsed_lines
  94. assert "inc:test.h" in parsed_lines
  95. assert "res:doc/test-package" in parsed_lines
  96. print("✅ USM integration parsing test passed!")
  97. return True
  98. def test_full_workflow():
  99. """Test the full workflow with mocked USM integration."""
  100. print("\nTesting full workflow...")
  101. # Create a temporary directory for testing
  102. with tempfile.TemporaryDirectory() as temp_dir:
  103. temp_path = Path(temp_dir)
  104. # Create test package info
  105. package_info = PackageInfo(
  106. name="full-test-package",
  107. version="1.0.0",
  108. summary="A full test package",
  109. licenses=[License(name="MIT", text="LICENSE", category=LicenseCategory.OPEN_SOURCE)]
  110. )
  111. # Create test build system
  112. build_system = BuildSystem(type=BuildSystemType.MAKE)
  113. # Create manifest generator
  114. manifest_generator = ManifestGenerator()
  115. # Generate initial manifest
  116. manifest = manifest_generator.generate(package_info, build_system)
  117. # Write initial manifest to file
  118. manifest_path = temp_path / "MANIFEST.usm"
  119. with open(manifest_path, "w") as f:
  120. f.write(manifest.to_json())
  121. print(f"Initial manifest written to: {manifest_path}")
  122. # Mock USM integration
  123. with patch.object(USMIntegration, 'is_available', return_value=True), \
  124. patch.object(USMIntegration, 'get_autoprovides') as mock_autoprovides:
  125. # Setup mock autoprovides
  126. mock_autoprovides.return_value = {
  127. "bin:full-test-package": "as-expected",
  128. "lib:libfulltest.so": Resource(path="usr/lib/libfulltest.so", path_base=PathBase.INSTALL, type=FileType.REG)
  129. }
  130. # Create USM integration instance
  131. usm_integration = USMIntegration()
  132. # Simulate the workflow
  133. if usm_integration.is_available():
  134. autoprovides = usm_integration.get_autoprovides(temp_path)
  135. if autoprovides:
  136. manifest = manifest_generator.update_with_autoprovides(manifest, autoprovides)
  137. # Rewrite the manifest file
  138. with open(manifest_path, "w") as f:
  139. f.write(manifest.to_json())
  140. # Verify the final manifest
  141. with open(manifest_path, "r") as f:
  142. final_manifest_data = json.load(f)
  143. print("Final manifest provides section:")
  144. for key, value in final_manifest_data["provides"].items():
  145. print(f" {key}: {value}")
  146. # Verify autoprovides are in the final manifest
  147. assert "bin:full-test-package" in final_manifest_data["provides"]
  148. assert "lib:libfulltest.so" in final_manifest_data["provides"]
  149. print("✅ Full workflow test passed!")
  150. return True
  151. if __name__ == "__main__":
  152. print("Running autoprovides fix tests...\n")
  153. try:
  154. test_autoprovides_integration()
  155. test_usm_integration_parsing()
  156. test_full_workflow()
  157. print("\n🎉 All tests passed! The autoprovides fix is working correctly.")
  158. except AssertionError as e:
  159. print(f"\n❌ Test failed: {e}")
  160. sys.exit(1)
  161. except Exception as e:
  162. print(f"\n❌ Unexpected error: {e}")
  163. import traceback
  164. traceback.print_exc()
  165. sys.exit(1)