#!/usr/bin/env python3 """ Comprehensive test script for build system detection with depth-based prioritization. """ import os import tempfile import shutil from pathlib import Path from src.autusm.analyzer import SourceAnalyzer def test_case_1_autotools_at_root(): """Test case: Autotools at root, cargo in subdirectory (like GCC).""" with tempfile.TemporaryDirectory() as temp_dir: base_dir = Path(temp_dir) # Root level autotools (base_dir / "configure.ac").touch() (base_dir / "Makefile.am").touch() # Subdirectory with cargo libformat_dir = base_dir / "libformat_parser" libformat_dir.mkdir() (libformat_dir / "Cargo.toml").touch() analyzer = SourceAnalyzer() build_system = analyzer.detect_build_system(base_dir) assert build_system.type.value == "autotools", f"Expected autotools, got {build_system.type.value}" print("✓ Test 1 passed: Autotools at root prioritized over cargo in subdirectory") return True def test_case_2_multiple_build_systems_at_root(): """Test case: Multiple build systems at root level (should use priority).""" with tempfile.TemporaryDirectory() as temp_dir: base_dir = Path(temp_dir) # Multiple build systems at root (base_dir / "configure.ac").touch() # autotools (base_dir / "CMakeLists.txt").touch() # cmake (base_dir / "meson.build").touch() # meson analyzer = SourceAnalyzer() build_system = analyzer.detect_build_system(base_dir) # CMAKE has priority 5, AUTOTOOLS has 4, MESON has 6 # MESON should be selected due to highest priority at same depth assert build_system.type.value == "meson", f"Expected meson, got {build_system.type.value}" print("✓ Test 2 passed: Meson selected due to highest priority at same depth") return True def test_case_3_only_subdirectory_build_systems(): """Test case: Build systems only in subdirectories (should use priority).""" with tempfile.TemporaryDirectory() as temp_dir: base_dir = Path(temp_dir) # Create subdirectories with different build systems cargo_dir = base_dir / "rust_component" cargo_dir.mkdir() (cargo_dir / "Cargo.toml").touch() npm_dir = base_dir / "js_component" npm_dir.mkdir() (npm_dir / "package.json").touch() analyzer = SourceAnalyzer() build_system = analyzer.detect_build_system(base_dir) # NPM has priority 8, CARGO has 7 # NPM should be selected due to higher priority at same depth assert build_system.type.value == "npm", f"Expected npm, got {build_system.type.value}" print("✓ Test 3 passed: NPM selected due to higher priority at same depth") return True def test_case_4_nested_build_systems(): """Test case: Build systems at different depths.""" with tempfile.TemporaryDirectory() as temp_dir: base_dir = Path(temp_dir) # Shallow build system cmake_dir = base_dir / "cmake_component" cmake_dir.mkdir() (cmake_dir / "CMakeLists.txt").touch() # Deeper build system cargo_dir = base_dir / "deep" / "rust_component" cargo_dir.mkdir(parents=True) (cargo_dir / "Cargo.toml").touch() analyzer = SourceAnalyzer() build_system = analyzer.detect_build_system(base_dir) # CMAKE should be selected due to shallower depth (1 vs 2) assert build_system.type.value == "cmake", f"Expected cmake, got {build_system.type.value}" print("✓ Test 4 passed: Shallower build system prioritized regardless of priority") return True def test_case_5_no_build_system(): """Test case: No build system detected.""" with tempfile.TemporaryDirectory() as temp_dir: base_dir = Path(temp_dir) # Just some source files, no build system (base_dir / "main.c").touch() (base_dir / "utils.h").touch() analyzer = SourceAnalyzer() build_system = analyzer.detect_build_system(base_dir) assert build_system.type.value == "unknown", f"Expected unknown, got {build_system.type.value}" print("✓ Test 5 passed: Unknown build system when none detected") return True def run_all_tests(): """Run all test cases.""" tests = [ test_case_1_autotools_at_root, test_case_2_multiple_build_systems_at_root, test_case_3_only_subdirectory_build_systems, test_case_4_nested_build_systems, test_case_5_no_build_system ] passed = 0 failed = 0 for test in tests: try: if test(): passed += 1 else: failed += 1 except Exception as e: print(f"✗ Test {test.__name__} failed with exception: {e}") failed += 1 print(f"\nTest Results: {passed} passed, {failed} failed") return failed == 0 if __name__ == "__main__": success = run_all_tests() exit(0 if success else 1)