test_comprehensive_build_system_detection.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3
  2. """
  3. Comprehensive test script for build system detection with depth-based prioritization.
  4. """
  5. import os
  6. import tempfile
  7. import shutil
  8. from pathlib import Path
  9. from src.autusm.analyzer import SourceAnalyzer
  10. def test_case_1_autotools_at_root():
  11. """Test case: Autotools at root, cargo in subdirectory (like GCC)."""
  12. with tempfile.TemporaryDirectory() as temp_dir:
  13. base_dir = Path(temp_dir)
  14. # Root level autotools
  15. (base_dir / "configure.ac").touch()
  16. (base_dir / "Makefile.am").touch()
  17. # Subdirectory with cargo
  18. libformat_dir = base_dir / "libformat_parser"
  19. libformat_dir.mkdir()
  20. (libformat_dir / "Cargo.toml").touch()
  21. analyzer = SourceAnalyzer()
  22. build_system = analyzer.detect_build_system(base_dir)
  23. assert build_system.type.value == "autotools", f"Expected autotools, got {build_system.type.value}"
  24. print("✓ Test 1 passed: Autotools at root prioritized over cargo in subdirectory")
  25. return True
  26. def test_case_2_multiple_build_systems_at_root():
  27. """Test case: Multiple build systems at root level (should use priority)."""
  28. with tempfile.TemporaryDirectory() as temp_dir:
  29. base_dir = Path(temp_dir)
  30. # Multiple build systems at root
  31. (base_dir / "configure.ac").touch() # autotools
  32. (base_dir / "CMakeLists.txt").touch() # cmake
  33. (base_dir / "meson.build").touch() # meson
  34. analyzer = SourceAnalyzer()
  35. build_system = analyzer.detect_build_system(base_dir)
  36. # CMAKE has priority 5, AUTOTOOLS has 4, MESON has 6
  37. # MESON should be selected due to highest priority at same depth
  38. assert build_system.type.value == "meson", f"Expected meson, got {build_system.type.value}"
  39. print("✓ Test 2 passed: Meson selected due to highest priority at same depth")
  40. return True
  41. def test_case_3_only_subdirectory_build_systems():
  42. """Test case: Build systems only in subdirectories (should use priority)."""
  43. with tempfile.TemporaryDirectory() as temp_dir:
  44. base_dir = Path(temp_dir)
  45. # Create subdirectories with different build systems
  46. cargo_dir = base_dir / "rust_component"
  47. cargo_dir.mkdir()
  48. (cargo_dir / "Cargo.toml").touch()
  49. npm_dir = base_dir / "js_component"
  50. npm_dir.mkdir()
  51. (npm_dir / "package.json").touch()
  52. analyzer = SourceAnalyzer()
  53. build_system = analyzer.detect_build_system(base_dir)
  54. # NPM has priority 8, CARGO has 7
  55. # NPM should be selected due to higher priority at same depth
  56. assert build_system.type.value == "npm", f"Expected npm, got {build_system.type.value}"
  57. print("✓ Test 3 passed: NPM selected due to higher priority at same depth")
  58. return True
  59. def test_case_4_nested_build_systems():
  60. """Test case: Build systems at different depths."""
  61. with tempfile.TemporaryDirectory() as temp_dir:
  62. base_dir = Path(temp_dir)
  63. # Shallow build system
  64. cmake_dir = base_dir / "cmake_component"
  65. cmake_dir.mkdir()
  66. (cmake_dir / "CMakeLists.txt").touch()
  67. # Deeper build system
  68. cargo_dir = base_dir / "deep" / "rust_component"
  69. cargo_dir.mkdir(parents=True)
  70. (cargo_dir / "Cargo.toml").touch()
  71. analyzer = SourceAnalyzer()
  72. build_system = analyzer.detect_build_system(base_dir)
  73. # CMAKE should be selected due to shallower depth (1 vs 2)
  74. assert build_system.type.value == "cmake", f"Expected cmake, got {build_system.type.value}"
  75. print("✓ Test 4 passed: Shallower build system prioritized regardless of priority")
  76. return True
  77. def test_case_5_no_build_system():
  78. """Test case: No build system detected."""
  79. with tempfile.TemporaryDirectory() as temp_dir:
  80. base_dir = Path(temp_dir)
  81. # Just some source files, no build system
  82. (base_dir / "main.c").touch()
  83. (base_dir / "utils.h").touch()
  84. analyzer = SourceAnalyzer()
  85. build_system = analyzer.detect_build_system(base_dir)
  86. assert build_system.type.value == "unknown", f"Expected unknown, got {build_system.type.value}"
  87. print("✓ Test 5 passed: Unknown build system when none detected")
  88. return True
  89. def run_all_tests():
  90. """Run all test cases."""
  91. tests = [
  92. test_case_1_autotools_at_root,
  93. test_case_2_multiple_build_systems_at_root,
  94. test_case_3_only_subdirectory_build_systems,
  95. test_case_4_nested_build_systems,
  96. test_case_5_no_build_system
  97. ]
  98. passed = 0
  99. failed = 0
  100. for test in tests:
  101. try:
  102. if test():
  103. passed += 1
  104. else:
  105. failed += 1
  106. except Exception as e:
  107. print(f"✗ Test {test.__name__} failed with exception: {e}")
  108. failed += 1
  109. print(f"\nTest Results: {passed} passed, {failed} failed")
  110. return failed == 0
  111. if __name__ == "__main__":
  112. success = run_all_tests()
  113. exit(0 if success else 1)