gtestutils: add "options" to g_test_init(), make option-argv0 use gtester
authorDan Winship <danw@gnome.org>
Sun, 10 Nov 2013 18:23:15 +0000 (13:23 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 23 Nov 2013 05:43:45 +0000 (00:43 -0500)
Declare that the previously-unused "..." argument to g_test_init() is
actually a NULL-terminated list of strings indicating testing options,
and add an option "no_g_set_prgname", which keeps g_test_init() from
calling g_set_prgname(). Then we can port glib/tests/option-argv0 to
use gtester, by passing that option.

https://bugzilla.gnome.org/show_bug.cgi?id=711796

glib/gtestutils.c
glib/gtestutils.h
glib/tests/option-argv0.c

index fa820db..34cee4d 100644 (file)
@@ -627,6 +627,7 @@ static GTestConfig mutable_test_config_vars = {
   TRUE,         /* test_undefined */
 };
 const GTestConfig * const g_test_config_vars = &mutable_test_config_vars;
+static gboolean  no_g_set_prgname = FALSE;
 
 /* --- functions --- */
 const char*
@@ -1003,7 +1004,9 @@ parse_args (gint    *argc_p,
  *        Changed if any arguments were handled.
  * @argv: Address of the @argv parameter of main().
  *        Any parameters understood by g_test_init() stripped before return.
- * @...: Reserved for future extension. Currently, you must pass %NULL.
+ * @...: %NULL-terminated list of special options. Currently the only
+ *       defined option is <literal>"no_g_set_prgname"</literal>, which
+ *       will cause g_test_init() to not call g_set_prgname().
  *
  * Initialize the GLib testing framework, e.g. by seeding the
  * test random number generator, the name for g_get_prgname()
@@ -1094,9 +1097,10 @@ g_test_init (int    *argc,
 {
   static char seedstr[4 + 4 * 8 + 1];
   va_list args;
-  gpointer vararg1;
+  gpointer option;
   /* make warnings and criticals fatal for all test programs */
   GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
+
   fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
   g_log_set_always_fatal (fatal_mask);
   /* check caller args */
@@ -1106,9 +1110,12 @@ g_test_init (int    *argc,
   mutable_test_config_vars.test_initialized = TRUE;
 
   va_start (args, argv);
-  vararg1 = va_arg (args, gpointer); /* reserved for future extensions */
+  while ((option = va_arg (args, char *)))
+    {
+      if (g_strcmp0 (option, "no_g_set_prgname") == 0)
+        no_g_set_prgname = TRUE;
+    }
   va_end (args);
-  g_return_if_fail (vararg1 == NULL);
 
   /* setup random seed string */
   g_snprintf (seedstr, sizeof (seedstr), "R02S%08x%08x%08x%08x", g_random_int(), g_random_int(), g_random_int(), g_random_int());
@@ -1116,7 +1123,8 @@ g_test_init (int    *argc,
 
   /* parse args, sets up mode, changes seed, etc. */
   parse_args (argc, argv);
-  if (!g_get_prgname())
+
+  if (!g_get_prgname() && !no_g_set_prgname)
     g_set_prgname ((*argv)[0]);
 
   /* verify GRand reliability, needed for reliable seeds */
index 889df44..9700f38 100644 (file)
@@ -107,7 +107,7 @@ void    g_test_maximized_result         (double          maximized_quantity,
 GLIB_AVAILABLE_IN_ALL
 void    g_test_init                     (int            *argc,
                                          char         ***argv,
-                                         ...);
+                                         ...) G_GNUC_NULL_TERMINATED;
 /* query testing framework config */
 #define g_test_initialized()            (g_test_config_vars->test_initialized)
 #define g_test_quick()                  (g_test_config_vars->test_quick)
index c7bbc7c..8ae00ca 100644 (file)
@@ -54,14 +54,11 @@ int
 main (int   argc,
       char *argv[])
 {
-  /* Note - we can't actually use g_test_* because g_test_init mutates
-   * g_get_prgname() which is exactly what we wanted to test =/
-   */
+  g_test_init (&argc, &argv, "no_g_set_prgname", NULL);
+
 #if defined __linux || defined __OpenBSD__
-  g_print ("/option/argv0: ");
-  test_platform_argv0 ();
-  g_print ("OK\n");
+  g_test_add_func ("/option/argv0", test_platform_argv0);
 #endif
 
-  return 0;
+  return g_test_run ();
 }