From b8d751af3e611d4c1ee9e2ba511f1725a7ddcd83 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 3 Aug 2018 15:09:28 +0100 Subject: [PATCH] testing: Exercise TAP output (Tweaked by Philip Withnall to fix some minor leaks, code formatting, and add a test comment.) Signed-off-by: Simon McVittie --- glib/tests/Makefile.am | 1 + glib/tests/meson.build | 7 ++ glib/tests/testing-helper.c | 97 +++++++++++++++++++++++++++ glib/tests/testing.c | 126 ++++++++++++++++++++++++++++++++++++ 4 files changed, 231 insertions(+) create mode 100644 glib/tests/testing-helper.c diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am index 172d16607..3f1050dc9 100644 --- a/glib/tests/Makefile.am +++ b/glib/tests/Makefile.am @@ -38,6 +38,7 @@ dist_test_extra_scripts = \ test_extra_programs = \ test-spawn-echo \ + testing-helper \ $(NULL) test_programs = \ diff --git a/glib/tests/meson.build b/glib/tests/meson.build index f5c0fbecb..0af71b072 100644 --- a/glib/tests/meson.build +++ b/glib/tests/meson.build @@ -183,6 +183,13 @@ executable('test-spawn-echo', 'test-spawn-echo.c', install: installed_tests_enabled, ) +executable('testing-helper', 'testing-helper.c', + c_args : test_cargs, + dependencies : test_deps, + install_dir: installed_tests_execdir, + install: installed_tests_enabled, +) + # some testing of gtester functionality if not meson.is_cross_build() and host_system != 'windows' xmllint = find_program('xmllint', required: false) diff --git a/glib/tests/testing-helper.c b/glib/tests/testing-helper.c new file mode 100644 index 000000000..43127e8dd --- /dev/null +++ b/glib/tests/testing-helper.c @@ -0,0 +1,97 @@ +/* + * Copyright 2018 Collabora Ltd. + * + * 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, see + * . + */ + +#include + +static void +test_pass (void) +{ +} + +static void +test_skip (void) +{ + g_test_skip ("not enough tea"); +} + +static void +test_fail (void) +{ + g_test_fail (); +} + +static void +test_incomplete (void) +{ + g_test_incomplete ("mind reading not implemented yet"); +} + +int +main (int argc, + char *argv[]) +{ + char *argv1; + + g_return_val_if_fail (argc > 1, 1); + argv1 = argv[1]; + + if (argc > 2) + memmove (&argv[1], &argv[2], (argc - 2) * sizeof (char *)); + + argc -= 1; + argv[argc] = NULL; + + g_test_init (&argc, &argv, NULL); + g_test_set_nonfatal_assertions (); + + if (g_strcmp0 (argv1, "pass") == 0) + { + g_test_add_func ("/pass", test_pass); + } + else if (g_strcmp0 (argv1, "skip") == 0) + { + g_test_add_func ("/skip", test_skip); + } + else if (g_strcmp0 (argv1, "incomplete") == 0) + { + g_test_add_func ("/incomplete", test_incomplete); + } + else if (g_strcmp0 (argv1, "fail") == 0) + { + g_test_add_func ("/fail", test_fail); + } + else if (g_strcmp0 (argv1, "all-non-failures") == 0) + { + g_test_add_func ("/pass", test_pass); + g_test_add_func ("/skip", test_skip); + g_test_add_func ("/incomplete", test_incomplete); + } + else if (g_strcmp0 (argv1, "all") == 0) + { + g_test_add_func ("/pass", test_pass); + g_test_add_func ("/skip", test_skip); + g_test_add_func ("/incomplete", test_incomplete); + g_test_add_func ("/fail", test_fail); + } + else + { + g_assert_not_reached (); + } + + return g_test_run (); +} diff --git a/glib/tests/testing.c b/glib/tests/testing.c index 64c5244f9..716516e2c 100644 --- a/glib/tests/testing.c +++ b/glib/tests/testing.c @@ -20,6 +20,8 @@ * if advised of the possibility of such damage. */ +#include "config.h" + /* We want to distinguish between messages originating from libglib * and messages originating from this program. */ @@ -890,6 +892,128 @@ test_combining (void) g_ptr_array_unref (argv); } +/* Test the TAP output when a test suite is run with --tap. */ +static void +test_tap (void) +{ + const char *testing_helper; + GPtrArray *argv; + GError *error = NULL; + int status; + gchar *output; + + testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL); + + g_test_message ("pass"); + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "pass"); + g_ptr_array_add (argv, "--tap"); + g_ptr_array_add (argv, NULL); + + g_spawn_sync (NULL, (char **) argv->pdata, NULL, + G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, &output, NULL, &status, + &error); + g_assert_no_error (error); + + g_spawn_check_exit_status (status, &error); + g_assert_no_error (error); + g_assert_nonnull (strstr (output, "\nok 1 /pass\n")); + g_free (output); + g_ptr_array_unref (argv); + + g_test_message ("skip"); + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "skip"); + g_ptr_array_add (argv, "--tap"); + g_ptr_array_add (argv, NULL); + + g_spawn_sync (NULL, (char **) argv->pdata, NULL, + G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, &output, NULL, &status, + &error); + g_assert_no_error (error); + + g_spawn_check_exit_status (status, &error); + g_assert_no_error (error); + g_assert_nonnull (strstr (output, "\nok 1 /skip # SKIP not enough tea\n")); + g_free (output); + g_ptr_array_unref (argv); + + g_test_message ("incomplete"); + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "incomplete"); + g_ptr_array_add (argv, "--tap"); + g_ptr_array_add (argv, NULL); + + g_spawn_sync (NULL, (char **) argv->pdata, NULL, + G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, &output, NULL, &status, + &error); + g_assert_no_error (error); + + g_spawn_check_exit_status (status, &error); + g_assert_no_error (error); + g_assert_nonnull (strstr (output, "\nnot ok 1 /incomplete # TODO mind reading not implemented yet\n")); + g_free (output); + g_ptr_array_unref (argv); + + g_test_message ("fail"); + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "fail"); + g_ptr_array_add (argv, "--tap"); + g_ptr_array_add (argv, NULL); + + g_spawn_sync (NULL, (char **) argv->pdata, NULL, + G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, &output, NULL, &status, + &error); + g_assert_no_error (error); + + g_spawn_check_exit_status (status, &error); + g_assert_error (error, G_SPAWN_EXIT_ERROR, 1); + g_assert_nonnull (strstr (output, "\nnot ok 1 /fail\n")); + g_free (output); + g_ptr_array_unref (argv); + + g_test_message ("all"); + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "all"); + g_ptr_array_add (argv, "--tap"); + g_ptr_array_add (argv, NULL); + + g_spawn_sync (NULL, (char **) argv->pdata, NULL, + G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, NULL, NULL, &status, + &error); + g_assert_error (error, G_SPAWN_EXIT_ERROR, 1); + g_clear_error (&error); + g_ptr_array_unref (argv); + + g_test_message ("all-non-failures"); + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (char *) testing_helper); + g_ptr_array_add (argv, "all-non-failures"); + g_ptr_array_add (argv, "--tap"); + g_ptr_array_add (argv, NULL); + + g_spawn_sync (NULL, (char **) argv->pdata, NULL, + G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, NULL, NULL, &status, + &error); + g_assert_no_error (error); + + g_spawn_check_exit_status (status, &error); + g_assert_no_error (error); + + g_ptr_array_unref (argv); +} + int main (int argc, char *argv[]) @@ -964,5 +1088,7 @@ main (int argc, g_test_add_func ("/misc/incomplete", test_incomplete); g_test_add_func ("/misc/timeout", test_subprocess_timed_out); + g_test_add_func ("/tap", test_tap); + return g_test_run(); }