|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +import platform |
| 4 | +import sys |
| 5 | +import textwrap |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +async def run_test( |
| 10 | + semaphore: asyncio.Semaphore, output_lock: asyncio.Lock, test_file: Path, symbolic_api: str |
| 11 | +) -> bool: |
| 12 | + async with semaphore: |
| 13 | + args = [test_file] |
| 14 | + |
| 15 | + # Codegen is different on macOS; there's some nondeterminism in SymEngine, based on e.g. |
| 16 | + # unordered_map iteration order, that we need to fix. For now, this is fine, so we check |
| 17 | + # that the tests pass, but allow them to generate different code than is checked in. |
| 18 | + include_update_flag = ( |
| 19 | + platform.system() == "Darwin" |
| 20 | + and test_file.name |
| 21 | + in { |
| 22 | + "symforce_codegen_test.py", |
| 23 | + "symforce_examples_robot_3d_localization_codegen_test.py", |
| 24 | + "symforce_examples_custom_factor_generation_codegen_test.py", |
| 25 | + "symforce_examples_bundle_adjustment_fixed_size_codegen_test.py", |
| 26 | + "symforce_gen_codegen_test.py", |
| 27 | + } |
| 28 | + and symbolic_api == "symengine" |
| 29 | + ) |
| 30 | + if include_update_flag: |
| 31 | + args.append("--update") |
| 32 | + |
| 33 | + proc = await asyncio.create_subprocess_exec( |
| 34 | + sys.executable, |
| 35 | + *args, |
| 36 | + env=dict(os.environ, SYMFORCE_WHEEL_TESTS="1", SYMFORCE_SYMBOLIC_API=symbolic_api), |
| 37 | + stderr=asyncio.subprocess.STDOUT, |
| 38 | + stdout=asyncio.subprocess.PIPE, |
| 39 | + ) |
| 40 | + |
| 41 | + stdout, _ = await proc.communicate() |
| 42 | + |
| 43 | + if proc.returncode != 0: |
| 44 | + async with output_lock: |
| 45 | + print( |
| 46 | + textwrap.dedent( |
| 47 | + """ |
| 48 | + -------------------------------------------------------------------------------- |
| 49 | + Test {test_file} on api {symbolic_api} failed with output: |
| 50 | + {output} |
| 51 | + -------------------------------------------------------------------------------- |
| 52 | + """ |
| 53 | + ).format(test_file=test_file, symbolic_api=symbolic_api, output=stdout.decode()) |
| 54 | + ) |
| 55 | + return False |
| 56 | + |
| 57 | + return True |
| 58 | + |
| 59 | + |
| 60 | +async def main() -> int: |
| 61 | + project = Path(sys.argv[1]) |
| 62 | + |
| 63 | + tests = project.glob("test/*_test.py") |
| 64 | + semaphore = asyncio.Semaphore(os.cpu_count()) |
| 65 | + output_lock = asyncio.Lock() |
| 66 | + tests = [(test, symbolic_api) for test in tests for symbolic_api in ("symengine", "sympy")] |
| 67 | + results = await asyncio.gather( |
| 68 | + *[run_test(semaphore, output_lock, test, symbolic_api) for test, symbolic_api in tests] |
| 69 | + ) |
| 70 | + total_tests = len(results) |
| 71 | + succeeded_tests = sum(results) |
| 72 | + print(f"Ran {total_tests}, {succeeded_tests} passed, {total_tests - succeeded_tests} failed") |
| 73 | + if total_tests != succeeded_tests: |
| 74 | + for result, (test, symbolic_api) in zip(results, tests): |
| 75 | + if not result: |
| 76 | + print(f" {test} ({symbolic_api})") |
| 77 | + if total_tests != succeeded_tests: |
| 78 | + return 1 |
| 79 | + return 0 |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + sys.exit(asyncio.run(main())) |
0 commit comments