From 24b50c2973258e8563b2cbce6f62de8cbe1ebf23 Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Tue, 17 Sep 2024 18:23:59 +0200 Subject: [PATCH] test: Insert default test class for ModIO The standard tests provide a prepared process image and a defined config.rsc. --- tests/__init__.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/__init__.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..35c6c20 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +"""Shared functions for tests.""" +__author__ = "Sven Sager" +__copyright__ = "Copyright (C) 2024 Sven Sager" +__license__ = "GPLv2" + +from os.path import dirname, join +from tempfile import NamedTemporaryFile +from unittest import TestCase + +import revpimodio2 + +DEFAULT_PROCIMG = join(dirname(__file__), "proc.img") +RUN_ON_REVPI = False # todo: Check revpi system + + +class TestRevPiModIO(TestCase): + + data_dir = dirname(__file__) + + def setUp(self): + self.fh_procimg = NamedTemporaryFile("wb+", 0, prefix="test_procimg_") + self.fh_procimg.write(b"\x00" * 4096) + self.fh_procimg.seek(0) + + def tearDown(self): + self.fh_procimg.close() + + def modio(self, **kwargs): + """Default ModIO object with temp process image.""" + if not RUN_ON_REVPI: + if "procimg" in kwargs: + # Use a copy of given prepared process image + with open(kwargs["procimg"], "rb") as fh: + self.fh_procimg.write(fh.read()) + self.fh_procimg.seek(0) + + # Always use the temporary process image of testing class + kwargs["procimg"] = self.fh_procimg.name + + # Always use a config inside the testing folder (default config.rsc) + kwargs["configrsc"] = join(self.data_dir, kwargs.get("configrsc", "config.rsc")) + + return revpimodio2.RevPiModIO(**kwargs)