[elbe-devel] [PATCH 09/11] elbepack: migrate tests to subprocess package

Thomas Weißschuh thomas.weissschuh at linutronix.de
Wed Mar 13 16:54:58 CET 2024


The subprocess APIs are more powerful, better documented and
standardized.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/commands/test.py      | 25 -------------------------
 elbepack/tests/test_flake8.py  | 11 ++---------
 elbepack/tests/test_preproc.py |  6 ++----
 elbepack/tests/test_xml.py     | 38 +++++++++++++++++++++-----------------
 4 files changed, 25 insertions(+), 55 deletions(-)

diff --git a/elbepack/commands/test.py b/elbepack/commands/test.py
deleted file mode 100644
index a2a025a6101f..000000000000
--- a/elbepack/commands/test.py
+++ /dev/null
@@ -1,25 +0,0 @@
-# ELBE - Debian Based Embedded Rootfilesystem Builder
-# SPDX-License-Identifier: GPL-3.0-or-later
-# SPDX-FileCopyrightText: 2020 Linutronix GmbH
-
-# elbepack/commands/test.py - Elbe unit test wrapper
-
-import subprocess
-
-
-class ElbeTestException(Exception):
-
-    def __init__(self, cmd, ret, out):
-        super().__init__()
-        self.cmd = cmd
-        self.ret = ret
-        self.out = out
-
-    def __str__(self):
-        return f'ElbeTestException: \"{self.cmd}\" returns {self.ret}\noutput:\n{self.out}'
-
-
-def system(cmd, allow_fail=False):
-    ps = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-    if ps.returncode != 0 and not allow_fail:
-        raise ElbeTestException(ps.args, ps.returncode, ps.stdout)
diff --git a/elbepack/tests/test_flake8.py b/elbepack/tests/test_flake8.py
index 5467b2976c2e..2881ee341862 100644
--- a/elbepack/tests/test_flake8.py
+++ b/elbepack/tests/test_flake8.py
@@ -3,17 +3,13 @@
 # SPDX-FileCopyrightText: 2020 Linutronix GmbH
 
 import glob
+import subprocess
 
-from elbepack.commands.test import ElbeTestException, system
 from elbepack.directories import elbe_exe, pack_dir
 
 import pytest
 
 
-flake8_opts = ['--max-line-length=100',
-               '--show-source']
-
-
 def _python_files():
     files = glob.glob(pack_dir + '/**/*.py', recursive=True)
     files.append(elbe_exe)
@@ -22,7 +18,4 @@ def _python_files():
 
 @pytest.mark.parametrize('f', _python_files())
 def test_flake8(f):
-    try:
-        system(f'flake8 {" ".join(flake8_opts)} {f}')
-    except ElbeTestException as e:
-        pytest.fail(e.out)
+    subprocess.run(['flake8', '--max-line-length=100', '--show-source', f], check=True)
diff --git a/elbepack/tests/test_preproc.py b/elbepack/tests/test_preproc.py
index cc807836628a..be773465b2b5 100644
--- a/elbepack/tests/test_preproc.py
+++ b/elbepack/tests/test_preproc.py
@@ -3,10 +3,8 @@
 # SPDX-FileCopyrightText: 2020-2021 Linutronix GmbH
 
 import os
-import sys
 
-from elbepack.commands.test import system
-from elbepack.directories import elbe_dir, elbe_exe
+from elbepack.directories import elbe_dir, run_elbe
 
 import pytest
 
@@ -22,4 +20,4 @@ def _test_cases():
 
 @pytest.mark.parametrize('f', _test_cases())
 def test_preproc(f):
-    system(f'{sys.executable} {elbe_exe} preprocess "{f}"')
+    run_elbe(['preprocess', f], check=True)
diff --git a/elbepack/tests/test_xml.py b/elbepack/tests/test_xml.py
index 47bebb9f739c..1d370f947961 100644
--- a/elbepack/tests/test_xml.py
+++ b/elbepack/tests/test_xml.py
@@ -3,10 +3,9 @@
 # SPDX-FileCopyrightText: 2020 Linutronix GmbH
 
 import os
-import sys
+import subprocess
 
-from elbepack.commands.test import system
-from elbepack.directories import elbe_dir, elbe_exe
+from elbepack.directories import elbe_dir, run_elbe
 
 import pytest
 
@@ -21,7 +20,7 @@ def _test_cases(prefix):
 
 
 def _delete_project(uuid):
-    system(f'{sys.executable} {elbe_exe} control del_project {uuid}', allow_fail=True)
+    run_elbe(['control', 'del_project', uuid])
 
 
 @pytest.fixture(scope='module', params=_test_cases('simple'))
@@ -29,15 +28,21 @@ def simple_build(request, tmp_path_factory):
     build_dir = tmp_path_factory.mktemp('build_dir')
     prj = build_dir / 'uuid.prj'
 
-    system(
-        f'{sys.executable} {elbe_exe} initvm submit "{request.param}" '
-        f'--output "{build_dir}" --keep-files '
-        f'--build-sdk --writeproject "{prj}"')
+    run_elbe([
+        'initvm', 'submit', request.param,
+        '--output', build_dir,
+        '--keep-files', '--build-sdk',
+        '--writeproject', prj,
+    ], check=True)
 
     uuid = prj.read_text()
 
-    system(f'{sys.executable} {elbe_exe} control list_projects | '
-           f'grep {uuid} | grep build_done || false')
+    ps = run_elbe([
+        'control', 'list_projects',
+    ], capture_output=True, encoding='utf-8', check=True)
+
+    if uuid not in ps.stdout:
+        raise RuntimeError('Project was not created')
 
     yield build_dir
 
@@ -47,7 +52,7 @@ def simple_build(request, tmp_path_factory):
 @pytest.mark.slow
 @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}"')
+    run_elbe(['check-build', check_build, simple_build], check=True)
 
 
 @pytest.mark.slow
@@ -56,14 +61,13 @@ 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}"')
+    run_elbe(['pbuilder', 'create', '--xmlfile', xml], check=True)
 
     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}')
+        subprocess.run(['git', 'clone', f'https://github.com/Linutronix/{package}.git'],
+                       check=True, cwd=build_dir)
+        run_elbe(['pbuilder', 'build', '--project', uuid],
+                 check=True, cwd=build_dir.joinpath(package))

-- 
2.44.0



More information about the elbe-devel mailing list