mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-01 15:03:39 +02:00
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:
@@ -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
|
||||
|
@@ -23,21 +23,16 @@
|
||||
|
||||
"""Integration tests for the gio utility."""
|
||||
|
||||
import collections
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
import taptestrunner
|
||||
import testprogramrunner
|
||||
|
||||
|
||||
Result = collections.namedtuple("Result", ("info", "out", "err"))
|
||||
|
||||
|
||||
class TestGioTool(unittest.TestCase):
|
||||
class TestGioTool(testprogramrunner.TestProgramRunner):
|
||||
"""Integration test for running the gio tool.
|
||||
|
||||
This can be run when installed or uninstalled. When uninstalled, it
|
||||
@@ -48,61 +43,11 @@ class TestGioTool(unittest.TestCase):
|
||||
effects on the file system.
|
||||
"""
|
||||
|
||||
# Track the cwd, we want to back out to that to clean up our tempdir
|
||||
cwd = ""
|
||||
|
||||
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)
|
||||
|
||||
ext = ""
|
||||
if os.name == "nt":
|
||||
ext = ".exe"
|
||||
|
||||
if "G_TEST_BUILDDIR" in os.environ:
|
||||
self.__gio = os.path.join(
|
||||
os.environ["G_TEST_BUILDDIR"],
|
||||
"..",
|
||||
"gio" + ext,
|
||||
)
|
||||
else:
|
||||
self.__gio = shutil.which("gio" + ext)
|
||||
print("gio:", self.__gio)
|
||||
|
||||
def tearDown(self):
|
||||
os.chdir(self.cwd)
|
||||
self.tmpdir.cleanup()
|
||||
PROGRAM_NAME = "gio"
|
||||
PROGRAM_TYPE = testprogramrunner.ProgramType.NATIVE
|
||||
|
||||
def runGio(self, *args):
|
||||
argv = [self.__gio]
|
||||
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()
|
||||
|
||||
result = Result(info, out, err)
|
||||
|
||||
print("Output:", result.out)
|
||||
return result
|
||||
return self.runTestProgram(args, timeout_seconds=6)
|
||||
|
||||
def test_help(self):
|
||||
"""Test the --help argument and help subcommand."""
|
||||
|
@@ -186,12 +186,14 @@ test_extra_programs = {
|
||||
python_tests = {
|
||||
# FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/2764
|
||||
'codegen.py' : {
|
||||
'env': {'_G_TEST_PROGRAM_RUNNER_PATH': fs.parent(gdbus_codegen.full_path())},
|
||||
'can_fail' : host_system == 'freebsd',
|
||||
'suite': ['gdbus-codegen', 'slow'],
|
||||
'timeout': 90,
|
||||
},
|
||||
'gio-tool.py' : {
|
||||
'depends' : gio_tool,
|
||||
'env': {'_G_TEST_PROGRAM_RUNNER_PATH': fs.parent(gio_tool.full_path())},
|
||||
'can_fail' : host_system == 'windows',
|
||||
},
|
||||
}
|
||||
@@ -1183,13 +1185,18 @@ foreach test_name, extra_args : python_tests
|
||||
depends += test_extra_programs_targets[program]
|
||||
endforeach
|
||||
|
||||
local_test_env = python_test_env
|
||||
foreach var, value : extra_args.get('env', {})
|
||||
local_test_env.append(var, value)
|
||||
endforeach
|
||||
|
||||
test(
|
||||
test_name,
|
||||
python,
|
||||
protocol : extra_args.get('protocol', test_protocol),
|
||||
depends: depends,
|
||||
args: ['-B', files(test_name)],
|
||||
env: python_test_env,
|
||||
env: local_test_env,
|
||||
timeout: timeout,
|
||||
suite: suite,
|
||||
)
|
||||
|
Reference in New Issue
Block a user