diff --git a/tests/helper.c b/tests/helper.c new file mode 100644 index 0000000000000000000000000000000000000000..fa4264551205376c016ca75ac7f0ebbb88d25a73 --- /dev/null +++ b/tests/helper.c @@ -0,0 +1,75 @@ +/* + * Helper for misc smaller tests that need to try things in a separate + * process. + * + * Copyright © 2020 Collabora Ltd. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <glib.h> +#include <glib/gstdio.h> + +#include "glib-backports.h" +#include "libglnx/libglnx.h" + +#include "utils.h" + +static void +try_divert_stdout (void) +{ + g_autoptr(GError) error = NULL; + g_autoptr(FILE) original_stdout = NULL; + + original_stdout = pv_divert_stdout_to_stderr (&error); + g_assert_no_error (error); + + g_setenv ("G_MESSAGES_DEBUG", "all", TRUE); + + g_print ("printed-with-g-print\n"); + g_log ("tests-helper", G_LOG_LEVEL_DEBUG, "logged-as-debug"); + g_log (NULL, G_LOG_LEVEL_INFO, "logged-as-info"); + fprintf (original_stdout, "printed-to-original-stdout"); + + fflush (stdout); + fflush (original_stdout); +} + +int +main (int argc, + char **argv) +{ + if (argc < 2) + g_error ("Usage: %s MODE [ARGUMENTS...]", argv[0]); + + if (g_strcmp0 (argv[1], "divert-stdout") == 0) + { + try_divert_stdout (); + } + else + { + g_error ("Unknown argv[1]: %s", argv[1]); + } + + return 0; +} + diff --git a/tests/meson.build b/tests/meson.build index b122e5e4850b367b3d7c400aa9bbfcd103eb9e03..298889133f1afe22ab9f3da2e4434bab6c069719 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -39,6 +39,7 @@ tests = [ 'pyflakes.sh', 'shellcheck.sh', 'test-locale-gen.sh', + 'utils.py', ] compiled_tests = [ @@ -106,6 +107,7 @@ endforeach helpers = [ 'cheap-copy', 'elf-get-soname', + 'helper', ] foreach helper : helpers diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..99c3cc4cca57995dad933fe2b83dd6c3dc9a740c --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +# Copyright 2020 Collabora Ltd. +# +# SPDX-License-Identifier: MIT + +import os +import subprocess +import sys +import tempfile + + +try: + import typing + typing # placate pyflakes +except ImportError: + pass + +from testutils import ( + BaseTest, + run_subprocess, + test_main, +) + + +class TestUtils(BaseTest): + def setUp(self) -> None: + super().setUp() + self.helper = os.path.join(self.G_TEST_BUILDDIR, 'test-helper') + + def test_divert_stdout(self) -> None: + completed = run_subprocess( + [self.helper, 'divert-stdout'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + self.assertRegex( + completed.stderr.decode('utf-8'), + ( + r'(?s)' # make .* match newlines + r'printed-with-g-print.*' + r'logged-as-debug.*' + r'logged-as-info' + ) + ) + self.assertEqual(completed.stdout, b'printed-to-original-stdout') + self.assertEqual(completed.returncode, 0) + + def tearDown(self) -> None: + super().tearDown() + + +if __name__ == '__main__': + assert sys.version_info >= (3, 5), \ + 'Python 3.5+ is required (configure with -Dpython=python3.5 ' \ + 'if necessary)' + + test_main() + +# vi: set sw=4 sts=4 et: