Created
March 16, 2025 10:06
-
-
Save monperrus/553d1a4cc84d47f2d9dc1f0b6ab11729 to your computer and use it in GitHub Desktop.
a python script to identify the type of build and tests in the current directory and run the tests accordingly (ex maven test for maven, etc)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import os | |
| import subprocess | |
| import sys | |
| def run_command(command): | |
| """Run a shell command and return the result.""" | |
| try: | |
| print(f"Executing: {' '.join(command)}") | |
| result = subprocess.run(command, check=True) | |
| print(f"Tests completed successfully with exit code {result.returncode}") | |
| return True | |
| except subprocess.CalledProcessError as e: | |
| print(f"Error running tests: {e}") | |
| return False | |
| def detect_and_run_tests(): | |
| """Detect build system type and run appropriate tests.""" | |
| current_dir = os.getcwd() | |
| print(f"Detecting build system in: {current_dir}") | |
| # Maven detection | |
| if os.path.exists("pom.xml"): | |
| print("Maven project detected") | |
| return run_command(["mvn", "test"]) | |
| # Gradle detection | |
| if os.path.exists("build.gradle") or os.path.exists("build.gradle.kts"): | |
| print("Gradle project detected") | |
| if os.path.exists("gradlew"): | |
| return run_command(["./gradlew", "test"]) | |
| else: | |
| return run_command(["gradle", "test"]) | |
| # npm/Node.js detection | |
| if os.path.exists("package.json"): | |
| print("Node.js project detected") | |
| return run_command(["npm", "test"]) | |
| # Python detection | |
| if os.path.exists("setup.py") or os.path.exists("pytest.ini") or os.path.exists("requirements.txt"): | |
| print("Python project detected") | |
| if os.path.exists("pytest.ini"): | |
| return run_command(["pytest"]) | |
| elif os.path.exists("setup.py"): | |
| return run_command(["python", "setup.py", "test"]) | |
| else: | |
| # Try to find test files | |
| if os.path.exists("tests") or os.path.exists("test"): | |
| return run_command(["pytest"]) | |
| else: | |
| print("Python project detected but no test directory found") | |
| return False | |
| # Go detection | |
| if os.path.exists("go.mod"): | |
| print("Go project detected") | |
| return run_command(["go", "test", "./..."]) | |
| # Rust detection | |
| if os.path.exists("Cargo.toml"): | |
| print("Rust project detected") | |
| return run_command(["cargo", "test"]) | |
| # .NET detection | |
| if any(f.endswith(".sln") for f in os.listdir(current_dir)) or any(f.endswith(".csproj") for f in os.listdir(current_dir)): | |
| print(".NET project detected") | |
| return run_command(["dotnet", "test"]) | |
| # Ruby/Rails detection | |
| if os.path.exists("Gemfile"): | |
| print("Ruby project detected") | |
| return run_command(["bundle", "exec", "rake", "test"]) | |
| print("No recognized build system detected") | |
| return False | |
| if __name__ == "__main__": | |
| success = detect_and_run_tests() | |
| sys.exit(0 if success else 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment