Skip to content

Instantly share code, notes, and snippets.

@joelonsql
Created October 28, 2024 20:05
Show Gist options
  • Save joelonsql/25716b92657c9fef2b73fb63e3f14226 to your computer and use it in GitHub Desktop.
Save joelonsql/25716b92657c9fef2b73fb63e3f14226 to your computer and use it in GitHub Desktop.
Script that reruns failed PostgreSQL Meson tests and, if they pass, executes the full test suite.
#!/usr/bin/env python3
import json
import subprocess
import sys
from pathlib import Path
def run_meson_tests(test_names=None, meson_args=None):
"""Run meson tests with the given test names and arguments"""
if meson_args is None:
meson_args = []
meson_command = ["meson", "test"] + meson_args
if test_names:
meson_command.extend(test_names)
command_str = ' '.join(f'"{arg}"' if ' ' in arg else arg for arg in meson_command)
print("\n🐘πŸ§ͺ Constructed Meson Test Command:")
print(command_str)
print("\n🐘πŸ§ͺ Executing Meson Test Command...")
try:
result = subprocess.run(meson_command, check=True)
return True
except subprocess.CalledProcessError as e:
print(f"\nπŸ˜πŸ’” Meson tests failed with return code {e.returncode}.")
return False
except FileNotFoundError:
print(f"\nπŸ˜πŸ’” Error: 'meson' command not found. Please ensure Meson is installed and in your PATH.")
sys.exit(1)
def main():
test_log_path = Path('./meson-logs/testlog.json')
if not test_log_path.is_file():
print(f"πŸ˜πŸ’” Error: Test log '{test_log_path}' does not exist.")
sys.exit(1)
failed_tests = []
# Read and parse the test log (JSONL format - one JSON object per line)
with test_log_path.open('r') as log_file:
for line_num, line in enumerate(log_file, start=1):
line = line.strip()
if not line:
continue # Skip empty lines
try:
test = json.loads(line)
except json.JSONDecodeError as e:
print(f"πŸ˜πŸ’” Error: Invalid JSON on line {line_num}: {e}")
sys.exit(1)
if test.get('result') == "ERROR":
full_test_name = test.get('name', '')
if ' / ' in full_test_name:
_, test_name = full_test_name.split(' / ', 1)
else:
test_name = full_test_name # Fallback if format is unexpected
failed_tests.append(test_name)
# Extract any command-line arguments passed to the script
meson_args = sys.argv[1:]
if not failed_tests:
print(f"πŸ˜βœ… No previous failed tests found. Running all tests...")
success = run_meson_tests(meson_args=meson_args)
sys.exit(0 if success else 1)
else:
print(f"πŸ˜πŸ’” Failed Tests:")
for test in failed_tests:
print(f" - {test}")
print("\n🐘πŸ§ͺ Running previously failed tests...")
if run_meson_tests(failed_tests, meson_args):
print(f"\nπŸ˜βœ… Previously failed tests now pass. Running all tests...")
success = run_meson_tests(meson_args=meson_args)
sys.exit(0 if success else 1)
else:
sys.exit(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment