aboutsummaryrefslogtreecommitdiff
path: root/python/pytests/conftest.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/pytests/conftest.py')
-rw-r--r--python/pytests/conftest.py51
1 files changed, 45 insertions, 6 deletions
diff --git a/python/pytests/conftest.py b/python/pytests/conftest.py
index b595a01..1cb0857 100644
--- a/python/pytests/conftest.py
+++ b/python/pytests/conftest.py
@@ -16,6 +16,8 @@
import pathlib
import shutil
+import subprocess
+import pytest
def pytest_sessionstart():
@@ -24,13 +26,50 @@ def pytest_sessionstart():
base_dir = (pathlib.Path(__file__).parent / "../..").resolve()
tmp_dir = base_dir / "python/pytests/tmp"
- tmp_dir.mkdir(exist_ok=True)
+ if tmp_dir.exists():
+ shutil.rmtree(tmp_dir)
+ tmp_dir.mkdir()
+ # Using flatc and flatbuffers' reflection feature to convert tosa.fbs to
+ # json for easier reading
+ flatbuffers_dir = base_dir / "third_party/flatbuffers"
+ flatc = flatbuffers_dir / "flatc"
+ reflection_fbs = flatbuffers_dir / "reflection/reflection.fbs"
+ tosa_fbs = base_dir / "schema/tosa.fbs"
-def pytest_sessionfinish():
- """Cleaning up temporary files."""
+ # Using flatbuffers reflection to serialize the TOSA flatbuffers schema
+ # into binary
+ _ = subprocess.run(
+ [flatc, "--binary", "--schema", "-o", tmp_dir, tosa_fbs], check=True
+ )
- base_dir = (pathlib.Path(__file__).parent / "../..").resolve()
- tmp_dir = base_dir / "python/pytests/tmp"
+ # This file is generated by the previous command
+ tosa_bfbs = tmp_dir / "tosa.bfbs"
+
+ # Deserializing the binary into JSON using the reflection schema
+ _ = subprocess.run(
+ [
+ flatc,
+ "--json",
+ "--strict-json",
+ "-o",
+ tmp_dir,
+ reflection_fbs,
+ "--",
+ tosa_bfbs,
+ ],
+ check=True,
+ )
+
+
+def pytest_addoption(parser: pytest.Parser):
+ parser.addoption("--leave-tmp", dest="leave_tmp", action="store_true")
+
+
+def pytest_sessionfinish(session: pytest.Session):
+ """Cleaning up temporary files, unless the --leave-tmp flag is set"""
- shutil.rmtree(tmp_dir)
+ if not session.config.option.leave_tmp:
+ base_dir = (pathlib.Path(__file__).parent / "../..").resolve()
+ tmp_dir = base_dir / "python/pytests/tmp"
+ shutil.rmtree(tmp_dir)