|
| 1 | +import contextlib |
| 2 | +import glob |
| 3 | +import io |
| 4 | +import os |
| 5 | +import runpy |
| 6 | +import sys |
| 7 | + |
| 8 | +# Check if a specific test is provided |
| 9 | +if len(sys.argv) > 1: |
| 10 | + TEST_FILE = sys.argv[1] |
| 11 | + print(f"Checking file: {TEST_FILE}") |
| 12 | + if not os.path.isfile(TEST_FILE): |
| 13 | + print("Error: Test file '{}' not found.".format(TEST_FILE)) |
| 14 | + sys.exit(1) |
| 15 | + TEST_FILES = [TEST_FILE] |
| 16 | +else: |
| 17 | + test_dir = os.getenv("TEST_DIR", "./tests/") |
| 18 | + g = f"{test_dir}*.py" |
| 19 | + print(f"CHecking glob: {g}") |
| 20 | + # Find all Python test files in ./tests directory |
| 21 | + TEST_FILES = [f for f in glob.glob(g) if ".skip" not in f and ".broken" not in f] |
| 22 | + |
| 23 | +# Colors for output |
| 24 | +GREEN = "\033[0;32m" |
| 25 | +RED = "\033[0;31m" |
| 26 | +YELLOW = "\033[1;33m" |
| 27 | +NC = "\033[0m" # No Color |
| 28 | + |
| 29 | + |
| 30 | +# Function to run a test and display result |
| 31 | +def run_test(test_file): |
| 32 | + print("{}Running test: {}{}".format(YELLOW, test_file, NC)) |
| 33 | + |
| 34 | + buf = io.StringIO() |
| 35 | + try: |
| 36 | + with contextlib.redirect_stdout(buf): |
| 37 | + runpy.run_path(test_file, run_name="__main__") |
| 38 | + print(buf.getvalue()) |
| 39 | + print("{}✓ PASS: {}{}".format(GREEN, test_file, NC)) |
| 40 | + return True |
| 41 | + except SystemExit as e: |
| 42 | + output = buf.getvalue() |
| 43 | + print(output) |
| 44 | + if e.code == 0: |
| 45 | + print("{}✓ PASS: {}{}".format(GREEN, test_file, NC)) |
| 46 | + return True |
| 47 | + else: |
| 48 | + print("{}✗ FAIL: {}{}".format(RED, test_file, NC)) |
| 49 | + return False |
| 50 | + except Exception: |
| 51 | + output = buf.getvalue() |
| 52 | + print(output) |
| 53 | + print("{}✗ FAIL: {}{}".format(RED, test_file, NC)) |
| 54 | + import traceback |
| 55 | + |
| 56 | + traceback.print_exc() |
| 57 | + return False |
| 58 | + |
| 59 | + |
| 60 | +# Initialize counters and failed list |
| 61 | +TOTAL_TESTS = 0 |
| 62 | +PASSED_TESTS = 0 |
| 63 | +FAILED_TESTS = 0 |
| 64 | +FAILED_LIST = [] |
| 65 | + |
| 66 | +# Run each test |
| 67 | +print("Found {} tests".format(len(TEST_FILES))) |
| 68 | +for test in TEST_FILES: |
| 69 | + TOTAL_TESTS += 1 |
| 70 | + if run_test(test): |
| 71 | + PASSED_TESTS += 1 |
| 72 | + else: |
| 73 | + FAILED_TESTS += 1 |
| 74 | + FAILED_LIST.append(test) |
| 75 | + print("") |
| 76 | + |
| 77 | +# Summary |
| 78 | +print("{}Test Summary:{}".format(YELLOW, NC)) |
| 79 | +print("Total tests: {}".format(TOTAL_TESTS)) |
| 80 | +print("{}Passed: {}{}".format(GREEN, PASSED_TESTS, NC)) |
| 81 | +print("{}Failed: {}{}".format(RED, FAILED_TESTS, NC)) |
| 82 | + |
| 83 | +# List failed tests if any |
| 84 | +if FAILED_TESTS > 0: |
| 85 | + print("{}Failed tests:{}".format(RED, NC)) |
| 86 | + for failed in FAILED_LIST: |
| 87 | + print("{} - {}{}".format(RED, failed, NC)) |
| 88 | + |
| 89 | +# Exit with failure if any test failed |
| 90 | +if FAILED_TESTS > 0: |
| 91 | + sys.exit(1) |
| 92 | +else: |
| 93 | + print("{}All tests passed! Package works.{}".format(GREEN, NC)) |
0 commit comments