tests/lib: Add a new unittest type to simplify launching test programs

We were reusing the same logic everywhere, while we can just reuse an
unique class to base our tests on that avoids having to copy-and-paste
code for no good reason
This commit is contained in:
Marco Trevisan (Treviño)
2025-02-05 00:58:29 +01:00
parent 4bcd99de43
commit 00ebf4e1eb
11 changed files with 234 additions and 407 deletions

View File

@@ -22,7 +22,6 @@
"""Integration tests for gdbus-codegen utility."""
import collections
import os
import shutil
import subprocess
@@ -33,15 +32,13 @@ import unittest
import xml.etree.ElementTree as ET
import taptestrunner
import testprogramrunner
# Disable line length warnings as wrapping the C code templates would be hard
# flake8: noqa: E501
Result = collections.namedtuple("Result", ("info", "out", "err", "subs"))
class TestCodegen(unittest.TestCase):
class TestCodegen(testprogramrunner.TestProgramRunner):
"""Integration test for running gdbus-codegen.
This can be run when installed or uninstalled. When uninstalled, it
@@ -54,8 +51,8 @@ class TestCodegen(unittest.TestCase):
just test command line behaviour in this integration test.
"""
# Track the cwd, we want to back out to that to clean up our tempdir
cwd = ""
PROGRAM_NAME = "gdbus-codegen"
PROGRAM_TYPE = testprogramrunner.ProgramType.INTERPRETED
ARGUMENTS_TYPES = {
"b": {"value_type": "boolean"},
@@ -78,59 +75,12 @@ class TestCodegen(unittest.TestCase):
"asv": {"value_type": "variant", "variant_type": "a{sv}"},
}
def setUp(self):
self.timeout_seconds = 6 # seconds per test
self.tmpdir = tempfile.TemporaryDirectory()
self.cwd = os.getcwd()
os.chdir(self.tmpdir.name)
print("tmpdir:", self.tmpdir.name)
if "G_TEST_BUILDDIR" in os.environ:
self.__codegen = os.path.join(
os.environ["G_TEST_BUILDDIR"],
"..",
"gdbus-2.0",
"codegen",
"gdbus-codegen",
)
else:
self.__codegen = shutil.which("gdbus-codegen")
print("codegen:", self.__codegen)
def tearDown(self):
os.chdir(self.cwd)
self.tmpdir.cleanup()
def runCodegen(self, *args):
argv = [self.__codegen]
# shebang lines are not supported on native
# Windows consoles
if os.name == "nt":
argv.insert(0, sys.executable)
argv.extend(args)
print("Running:", argv)
env = os.environ.copy()
env["LC_ALL"] = "C.UTF-8"
env["G_DEBUG"] = "fatal-warnings"
print("Environment:", env)
# We want to ensure consistent line endings...
info = subprocess.run(
argv,
timeout=self.timeout_seconds,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
universal_newlines=True,
)
info.check_returncode()
out = info.stdout.strip()
err = info.stderr.strip()
return self.runTestProgram(args)
def _getSubs(self):
# Known substitutions for standard boilerplate
subs = {
return {
"standard_top_comment": "/*\n"
" * This file is generated by gdbus-codegen, do not modify it.\n"
" *\n"
@@ -326,11 +276,6 @@ class TestCodegen(unittest.TestCase):
"}",
}
result = Result(info, out, err, subs)
print("Output:", result.out)
return result
def runCodegenWithInterface(self, interface_contents, *args):
with tempfile.NamedTemporaryFile(
dir=self.tmpdir.name, suffix=".xml", delete=False