Merge branch 'empty-argv' into 'main'

Various minor fixes for empty argv handling

See merge request GNOME/glib!2466
This commit is contained in:
Philip Withnall
2022-02-20 09:36:41 +00:00
14 changed files with 120 additions and 25 deletions

View File

@@ -102,6 +102,24 @@ main (int argc,
argc -= 1;
argv[argc] = NULL;
if (g_strcmp0 (argv1, "init-null-argv0") == 0)
{
int test_argc = 0;
char *test_argva[1] = { NULL };
char **test_argv = test_argva;
/* Test that `g_test_init()` can handle being called with an empty argv
* and argc == 0. While this isnt recommended, it is possible for another
* process to use execve() to call a gtest process this way, so wed
* better handle it gracefully.
*
* This test cant be run after `g_test_init()` has been called normally,
* as it isnt allowed to be called more than once in a process. */
g_test_init (&test_argc, &test_argv, NULL);
return 0;
}
g_test_init (&argc, &argv, NULL);
g_test_set_nonfatal_assertions ();

View File

@@ -1585,6 +1585,40 @@ test_tap_summary (void)
g_ptr_array_unref (argv);
}
static void
test_init_no_argv0 (void)
{
const char *testing_helper;
GPtrArray *argv;
GError *error = NULL;
int status;
gchar *output;
g_test_summary ("Test that g_test_init() can be called safely with argc == 0.");
testing_helper = g_test_get_filename (G_TEST_BUILT, "testing-helper" EXEEXT, NULL);
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (char *) testing_helper);
g_ptr_array_add (argv, "init-null-argv0");
g_ptr_array_add (argv, NULL);
/* This has to be spawned manually and cant be run with g_test_subprocess()
* because the test helper cant be run after `g_test_init()` has been called
* in the process. */
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_wait_status (status, &error);
g_assert_no_error (error);
g_assert_nonnull (strstr (output, "# random seed:"));
g_free (output);
g_ptr_array_unref (argv);
}
int
main (int argc,
char *argv[])
@@ -1682,6 +1716,8 @@ main (int argc,
g_test_add_func ("/tap", test_tap);
g_test_add_func ("/tap/summary", test_tap_summary);
g_test_add_func ("/init/no_argv0", test_init_no_argv0);
ret = g_test_run ();
/* We can't test for https://gitlab.gnome.org/GNOME/glib/-/issues/2563