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 glib-genmarshal utility."""
import collections
import os
import shutil
import subprocess
@@ -32,16 +31,14 @@ from textwrap import dedent
import unittest
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 TestGenmarshal(unittest.TestCase):
class TestGenmarshal(testprogramrunner.TestProgramRunner):
"""Integration test for running glib-genmarshal.
This can be run when installed or uninstalled. When uninstalled, it
@@ -54,58 +51,15 @@ class TestGenmarshal(unittest.TestCase):
convert this test to just check command line behaviour.
"""
# Track the cwd, we want to back out to that to clean up our tempdir
cwd = ""
def setUp(self):
self.timeout_seconds = 10 # 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.__genmarshal = os.path.join(
os.environ["G_TEST_BUILDDIR"], "..", "glib-genmarshal"
)
else:
self.__genmarshal = shutil.which("glib-genmarshal")
print("genmarshal:", self.__genmarshal)
def tearDown(self):
os.chdir(self.cwd)
self.tmpdir.cleanup()
PROGRAM_NAME = "glib-genmarshal"
PROGRAM_TYPE = testprogramrunner.ProgramType.INTERPRETED
def runGenmarshal(self, *args):
argv = [self.__genmarshal]
# 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": "This file is generated by glib-genmarshal, do not modify "
"it. This code is licensed under the same license as the "
"containing project. Note that it links to GLib, so must "