[elbe-devel] [PATCH 09/10] elbepack: test_xml; switch to pytest APIs

Thomas Weißschuh thomas.weissschuh at linutronix.de
Mon Mar 11 18:03:02 CET 2024


Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/tests/test_xml.py | 113 +++++++++++++++++++--------------------------
 1 file changed, 47 insertions(+), 66 deletions(-)

diff --git a/elbepack/tests/test_xml.py b/elbepack/tests/test_xml.py
index 000add833b5e..42ff17ed4b26 100644
--- a/elbepack/tests/test_xml.py
+++ b/elbepack/tests/test_xml.py
@@ -4,85 +4,66 @@
 
 import os
 import sys
-import tempfile
-import unittest
 
-from elbepack.commands.test import ElbeTestCase, ElbeTestLevel, system
+from elbepack.commands.test import system
 from elbepack.directories import elbe_dir, elbe_exe
 
 import pytest
 
 
- at unittest.skipIf(ElbeTestCase.level < ElbeTestLevel.INITVM,
-                 'Test level not set to INITVM')
- at pytest.mark.slow
-class TestSimpleXML(ElbeTestCase):
+def _test_cases(prefix):
+    return [
+        os.path.join(elbe_dir, 'tests', fname)
+        for fname
+        in os.listdir(os.path.join(elbe_dir, 'tests'))
+        if fname.startswith(prefix) and fname.endswith('.xml')
+    ]
+
+
+def _delete_project(uuid):
+    system(f'{sys.executable} {elbe_exe} control del_project {uuid}', allow_fail=True)
 
-    def test_simple_build(self):
-        for param in [os.path.join(elbe_dir, 'tests', fname)
-                      for fname
-                      in os.listdir(os.path.join(elbe_dir, 'tests'))
-                      if fname.startswith('simple') and fname.endswith('.xml')]:
 
-            with tempfile.TemporaryDirectory(prefix='elbe-test-simple-xml-') as build_dir:
+ at pytest.fixture(scope='module', params=_test_cases('simple'))
+def simple_build(request, tmp_path_factory):
+    build_dir = tmp_path_factory.mktemp('build_dir')
+    prj = build_dir / 'uuid.prj'
 
-                prj = os.path.join(build_dir, 'uuid.prj')
-                uuid = None
+    system(
+        f'{sys.executable} {elbe_exe} initvm submit "{request.param}" '
+        f'--output "{build_dir}" --keep-files '
+        f'--build-sdk --writeproject "{prj}"')
 
-                try:
-                    system(
-                        f'{sys.executable} {elbe_exe} initvm submit "{param}" '
-                        f'--output "{build_dir}" --keep-files '
-                        f'--build-sdk --writeproject "{prj}"')
+    uuid = prj.read_text()
 
-                    # Ensure project build is done
-                    with open(prj, 'r') as f:
-                        uuid = f.read()
-                        system(f'{sys.executable} {elbe_exe} control list_projects | '
-                               f'grep {uuid} | grep build_done || false')
+    system(f'{sys.executable} {elbe_exe} control list_projects | '
+           f'grep {uuid} | grep build_done || false')
 
-                    for cmd in ('cdrom', 'img', 'sdk', 'rebuild'):
-                        with self.subTest(f'check build {cmd}'):
-                            system(f'{sys.executable} {elbe_exe} check-build {cmd} "{build_dir}"')
+    yield uuid
 
-                except Exception as e:
-                    raise e
-                else:
-                    # This is a tear down of the project, it's okay if it fails
-                    system(
-                        f'{sys.executable} {elbe_exe} control del_project {uuid}', allow_fail=True)
+    _delete_project(uuid)
+
+
+ at pytest.mark.slow
+ at pytest.mark.parametrize('check_build', ('cdrom', 'img', 'sdk', 'rebuild'))
+def test_simple_build(simple_build, check_build):
+    system(f'{sys.executable} {elbe_exe} check-build {check_build} "{simple_build}"')
 
 
- at unittest.skipIf(ElbeTestCase.level < ElbeTestLevel.INITVM,
-                 'Test level not set to INITVM')
 @pytest.mark.slow
-class TestPbuilder(ElbeTestCase):
-
-    def test_pbuilder_build(self):
-        for param in [os.path.join(elbe_dir, 'tests', fname)
-                      for fname
-                      in os.listdir(os.path.join(elbe_dir, 'tests'))
-                      if fname.startswith('pbuilder') and fname.endswith('.xml')]:
-
-            with tempfile.TemporaryDirectory(prefix='elbe-test-pbuilder-xml-') as build_dir:
-
-                prj = os.path.join(build_dir, 'uuid.prj')
-                uuid = None
-
-                try:
-                    system(f'{sys.executable} {elbe_exe} pbuilder create --xmlfile "{self.param}" \
-                                                        --writeproject "{prj}"')
-                    with open(prj, 'r') as f:
-                        uuid = f.read()
-
-                    for package in ['libgpio', 'gpiotest']:
-                        system(f'cd "{build_dir}"; \
-                                 git clone https://github.com/Linutronix/{package}.git')
-                        system(f'cd "{build_dir}/{package}"; \
-                                 {sys.executable} {elbe_exe} pbuilder build --project {uuid}')
-                except Exception as e:
-                    raise e
-                else:
-                    # This is a tearDown of the project, it's okay if it fails
-                    system(
-                        f'{sys.executable} {elbe_exe} control del_project {uuid}', allow_fail=True)
+ at pytest.mark.parametrize('xml', _test_cases('pbuilder'))
+def test_pbuilder_build(xml, tmp_path, request):
+    build_dir = tmp_path
+    prj = build_dir / 'uuid.prj'
+
+    system(f'{sys.executable} {elbe_exe} pbuilder create --xmlfile "{xml}" '
+           f'--writeproject "{prj}"')
+
+    uuid = prj.read_text()
+    request.addfinalizer(lambda: _delete_project(uuid))
+
+    for package in ['libgpio', 'gpiotest']:
+        system(f'cd "{build_dir}"; \
+                 git clone https://github.com/Linutronix/{package}.git')
+        system(f'cd "{build_dir}/{package}"; \
+                 {sys.executable} {elbe_exe} pbuilder build --project {uuid}')

-- 
2.44.0



More information about the elbe-devel mailing list