From 6e13113b5d1a1d8afe00c8a577a014db7df5b0a4 Mon Sep 17 00:00:00 2001 From: Kaushik Varadharajan Date: Fri, 14 Jun 2024 00:05:37 +0000 Subject: Add Pytest unit tests for serialization library Tests run with `pytest` command in repository's root directory. Signed-off-by: Kaushik Varadharajan Change-Id: I9d8a13b233ffafc9119ef503d46f7808461a194f --- python/pytests/conftest.py | 51 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) (limited to 'python/pytests/conftest.py') 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) -- cgit v1.2.1