From 00ebf4e1eb975b883e1a8ea1b92d9f08976fb97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 5 Feb 2025 00:58:29 +0100 Subject: [PATCH] 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 --- gio/tests/codegen.py | 69 ++------------ gio/tests/gio-tool.py | 65 +------------ gio/tests/meson.build | 9 +- glib/tests/assert-msg-test.py | 86 +++-------------- glib/tests/messages-low-memory.py | 52 +--------- gobject/tests/genmarshal.py | 60 ++---------- gobject/tests/gobject-query.py | 48 +--------- gobject/tests/meson.build | 16 +++- gobject/tests/mkenums.py | 72 +++----------- tests/lib/testprogramrunner.py | 153 ++++++++++++++++++++++++++++++ tests/meson.build | 11 ++- 11 files changed, 234 insertions(+), 407 deletions(-) create mode 100644 tests/lib/testprogramrunner.py diff --git a/gio/tests/codegen.py b/gio/tests/codegen.py index a18dcb1a9..7d5dcfd97 100644 --- a/gio/tests/codegen.py +++ b/gio/tests/codegen.py @@ -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 diff --git a/gio/tests/gio-tool.py b/gio/tests/gio-tool.py index 1a030dfb9..73d80dbc6 100644 --- a/gio/tests/gio-tool.py +++ b/gio/tests/gio-tool.py @@ -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.""" diff --git a/gio/tests/meson.build b/gio/tests/meson.build index 01105e56e..682453d1f 100644 --- a/gio/tests/meson.build +++ b/gio/tests/meson.build @@ -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, ) diff --git a/glib/tests/assert-msg-test.py b/glib/tests/assert-msg-test.py index 5e5293b5b..9d2bde4d3 100755 --- a/glib/tests/assert-msg-test.py +++ b/glib/tests/assert-msg-test.py @@ -20,16 +20,13 @@ """ Integration tests for g_assert() functions. """ -import collections import os import shutil -import subprocess import tempfile import unittest import taptestrunner - -Result = collections.namedtuple("Result", ("info", "out", "err")) +import testprogramrunner GDB_SCRIPT = """ # Work around https://sourceware.org/bugzilla/show_bug.cgi?id=22501 @@ -42,7 +39,7 @@ quit """ -class TestAssertMessage(unittest.TestCase): +class TestAssertMessage(testprogramrunner.TestProgramRunner): """Integration test for throwing message on g_assert(). This can be run when installed or uninstalled. When uninstalled, @@ -54,80 +51,21 @@ class TestAssertMessage(unittest.TestCase): and automated tools can more easily debug assertion failures. """ - def setUp(self): - self.__gdb = shutil.which("gdb") - self.timeout_seconds = 10 # seconds per test + PROGRAM_NAME = "assert-msg-test" + PROGRAM_TYPE = testprogramrunner.ProgramType.NATIVE - ext = "" - if os.name == "nt": - ext = ".exe" - if "G_TEST_BUILDDIR" in os.environ: - self.__assert_msg_test = os.path.join( - os.environ["G_TEST_BUILDDIR"], "assert-msg-test" + ext - ) - else: - self.__assert_msg_test = os.path.join( - os.path.dirname(__file__), "assert-msg-test" + ext - ) - print("assert-msg-test:", self.__assert_msg_test) + def setUp(self): + super().setUp() + self.__gdb = shutil.which("gdb") def runAssertMessage(self, *args): - argv = [self.__assert_msg_test] - argv.extend(args) - print("Running:", argv) - - env = os.environ.copy() - env["LC_ALL"] = "C.UTF-8" - 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, - ) - out = info.stdout.strip() - err = info.stderr.strip() - - result = Result(info, out, err) - - print("Output:", result.out) - print("Error:", result.err) - return result + return self.runTestProgram(args, should_fail=True) def runGdbAssertMessage(self, *args): if self.__gdb is None: - return Result(None, "", "") + return testprogramrunner.Result() - argv = ["gdb", "-n", "--batch"] - argv.extend(args) - print("Running:", argv) - - env = os.environ.copy() - env["LC_ALL"] = "C.UTF-8" - 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, - ) - out = info.stdout.strip() - err = info.stderr.strip() - - result = Result(info, out, err) - - print("Output:", result.out) - print("Error:", result.err) - print(result.info) - return result + return self.runTestProgram(args, wrapper_args=["gdb", "-n", "--batch"]) def test_gassert(self): """Test running g_assert() and fail the program.""" @@ -154,9 +92,7 @@ class TestAssertMessage(unittest.TestCase): try: tmp.write(GDB_SCRIPT) tmp.close() - result = self.runGdbAssertMessage( - "-x", tmp.name, self.__assert_msg_test - ) + result = self.runGdbAssertMessage("-x", tmp.name) finally: os.unlink(tmp.name) diff --git a/glib/tests/messages-low-memory.py b/glib/tests/messages-low-memory.py index eb8036958..32f066f07 100644 --- a/glib/tests/messages-low-memory.py +++ b/glib/tests/messages-low-memory.py @@ -21,17 +21,14 @@ """ Integration tests for g_message functions on low-memory. """ -import collections import os -import subprocess import unittest import taptestrunner - -Result = collections.namedtuple("Result", ("info", "out", "err")) +import testprogramrunner -class TestMessagesLowMemory(unittest.TestCase): +class TestMessagesLowMemory(testprogramrunner.TestProgramRunner): """Integration test for checking g_message()’s behavior on low memory. This can be run when installed or uninstalled. When uninstalled, @@ -42,51 +39,12 @@ class TestMessagesLowMemory(unittest.TestCase): error message. """ - test_binary = "messages-low-memory" - - def setUp(self): - ext = "" - if os.name == "nt": - ext = ".exe" - if "G_TEST_BUILDDIR" in os.environ: - self._test_binary = os.path.join( - os.environ["G_TEST_BUILDDIR"], self.test_binary + ext - ) - else: - self._test_binary = os.path.join( - os.path.dirname(__file__), self.test_binary + ext - ) - print("messages-low-memory:", self._test_binary) - - def runTestBinary(self, *args): - print("Running:", *args) - - 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( - *args, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - env=env, - universal_newlines=True, - ) - out = info.stdout.strip() - err = info.stderr.strip() - - result = Result(info, out, err) - - print("Return code:", result.info.returncode) - print("Output:", result.out) - print("Error:", result.err) - return result + PROGRAM_NAME = "messages-low-memory" + PROGRAM_TYPE = testprogramrunner.ProgramType.NATIVE def test_message_memory_allocation_failure(self): """Test running g_message() when memory is exhausted.""" - result = self.runTestBinary(self._test_binary) + result = self.runTestProgram([], should_fail=True) if result.info.returncode == 77: self.skipTest("Not supported") diff --git a/gobject/tests/genmarshal.py b/gobject/tests/genmarshal.py index e2ecf7424..262c713cb 100644 --- a/gobject/tests/genmarshal.py +++ b/gobject/tests/genmarshal.py @@ -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 " diff --git a/gobject/tests/gobject-query.py b/gobject/tests/gobject-query.py index aab03be77..048de8d21 100644 --- a/gobject/tests/gobject-query.py +++ b/gobject/tests/gobject-query.py @@ -20,19 +20,13 @@ """Integration tests for gobject-query utility.""" -import collections -import os -import shutil -import subprocess import unittest import taptestrunner +import testprogramrunner -Result = collections.namedtuple("Result", ("info", "out", "err")) - - -class TestGobjectQuery(unittest.TestCase): +class TestGobjectQuery(testprogramrunner.TestProgramRunner): """Integration test for running gobject-query. This can be run when installed or uninstalled. When uninstalled, it @@ -42,44 +36,10 @@ class TestGobjectQuery(unittest.TestCase): handling of command line arguments, and its exit statuses. """ - def setUp(self): - self.timeout_seconds = 10 # seconds per test - if "G_TEST_BUILDDIR" in os.environ: - self.__gobject_query = os.path.join( - os.environ["G_TEST_BUILDDIR"], "..", "gobject-query" - ) - else: - self.__gobject_query = shutil.which("gobject-query") - print("gobject-query:", self.__gobject_query) + PROGRAM_NAME = "gobject-query" def runGobjectQuery(self, *args): - argv = [self.__gobject_query] - 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, - text=True, - encoding="utf-8", - ) - 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) def test_help(self): """Test the --help argument.""" diff --git a/gobject/tests/meson.build b/gobject/tests/meson.build index f59fa72aa..933461f5a 100644 --- a/gobject/tests/meson.build +++ b/gobject/tests/meson.build @@ -161,12 +161,17 @@ if cc.get_id() != 'msvc' endif python_tests = { - 'genmarshal.py' : {}, + 'genmarshal.py' : { + 'env': {'_G_TEST_PROGRAM_RUNNER_PATH': fs.parent(glib_genmarshal.full_path())}, + }, 'gobject-query.py' : { 'depends' : gobject_query, + 'env': {'_G_TEST_PROGRAM_RUNNER_PATH': fs.parent(gobject_query.full_path())}, 'can_fail' : host_system == 'windows', }, - 'mkenums.py' : {}, + 'mkenums.py' : { + 'env': {'_G_TEST_PROGRAM_RUNNER_PATH': fs.parent(glib_mkenums.full_path())}, + }, } test_env = environment() @@ -238,13 +243,18 @@ foreach test_name, extra_args : python_tests suite += 'failing' endif + local_test_env = python_test_env + foreach var, value : extra_args.get('env', {}) + local_test_env.set(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, suite: suite, ) diff --git a/gobject/tests/mkenums.py b/gobject/tests/mkenums.py index 8e9273090..98004bef8 100644 --- a/gobject/tests/mkenums.py +++ b/gobject/tests/mkenums.py @@ -22,22 +22,16 @@ """Integration tests for glib-mkenums utility.""" -import collections import os -import shutil -import subprocess -import sys import tempfile import textwrap import unittest import taptestrunner +import testprogramrunner -Result = collections.namedtuple("Result", ("info", "out", "err", "subs")) - - -class TestMkenums(unittest.TestCase): +class TestMkenums(testprogramrunner.TestProgramRunner): """Integration test for running glib-mkenums. This can be run when installed or uninstalled. When uninstalled, it @@ -50,27 +44,14 @@ class TestMkenums(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 = "" + PROGRAM_NAME = "glib-mkenums" + PROGRAM_TYPE = testprogramrunner.ProgramType.INTERPRETED + rspfile = False 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.__mkenums = os.path.join( - os.environ["G_TEST_BUILDDIR"], "..", "glib-mkenums" - ) - else: - self.__mkenums = shutil.which("glib-mkenums") - print("rspfile: {}, mkenums:".format(self.rspfile), self.__mkenums) - - def tearDown(self): - os.chdir(self.cwd) - self.tmpdir.cleanup() + super().setUp() + print("rspfile: {}".format(self.rspfile)) def _write_rspfile(self, argv): import shlex @@ -85,39 +66,17 @@ class TestMkenums(unittest.TestCase): return f.name def runMkenums(self, *args): + argv = list(args) + if self.rspfile: rspfile = self._write_rspfile(args) - args = ["@" + rspfile] - argv = [self.__mkenums] + argv = ["@" + rspfile] - # 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(argv) + def _getSubs(self): # Known substitutions for standard boilerplate - subs = { + return { "standard_top_comment": "This file is generated by glib-mkenums, do not modify " "it. This code is licensed under the same license as the " "containing project. Note that it links to GLib, so must " @@ -125,11 +84,6 @@ class TestMkenums(unittest.TestCase): "standard_bottom_comment": "Generated data ends here", } - result = Result(info, out, err, subs) - - print("Output:", result.out) - return result - def runMkenumsWithTemplate(self, template_contents, *args): with tempfile.NamedTemporaryFile( dir=self.tmpdir.name, suffix=".template", delete=False diff --git a/tests/lib/testprogramrunner.py b/tests/lib/testprogramrunner.py new file mode 100644 index 000000000..64be095a0 --- /dev/null +++ b/tests/lib/testprogramrunner.py @@ -0,0 +1,153 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# +# Copyright © 2018 Endless Mobile, Inc. +# Copyright © 2025 Canonical Ltd. +# +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301 USA + +"""Integration tests for GLib utilities.""" + +import os +import shutil +import subprocess +import tempfile +import unittest +import sys + +from dataclasses import dataclass +from enum import Enum + + +class ProgramType(Enum): + """Enum to define the kind of tool to use""" + + NATIVE = 1 + INTERPRETED = 2 + + +@dataclass +class Result: + """Class for keeping track of the executable result.""" + + info: subprocess.CompletedProcess + out: str + err: str + subs: dict + + +class TestProgramRunner(unittest.TestCase): + """Integration test for running glib-based tools. + + This can be run when installed or uninstalled. When uninstalled, it + requires G_TEST_BUILDDIR or _G_TEST_PROGRAM_RUNNER_PATH to be set. + """ + + PROGRAM_NAME: str = None + PROGRAM_TYPE: ProgramType = ProgramType.NATIVE + INTERPRETER: str = None + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.assertTrue(cls.PROGRAM_NAME, "class PROGRAM_NAME must be set") + + ext = "" + if cls.PROGRAM_TYPE == ProgramType.NATIVE and os.name == "nt": + ext = ".exe" + + cls._program_name = f"{cls.PROGRAM_NAME}{ext}" + + if "_G_TEST_PROGRAM_RUNNER_PATH" in os.environ: + cls.__program = os.path.join( + os.environ["_G_TEST_PROGRAM_RUNNER_PATH"], cls._program_name + ) + elif "G_TEST_BUILDDIR" in os.environ: + cls.__program = os.path.join( + os.environ["G_TEST_BUILDDIR"], cls._program_name + ) + else: + cls.__program = os.path.join(os.path.dirname(__file__), cls._program_name) + if not os.path.exists(cls.__program): + cls.__program = shutil.which(cls._program_name) + + def setUp(self): + print(f"{self.PROGRAM_NAME}: {self.__program}") + self.assertTrue(os.path.exists(self.__program)) + + self.tmpdir = tempfile.TemporaryDirectory() + self.addCleanup(self.tmpdir.cleanup) + old_cwd = os.getcwd() + self.addCleanup(os.chdir, old_cwd) + os.chdir(self.tmpdir.name) + print("tmpdir:", self.tmpdir.name) + + def runTestProgram( + self, *args, should_fail=False, timeout_seconds=10, wrapper_args=[] + ) -> Result: + argv = [self.__program] + + argv.extend(*args) + + # shebang lines are not supported on native + # Windows consoles + if self.PROGRAM_TYPE == ProgramType.INTERPRETED and os.name == "nt": + argv.insert(0, self.INTERPRETER if self.INTERPRETER else sys.executable) + + argv = wrapper_args + argv + + env = os.environ.copy() + env["LC_ALL"] = "C.UTF-8" + env["G_DEBUG"] = "fatal-warnings" + + print("Running:", argv) + + # We want to ensure consistent line endings... + info = subprocess.run( + argv, + timeout=timeout_seconds, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env=env, + universal_newlines=True, + text=True, + encoding="utf-8", + check=False, + ) + + result = Result( + info=info, + out=info.stdout.strip(), + err=info.stderr.strip(), + subs=self._getSubs(), + ) + + print("Return code:", result.info.returncode) + print("Output:\n", result.out) + print("Error:\n", result.err) + + if should_fail: + with self.assertRaises(subprocess.CalledProcessError): + info.check_returncode() + else: + info.check_returncode() + + return result + + def _getSubs(self) -> dict: + return {} diff --git a/tests/meson.build b/tests/meson.build index 376925863..e5518c64d 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -36,10 +36,15 @@ test( # TAP test runner for Python tests if installed_tests_enabled - install_data( - files(fs.relative_to( + lib_path = fs.relative_to( meson.project_source_root() / python_test_libraries_path, - meson.current_source_dir()) / 'taptestrunner.py'), + meson.current_source_dir()) + + install_data( + files( + lib_path / 'taptestrunner.py', + lib_path / 'testprogramrunner.py', + ), install_dir: installed_tests_execdir, install_tag: 'tests', )