[elbe-devel] [PATCH 9/9] tests: test_xml: migrate to run_elbe_subcommand

Thomas Weißschuh thomas.weissschuh at linutronix.de
Thu Jul 25 11:47:19 CEST 2024


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

diff --git a/elbepack/tests/test_xml.py b/elbepack/tests/test_xml.py
index ddff4cf998b6..b620776ae096 100644
--- a/elbepack/tests/test_xml.py
+++ b/elbepack/tests/test_xml.py
@@ -2,12 +2,14 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # SPDX-FileCopyrightText: 2020 Linutronix GmbH
 
+import contextlib
+import io
 import pathlib
 import subprocess
 
 import pytest
 
-from elbepack.directories import run_elbe
+from elbepack.directories import run_elbe_subcommand
 from elbepack.tests import parametrize_xml_test_files, xml_test_files
 
 
@@ -22,15 +24,17 @@ def initvm(tmp_path_factory, request):
 
     initvm_dir = tmp_path_factory.mktemp('initvm-') / 'initvm'
 
-    run_elbe(['initvm', 'create', '--fail-on-warning', '--directory', initvm_dir], check=True)
+    run_elbe_subcommand(['initvm', 'create', '--fail-on-warning', '--directory', initvm_dir])
 
     yield
 
-    run_elbe(['initvm', 'destroy', '--directory', initvm_dir])
+    with contextlib.suppress(Exception):
+        run_elbe_subcommand(['initvm', 'destroy', '--directory', initvm_dir])
 
 
 def _delete_project(uuid):
-    run_elbe(['control', 'del_project', uuid])
+    with contextlib.suppress(Exception):
+        run_elbe_subcommand(['control', 'del_project', uuid])
 
 
 @pytest.fixture(scope='module', params=xml_test_files('simple'), ids=lambda f: f.name)
@@ -38,20 +42,19 @@ def simple_build(request, initvm, tmp_path_factory):
     build_dir = tmp_path_factory.mktemp('build_dir')
     prj = build_dir / 'uuid.prj'
 
-    run_elbe([
+    run_elbe_subcommand([
         'initvm', 'submit', request.param,
         '--output', build_dir,
         '--keep-files', '--build-sdk',
         '--writeproject', prj,
-    ], check=True)
+    ])
 
     uuid = prj.read_text()
 
-    ps = run_elbe([
-        'control', 'list_projects',
-    ], capture_output=True, encoding='utf-8', check=True)
+    with contextlib.redirect_stdout(io.StringIO()) as stdout:
+        run_elbe_subcommand(['control', 'list_projects'])
 
-    if uuid not in ps.stdout:
+    if uuid not in stdout.getvalue():
         raise RuntimeError('Project was not created')
 
     yield build_dir
@@ -62,7 +65,14 @@ def simple_build(request, initvm, tmp_path_factory):
 @pytest.mark.slow
 @pytest.mark.parametrize('check_build', ('schema', 'cdrom', 'img', 'sdk', 'rebuild'))
 def test_simple_build(simple_build, check_build):
-    run_elbe(['check-build', check_build, simple_build], check=True)
+    run_elbe_subcommand(['check-build', check_build, simple_build])
+
+
+def _prjrepo_list_packages(uuid):
+    with contextlib.redirect_stdout(io.StringIO()) as stdout:
+        run_elbe_subcommand(['prjrepo', 'list_packages', uuid])
+
+        return stdout.getvalue()
 
 
 @pytest.mark.slow
@@ -71,33 +81,30 @@ def test_pbuilder_build(initvm, xml, tmp_path, request):
     build_dir = tmp_path
     prj = build_dir / 'uuid.prj'
 
-    run_elbe(['pbuilder', 'create', '--xmlfile', xml, '--writeproject', prj], check=True)
+    run_elbe_subcommand(['pbuilder', 'create', '--xmlfile', xml, '--writeproject', prj])
 
     uuid = prj.read_text()
     request.addfinalizer(lambda: _delete_project(uuid))
 
-    ps = run_elbe(['prjrepo', 'list_packages', uuid], check=True, capture_output=True)
-    assert ps.stdout == b''
+    assert _prjrepo_list_packages(uuid) == b''
 
     for package in ['libgpio', 'gpiotest']:
         subprocess.run(['git', 'clone', f'https://github.com/Linutronix/{package}.git'],
                        check=True, cwd=build_dir)
-        run_elbe(['pbuilder', 'build', '--project', uuid,
-                  '--source', build_dir.joinpath(package), '--output', build_dir.joinpath('out')],
-                 check=True)
+        run_elbe_subcommand(['pbuilder', 'build', '--project', uuid,
+                             '--source', build_dir.joinpath(package),
+                             '--output', build_dir.joinpath('out')])
 
-    ps = run_elbe(['prjrepo', 'list_packages', uuid], check=True, capture_output=True)
-    assert ps.stdout == (
+    assert _prjrepo_list_packages(uuid) == (
         b'gpiotest_1.0_amd64.deb\n'
         b'libgpio-dev_3.0.0_amd64.deb\n'
         b'libgpio1-dbgsym_3.0.0_amd64.deb\n'
         b'libgpio1_3.0.0_amd64.deb\n'
     )
 
-    run_elbe(['prjrepo', 'upload_pkg', uuid, here / 'equivs-dummy_1.0_all.deb'], check=True)
+    run_elbe_subcommand(['prjrepo', 'upload_pkg', uuid, here / 'equivs-dummy_1.0_all.deb'])
 
-    ps = run_elbe(['prjrepo', 'list_packages', uuid], check=True, capture_output=True)
-    assert ps.stdout == (
+    assert _prjrepo_list_packages(uuid) == (
         b'equivs-dummy_1.0_all.deb\n'
         b'gpiotest_1.0_amd64.deb\n'
         b'libgpio-dev_3.0.0_amd64.deb\n'

-- 
2.45.2



More information about the elbe-devel mailing list