diff --git a/tests/run_examples_test.py b/tests/run_examples_test.py new file mode 100644 index 0000000..9eb75f0 --- /dev/null +++ b/tests/run_examples_test.py @@ -0,0 +1,31 @@ +import asyncio +import glob +import subprocess + + +async def run_script(file_path: str) -> tuple[str, int | None, bytes, bytes]: + """Run a single script and return the result.""" + process = await asyncio.create_subprocess_exec("python", file_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = await process.communicate() + return file_path, process.returncode, stdout, stderr + + +async def main() -> None: + files: list[str] = glob.glob("example/**/*.py", recursive=True) + tasks: list = [run_script(file) for file in files] + results: list[tuple[str, int | None, bytes, bytes]] = await asyncio.gather(*tasks) + + for file, returncode, stdout, stderr in results: + if returncode != 0: + print(f"Error in {file}:") + print(stderr.decode()) + else: + print(f"{file} executed successfully.") + + +def test_examples() -> None: + asyncio.run(main()) + + +if __name__ == "__main__": + test_examples()