Merge branch 'non-fatal-assertions-option' into 'main'

test utils: Make nonfatal assertions an option

See merge request GNOME/glib!4384
This commit is contained in:
Philip Withnall 2024-11-08 15:26:48 +00:00
commit a162d7a9e9
4 changed files with 68 additions and 3 deletions

View File

@ -1637,11 +1637,14 @@ test_rm_isolate_dirs (void)
*
* Options which can be passed to @... are:
*
* - `"no_g_set_prgname"`: Causes g_test_init() to not call g_set_prgname().
* - %G_TEST_OPTION_ISOLATE_DIRS: Creates a unique temporary directory for each
* - `G_TEST_OPTION_NO_PRGNAME`: Causes g_test_init() to not call
* [func@GLib.set_prgname]. Since. 2.84
* - `G_TEST_OPTION_ISOLATE_DIRS`: Creates a unique temporary directory for each
* unit test and uses g_set_user_dirs() to set XDG directories to point into
* that temporary directory for the duration of the unit test. See the
* documentation for %G_TEST_OPTION_ISOLATE_DIRS.
* - `G_TEST_OPTION_NONFATAL_ASSERTIONS`: This has the same effect as
* [func@GLib.test_set_nonfatal_assertions]. Since 2.84
*
* Since 2.58, if tests are compiled with `G_DISABLE_ASSERT` defined,
* g_test_init() will print an error and exit. This is to prevent no-op tests
@ -1696,10 +1699,12 @@ void
va_start (args, argv);
while ((option = va_arg (args, char *)))
{
if (g_strcmp0 (option, "no_g_set_prgname") == 0)
if (g_strcmp0 (option, G_TEST_OPTION_NO_PRGNAME) == 0)
no_g_set_prgname = TRUE;
else if (g_strcmp0 (option, G_TEST_OPTION_ISOLATE_DIRS) == 0)
test_isolate_dirs = TRUE;
else if (g_strcmp0 (option, G_TEST_OPTION_NONFATAL_ASSERTIONS) == 0)
test_nonfatal_assertions = TRUE;
}
va_end (args);

View File

@ -302,6 +302,8 @@ void g_test_init (int *argc,
/**
* G_TEST_OPTION_ISOLATE_DIRS:
*
* A value that can be passed as an option to [func@GLib.test_init].
*
* Creates a unique temporary directory for each unit test and uses
* g_set_user_dirs() to set XDG directories to point into subdirectories of it
* for the duration of the unit test. The directory tree is cleaned up after the
@ -330,6 +332,29 @@ void g_test_init (int *argc,
*/
#define G_TEST_OPTION_ISOLATE_DIRS "isolate_dirs"
/**
* G_TEST_OPTION_NO_PRGNAME:
*
* A value that can be passed as an option to [func@GLib.test_init].
*
* If this option is given, [func@GLib.test_init] will not call [func@GLib.set_prgname].
*
* Since: 2.84
*/
#define G_TEST_OPTION_NO_PRGNAME "no_g_set_prgname"
/**
* G_TEST_OPTION_NONFATAL_ASSERTIONS:
*
* A value that can be passed as an option to [func@GLib.test_init].
*
* If this option is given, assertions will not abort the process, but
* call [func@GLib.test_fail]. Equivalent to [func@GLib.test_set_nonfatal_assertions].
*
* Since: 2.84
*/
#define G_TEST_OPTION_NONFATAL_ASSERTIONS "nonfatal-assertions"
/* While we discourage its use, g_assert() is often used in unit tests
* (especially in legacy code). g_assert_*() should really be used instead.
* g_assert() can be disabled at client program compile time, which can render

View File

@ -159,6 +159,9 @@ glib_tests = {
'extra_programs' : ['testing-helper'],
'c_args' : cc.get_id() == 'gcc' ? ['-Werror=sign-conversion'] : [],
},
'testing-nonfatal' : {
'protocol' : 'exitcode'
},
'test-printf' : {},
'thread' : {},
'thread-deprecated' : {},

View File

@ -0,0 +1,32 @@
/* Copyright (C) 2024 Red Hat, Inc.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, G_TEST_OPTION_NONFATAL_ASSERTIONS, NULL);
g_assert_cmpstr ("We can survive this!", ==, "Or maybe not?");
if (!g_test_failed ())
return 1;
return 0;
}