From 0445a53513c588f5eadef8e4c2dd1843b225035b Mon Sep 17 00:00:00 2001 From: Shroominic Date: Mon, 22 Jan 2024 21:24:55 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20examples=20test=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/run_examples_test.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/run_examples_test.py 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()