Imported Upstream version 2.74.3
[platform/upstream/glib.git] / glib / gtester.c
1 /* GLib testing framework runner
2  * Copyright (C) 2007 Sven Herzberg
3  * Copyright (C) 2007 Tim Janik
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20 #include "config.h"
21
22 #include <glib.h>
23 #include <glib-unix.h>
24 #include <gstdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/wait.h>
30 #include <errno.h>
31 #include <signal.h>
32
33 /* the read buffer size in bytes */
34 #define READ_BUFFER_SIZE 4096
35
36 /* --- prototypes --- */
37 static int      main_selftest   (int    argc,
38                                  char **argv);
39 static void     parse_args      (gint           *argc_p,
40                                  gchar        ***argv_p);
41
42 /* --- variables --- */
43 static GIOChannel  *ioc_report = NULL;
44 static gboolean     gtester_quiet = FALSE;
45 static gboolean     gtester_verbose = FALSE;
46 static gboolean     gtester_list_tests = FALSE;
47 static gboolean     gtester_selftest = FALSE;
48 static gboolean     gtester_ignore_deprecation = FALSE;
49 static gboolean     subtest_running = FALSE;
50 static gint         subtest_exitstatus = 0;
51 static gboolean     subtest_io_pending = FALSE;
52 static gboolean     subtest_quiet = TRUE;
53 static gboolean     subtest_verbose = FALSE;
54 static gboolean     subtest_mode_fatal = TRUE;
55 static gboolean     subtest_mode_perf = FALSE;
56 static gboolean     subtest_mode_quick = TRUE;
57 static gboolean     subtest_mode_undefined = TRUE;
58 static const gchar *subtest_seedstr = NULL;
59 static gchar       *subtest_last_seed = NULL;
60 static GSList      *subtest_paths = NULL;
61 static GSList      *skipped_paths = NULL;
62 static GSList      *subtest_args = NULL;
63 static gboolean     testcase_open = FALSE;
64 static guint        testcase_count = 0;
65 static guint        testcase_fail_count = 0;
66 static const gchar *output_filename = NULL;
67 static guint        log_indent = 0;
68 static gint         log_fd = -1;
69
70 /* --- functions --- */
71 static const char*
72 sindent (guint n)
73 {
74   static const char spaces[] = "                                                                                                    ";
75   gsize l = sizeof (spaces) - 1;
76   n = MIN (n, l);
77   return spaces + l - n;
78 }
79
80 static void G_GNUC_PRINTF (1, 2)
81 test_log_printfe (const char *format,
82                   ...)
83 {
84   char *result;
85   int r;
86   va_list args;
87   va_start (args, format);
88   result = g_markup_vprintf_escaped (format, args);
89   va_end (args);
90   do
91     r = write (log_fd, result, strlen (result));
92   while (r < 0 && errno == EINTR);
93   g_free (result);
94 }
95
96 static void
97 terminate (void)
98 {
99   kill (getpid(), SIGTERM);
100   g_abort();
101 }
102
103 static void
104 testcase_close (long double duration,
105                 gint        exit_status,
106                 guint       n_forks)
107 {
108   gboolean success;
109
110   g_return_if_fail (testcase_open > 0);
111   test_log_printfe ("%s<duration>%.6Lf</duration>\n", sindent (log_indent), duration);
112   success = exit_status == G_TEST_RUN_SUCCESS || exit_status == G_TEST_RUN_SKIPPED;
113   test_log_printfe ("%s<status exit-status=\"%d\" n-forks=\"%d\" result=\"%s\"/>\n",
114                     sindent (log_indent), exit_status, n_forks,
115                     success ? "success" : "failed");
116   log_indent -= 2;
117   test_log_printfe ("%s</testcase>\n", sindent (log_indent));
118   testcase_open--;
119   if (gtester_verbose)
120     {
121       switch (exit_status)
122         {
123         case G_TEST_RUN_SUCCESS:
124           g_print ("OK\n");
125           break;
126         case G_TEST_RUN_SKIPPED:
127           g_print ("SKIP\n");
128           break;
129         default:
130           g_print ("FAIL\n");
131           break;
132         }
133     }
134   if (!success && subtest_last_seed)
135     g_print ("GTester: last random seed: %s\n", subtest_last_seed);
136   if (!success)
137     testcase_fail_count += 1;
138   if (subtest_mode_fatal && !success)
139     terminate();
140 }
141
142 static void
143 test_log_msg (GTestLogMsg *msg)
144 {
145   switch (msg->log_type)
146     {
147       guint i;
148       gchar **strv;
149     case G_TEST_LOG_NONE:
150     case G_TEST_LOG_START_SUITE:
151     case G_TEST_LOG_STOP_SUITE:
152       break;
153     case G_TEST_LOG_ERROR:
154       strv = g_strsplit (msg->strings[0], "\n", -1);
155       for (i = 0; strv[i]; i++)
156         test_log_printfe ("%s<error>%s</error>\n", sindent (log_indent), strv[i]);
157       g_strfreev (strv);
158       break;
159     case G_TEST_LOG_START_BINARY:
160       test_log_printfe ("%s<binary file=\"%s\"/>\n", sindent (log_indent), msg->strings[0]);
161       subtest_last_seed = g_strdup (msg->strings[1]);
162       test_log_printfe ("%s<random-seed>%s</random-seed>\n", sindent (log_indent), subtest_last_seed);
163       break;
164     case G_TEST_LOG_LIST_CASE:
165       g_print ("%s\n", msg->strings[0]);
166       break;
167     case G_TEST_LOG_START_CASE:
168       testcase_count++;
169       if (gtester_verbose)
170         {
171           gchar *sc = g_strconcat (msg->strings[0], ":", NULL);
172           gchar *sleft = g_strdup_printf ("%-68s", sc);
173           g_free (sc);
174           g_print ("%70s ", sleft);
175           g_free (sleft);
176         }
177       g_return_if_fail (testcase_open == 0);
178       testcase_open++;
179       test_log_printfe ("%s<testcase path=\"%s\">\n", sindent (log_indent), msg->strings[0]);
180       log_indent += 2;
181       break;
182     case G_TEST_LOG_SKIP_CASE:
183       if (FALSE && gtester_verbose) /* enable to debug test case skipping logic */
184         {
185           gchar *sc = g_strconcat (msg->strings[0], ":", NULL);
186           gchar *sleft = g_strdup_printf ("%-68s", sc);
187           g_free (sc);
188           g_print ("%70s SKIPPED\n", sleft);
189           g_free (sleft);
190         }
191       test_log_printfe ("%s<testcase path=\"%s\" skipped=\"1\"/>\n", sindent (log_indent), msg->strings[0]);
192       break;
193     case G_TEST_LOG_STOP_CASE:
194       testcase_close (msg->nums[2], (int) msg->nums[0], (int) msg->nums[1]);
195       break;
196     case G_TEST_LOG_MIN_RESULT:
197     case G_TEST_LOG_MAX_RESULT:
198       test_log_printfe ("%s<performance minimize=\"%d\" maximize=\"%d\" value=\"%.16Lg\">\n",
199                         sindent (log_indent), msg->log_type == G_TEST_LOG_MIN_RESULT, msg->log_type == G_TEST_LOG_MAX_RESULT, msg->nums[0]);
200       test_log_printfe ("%s%s\n", sindent (log_indent + 2), msg->strings[0]);
201       test_log_printfe ("%s</performance>\n", sindent (log_indent));
202       break;
203     case G_TEST_LOG_MESSAGE:
204       test_log_printfe ("%s<message>\n%s\n%s</message>\n", sindent (log_indent), msg->strings[0], sindent (log_indent));
205       break;
206     }
207 }
208
209 static gboolean
210 child_report_cb (GIOChannel  *source,
211                  GIOCondition condition,
212                  gpointer     data)
213 {
214   GTestLogBuffer *tlb = data;
215   GIOStatus status = G_IO_STATUS_NORMAL;
216   gboolean first_read_eof = FALSE, first_read = TRUE;
217   gsize length = 0;
218   do
219     {
220       guint8 buffer[READ_BUFFER_SIZE];
221       GError *error = NULL;
222       status = g_io_channel_read_chars (source, (gchar*) buffer, sizeof (buffer), &length, &error);
223       if (first_read && (condition & G_IO_IN))
224         {
225           /* on some unixes (MacOS) we need to detect non-blocking fd EOF
226            * by an IO_IN select/poll followed by read()==0.
227            */
228           first_read_eof = length == 0;
229         }
230       first_read = FALSE;
231       if (length)
232         {
233           GTestLogMsg *msg;
234           g_test_log_buffer_push (tlb, length, buffer);
235           do
236             {
237               msg = g_test_log_buffer_pop (tlb);
238               if (msg)
239                 {
240                   test_log_msg (msg);
241                   g_test_log_msg_free (msg);
242                 }
243             }
244           while (msg);
245         }
246       g_clear_error (&error);
247       /* ignore the io channel status, which will report intermediate EOFs for non blocking fds */
248       (void) status;
249     }
250   while (length > 0);
251   /* g_print ("LASTIOSTATE: first_read_eof=%d condition=%d\n", first_read_eof, condition); */
252   if (first_read_eof || (condition & (G_IO_ERR | G_IO_HUP)))
253     {
254       /* if there's no data to read and select() reports an error or hangup,
255        * the fd must have been closed remotely
256        */
257       subtest_io_pending = FALSE;
258       return FALSE;
259     }
260   return TRUE; /* keep polling */
261 }
262
263 static void
264 child_watch_cb (GPid     pid,
265                 gint     status,
266                 gpointer data)
267 {
268   g_spawn_close_pid (pid);
269   if (WIFEXITED (status)) /* normal exit */
270     subtest_exitstatus = WEXITSTATUS (status);
271   else /* signal or core dump, etc */
272     subtest_exitstatus = 0xffffffff;
273   subtest_running = FALSE;
274 }
275
276 static gchar*
277 queue_gfree (GSList **slistp,
278              gchar   *string)
279 {
280   *slistp = g_slist_prepend (*slistp, string);
281   return string;
282 }
283
284 static void
285 unset_cloexec_fdp (gpointer fdp_data)
286 {
287   int r, *fdp = fdp_data;
288   do
289     r = fcntl (*fdp, F_SETFD, 0 /* FD_CLOEXEC */);
290   while (r < 0 && errno == EINTR);
291 }
292
293 static gboolean
294 launch_test_binary (const char *binary,
295                     guint       skip_tests)
296 {
297   GTestLogBuffer *tlb;
298   GSList *slist, *free_list = NULL;
299   GError *error = NULL;
300   int argc = 0;
301   const gchar **argv;
302   GPid pid = 0;
303   gint report_pipe[2] = { -1, -1 };
304   guint child_report_cb_id = 0;
305   gboolean loop_pending;
306   gint i = 0;
307
308   if (!g_unix_open_pipe (report_pipe, FD_CLOEXEC, &error))
309     {
310       if (subtest_mode_fatal)
311         g_error ("Failed to open pipe for test binary: %s: %s", binary, error->message);
312       else
313         g_warning ("Failed to open pipe for test binary: %s: %s", binary, error->message);
314       g_clear_error (&error);
315       return FALSE;
316     }
317
318   /* setup argc */
319   for (slist = subtest_args; slist; slist = slist->next)
320     argc++;
321   /* argc++; */
322   if (subtest_quiet)
323     argc++;
324   if (subtest_verbose)
325     argc++;
326   if (!subtest_mode_fatal)
327     argc++;
328   /* Either -m=quick or -m=slow is always appended. */
329   argc++;
330   if (subtest_mode_perf)
331     argc++;
332   if (!subtest_mode_undefined)
333     argc++;
334   if (gtester_list_tests)
335     argc++;
336   if (subtest_seedstr)
337     argc++;
338   argc++;
339   if (skip_tests)
340     argc++;
341   for (slist = subtest_paths; slist; slist = slist->next)
342     argc++;
343   for (slist = skipped_paths; slist; slist = slist->next)
344     argc++;
345
346   /* setup argv */
347   argv = g_malloc ((argc + 2) * sizeof(gchar *));
348   argv[i++] = binary;
349   for (slist = subtest_args; slist; slist = slist->next)
350     argv[i++] = (gchar*) slist->data;
351   /* argv[i++] = "--debug-log"; */
352   if (subtest_quiet)
353     argv[i++] = "--quiet";
354   if (subtest_verbose)
355     argv[i++] = "--verbose";
356   if (!subtest_mode_fatal)
357     argv[i++] = "--keep-going";
358   if (subtest_mode_quick)
359     argv[i++] = "-m=quick";
360   else
361     argv[i++] = "-m=slow";
362   if (subtest_mode_perf)
363     argv[i++] = "-m=perf";
364   if (!subtest_mode_undefined)
365     argv[i++] = "-m=no-undefined";
366   if (gtester_list_tests)
367     argv[i++] = "-l";
368   if (subtest_seedstr)
369     argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--seed=%s", subtest_seedstr));
370   argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestLogFD=%u", report_pipe[1]));
371   if (skip_tests)
372     argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestSkipCount=%u", skip_tests));
373   for (slist = subtest_paths; slist; slist = slist->next)
374     argv[i++] = queue_gfree (&free_list, g_strdup_printf ("-p=%s", (gchar*) slist->data));
375   for (slist = skipped_paths; slist; slist = slist->next)
376     argv[i++] = queue_gfree (&free_list, g_strdup_printf ("-s=%s", (gchar*) slist->data));
377   argv[i++] = NULL;
378
379   g_spawn_async_with_pipes (NULL, /* g_get_current_dir() */
380                             (gchar**) argv,
381                             NULL, /* envp */
382                             G_SPAWN_DO_NOT_REAP_CHILD, /* G_SPAWN_SEARCH_PATH */
383                             unset_cloexec_fdp, &report_pipe[1], /* pre-exec callback */
384                             &pid,
385                             NULL,       /* standard_input */
386                             NULL,       /* standard_output */
387                             NULL,       /* standard_error */
388                             &error);
389   g_slist_foreach (free_list, (void(*)(void*,void*)) g_free, NULL);
390   g_slist_free (free_list);
391   free_list = NULL;
392   close (report_pipe[1]);
393
394   if (!gtester_quiet)
395     g_print ("(pid=%lu)\n", (unsigned long) pid);
396
397   if (error)
398     {
399       close (report_pipe[0]);
400       if (subtest_mode_fatal)
401         g_error ("Failed to execute test binary: %s: %s", argv[0], error->message);
402       else
403         g_warning ("Failed to execute test binary: %s: %s", argv[0], error->message);
404       g_clear_error (&error);
405       g_free (argv);
406       return FALSE;
407     }
408   g_free (argv);
409
410   subtest_running = TRUE;
411   subtest_io_pending = TRUE;
412   tlb = g_test_log_buffer_new();
413   if (report_pipe[0] >= 0)
414     {
415       ioc_report = g_io_channel_unix_new (report_pipe[0]);
416       g_io_channel_set_flags (ioc_report, G_IO_FLAG_NONBLOCK, NULL);
417       g_io_channel_set_encoding (ioc_report, NULL, NULL);
418       g_io_channel_set_buffered (ioc_report, FALSE);
419       child_report_cb_id = g_io_add_watch_full (ioc_report, G_PRIORITY_DEFAULT - 1, G_IO_IN | G_IO_ERR | G_IO_HUP, child_report_cb, tlb, NULL);
420       g_io_channel_unref (ioc_report);
421     }
422   g_child_watch_add_full (G_PRIORITY_DEFAULT + 1, pid, child_watch_cb, NULL, NULL);
423
424   loop_pending = g_main_context_pending (NULL);
425   while (subtest_running ||     /* FALSE once child exits */
426          subtest_io_pending ||  /* FALSE once ioc_report closes */
427          loop_pending)          /* TRUE while idler, etc are running */
428     {
429       /* g_print ("LOOPSTATE: subtest_running=%d subtest_io_pending=%d\n", subtest_running, subtest_io_pending); */
430       /* check for unexpected hangs that are not signalled on report_pipe */
431       if (!subtest_running &&   /* child exited */
432           subtest_io_pending && /* no EOF detected on report_pipe */
433           !loop_pending)        /* no IO events pending however */
434         break;
435       g_main_context_iteration (NULL, TRUE);
436       loop_pending = g_main_context_pending (NULL);
437     }
438
439   if (subtest_io_pending)
440     g_source_remove (child_report_cb_id);
441
442   close (report_pipe[0]);
443   g_test_log_buffer_free (tlb);
444
445   return TRUE;
446 }
447
448 static void
449 launch_test (const char *binary)
450 {
451   gboolean success = TRUE;
452   GTimer *btimer = g_timer_new();
453   gboolean need_restart;
454
455   testcase_count = 0;
456   if (!gtester_quiet)
457     g_print ("TEST: %s... ", binary);
458
459  retry:
460   test_log_printfe ("%s<testbinary path=\"%s\">\n", sindent (log_indent), binary);
461   log_indent += 2;
462   g_timer_start (btimer);
463   subtest_exitstatus = 0;
464   success &= launch_test_binary (binary, testcase_count);
465   success &= subtest_exitstatus == 0;
466   need_restart = testcase_open != 0;
467   if (testcase_open)
468     testcase_close (0, -256, 0);
469   g_timer_stop (btimer);
470   test_log_printfe ("%s<duration>%.6f</duration>\n", sindent (log_indent), g_timer_elapsed (btimer, NULL));
471   log_indent -= 2;
472   test_log_printfe ("%s</testbinary>\n", sindent (log_indent));
473   g_free (subtest_last_seed);
474   subtest_last_seed = NULL;
475   if (need_restart)
476     {
477       /* restart test binary, skipping processed test cases */
478       goto retry;
479     }
480
481   /* count the inability to run a test as a failure */
482   if (!success && testcase_count == 0)
483     testcase_fail_count++;
484
485   if (!gtester_quiet)
486     g_print ("%s: %s\n", !success ? "FAIL" : "PASS", binary);
487   g_timer_destroy (btimer);
488   if (subtest_mode_fatal && !success)
489     terminate();
490 }
491
492 static void
493 usage (gboolean just_version)
494 {
495   if (just_version)
496     {
497       g_print ("gtester version %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
498       return;
499     }
500   g_print ("Usage:\n");
501   g_print ("gtester [OPTIONS] testprogram...\n\n");
502   /*        12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
503   g_print ("Help Options:\n");
504   g_print ("  -h, --help                    Show this help message\n\n");
505   g_print ("Utility Options:\n");
506   g_print ("  -v, --version                 Print version information\n");
507   g_print ("  --g-fatal-warnings            Make warnings fatal (abort)\n");
508   g_print ("  -k, --keep-going              Continue running after tests failed\n");
509   g_print ("  -l                            List paths of available test cases\n");
510   g_print ("  -m {perf|slow|thorough|quick} Run test cases according to mode\n");
511   g_print ("  -m {undefined|no-undefined}   Run test cases according to mode\n");
512   g_print ("  -p=TESTPATH                   Only start test cases matching TESTPATH\n");
513   g_print ("  -s=TESTPATH                   Skip test cases matching TESTPATH\n");
514   g_print ("  --seed=SEEDSTRING             Start tests with random seed SEEDSTRING\n");
515   g_print ("  -o=LOGFILE                    Write the test log to LOGFILE\n");
516   g_print ("  -q, --quiet                   Suppress per test binary output\n");
517   g_print ("  --verbose                     Report success per testcase\n");
518 }
519
520 static void
521 parse_args (gint    *argc_p,
522             gchar ***argv_p)
523 {
524   guint argc = *argc_p;
525   gchar **argv = *argv_p;
526   guint i, e;
527   /* parse known args */
528   for (i = 1; i < argc; i++)
529     {
530       if (strcmp (argv[i], "--g-fatal-warnings") == 0)
531         {
532           GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
533           fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
534           g_log_set_always_fatal (fatal_mask);
535           argv[i] = NULL;
536         }
537       else if (strcmp (argv[i], "--gtester-selftest") == 0)
538         {
539           gtester_selftest = TRUE;
540           argv[i] = NULL;
541           break;        /* stop parsing regular gtester arguments */
542         }
543       else if (strcmp (argv[i], "-h") == 0 || strcmp (argv[i], "--help") == 0)
544         {
545           usage (FALSE);
546           exit (0);
547           argv[i] = NULL;
548         }
549       else if (strcmp (argv[i], "-v") == 0 || strcmp (argv[i], "--version") == 0)
550         {
551           usage (TRUE);
552           exit (0);
553           argv[i] = NULL;
554         }
555       else if (strcmp (argv[i], "--keep-going") == 0 ||
556                strcmp (argv[i], "-k") == 0)
557         {
558           subtest_mode_fatal = FALSE;
559           argv[i] = NULL;
560         }
561       else if (strcmp ("-p", argv[i]) == 0 || strncmp ("-p=", argv[i], 3) == 0)
562         {
563           gchar *equal = argv[i] + 2;
564           if (*equal == '=')
565             subtest_paths = g_slist_prepend (subtest_paths, equal + 1);
566           else if (i + 1 < argc)
567             {
568               argv[i++] = NULL;
569               subtest_paths = g_slist_prepend (subtest_paths, argv[i]);
570             }
571           argv[i] = NULL;
572         }
573       else if (strcmp ("-s", argv[i]) == 0 || strncmp ("-s=", argv[i], 3) == 0)
574         {
575           gchar *equal = argv[i] + 2;
576           if (*equal == '=')
577             skipped_paths = g_slist_prepend (skipped_paths, equal + 1);
578           else if (i + 1 < argc)
579             {
580               argv[i++] = NULL;
581               skipped_paths = g_slist_prepend (skipped_paths, argv[i]);
582             }
583           argv[i] = NULL;
584         }
585       else if (strcmp ("--test-arg", argv[i]) == 0 || strncmp ("--test-arg=", argv[i], 11) == 0)
586         {
587           gchar *equal = argv[i] + 10;
588           if (*equal == '=')
589             subtest_args = g_slist_prepend (subtest_args, equal + 1);
590           else if (i + 1 < argc)
591             {
592               argv[i++] = NULL;
593               subtest_args = g_slist_prepend (subtest_args, argv[i]);
594             }
595           argv[i] = NULL;
596         }
597       else if (strcmp ("-o", argv[i]) == 0 || strncmp ("-o=", argv[i], 3) == 0)
598         {
599           gchar *equal = argv[i] + 2;
600           if (*equal == '=')
601             output_filename = equal + 1;
602           else if (i + 1 < argc)
603             {
604               argv[i++] = NULL;
605               output_filename = argv[i];
606             }
607           argv[i] = NULL;
608         }
609       else if (strcmp ("-m", argv[i]) == 0 || strncmp ("-m=", argv[i], 3) == 0)
610         {
611           gchar *equal = argv[i] + 2;
612           const gchar *mode = "";
613           if (*equal == '=')
614             mode = equal + 1;
615           else if (i + 1 < argc)
616             {
617               argv[i++] = NULL;
618               mode = argv[i];
619             }
620           if (strcmp (mode, "perf") == 0)
621             subtest_mode_perf = TRUE;
622           else if (strcmp (mode, "slow") == 0 || strcmp (mode, "thorough") == 0)
623             subtest_mode_quick = FALSE;
624           else if (strcmp (mode, "quick") == 0)
625             {
626               subtest_mode_quick = TRUE;
627               subtest_mode_perf = FALSE;
628             }
629           else if (strcmp (mode, "undefined") == 0)
630             subtest_mode_undefined = TRUE;
631           else if (strcmp (mode, "no-undefined") == 0)
632             subtest_mode_undefined = FALSE;
633           else
634             g_error ("unknown test mode: -m %s", mode);
635           argv[i] = NULL;
636         }
637       else if (strcmp ("-q", argv[i]) == 0 || strcmp ("--quiet", argv[i]) == 0)
638         {
639           gtester_quiet = TRUE;
640           gtester_verbose = FALSE;
641           argv[i] = NULL;
642         }
643       else if (strcmp ("--verbose", argv[i]) == 0)
644         {
645           gtester_quiet = FALSE;
646           gtester_verbose = TRUE;
647           argv[i] = NULL;
648         }
649       else if (strcmp ("-l", argv[i]) == 0)
650         {
651           gtester_list_tests = TRUE;
652           argv[i] = NULL;
653         }
654       else if (strcmp ("--seed", argv[i]) == 0 || strncmp ("--seed=", argv[i], 7) == 0)
655         {
656           gchar *equal = argv[i] + 6;
657           if (*equal == '=')
658             subtest_seedstr = equal + 1;
659           else if (i + 1 < argc)
660             {
661               argv[i++] = NULL;
662               subtest_seedstr = argv[i];
663             }
664           argv[i] = NULL;
665         }
666       else if (strcmp ("--i-know-this-is-deprecated", argv[i]) == 0)
667         {
668           gtester_ignore_deprecation = TRUE;
669           argv[i] = NULL;
670         }
671     }
672   /* collapse argv */
673   e = 0;
674   for (i = 0; i < argc; i++)
675     if (argv[i])
676       {
677         argv[e++] = argv[i];
678         if (i >= e)
679           argv[i] = NULL;
680       }
681   *argc_p = e;
682 }
683
684 int
685 main (int    argc,
686       char **argv)
687 {
688   gint ui;
689
690   g_set_prgname (argv[0]);
691   parse_args (&argc, &argv);
692   if (gtester_selftest)
693     return main_selftest (argc, argv);
694
695   if (argc <= 1)
696     {
697       usage (FALSE);
698       return 1;
699     }
700
701   if (!gtester_ignore_deprecation)
702     g_warning ("Deprecated: Since GLib 2.62, gtester and gtester-report are "
703                "deprecated. Port to TAP.");
704
705   if (output_filename)
706     {
707       int errsv;
708       log_fd = g_open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
709       errsv = errno;
710       if (log_fd < 0)
711         g_error ("Failed to open log file '%s': %s", output_filename, g_strerror (errsv));
712     }
713
714   test_log_printfe ("<?xml version=\"1.0\"?>\n");
715   test_log_printfe ("<!-- Deprecated: Since GLib 2.62, gtester and "
716                     "gtester-report are deprecated. Port to TAP. -->\n");
717   test_log_printfe ("%s<gtester>\n", sindent (log_indent));
718   log_indent += 2;
719   for (ui = 1; ui < argc; ui++)
720     {
721       const char *binary = argv[ui];
722       launch_test (binary);
723       /* we only get here on success or if !subtest_mode_fatal */
724     }
725   log_indent -= 2;
726   test_log_printfe ("%s</gtester>\n", sindent (log_indent));
727
728   close (log_fd);
729
730   return testcase_fail_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
731 }
732
733 static void
734 fixture_setup (guint        *fix,
735                gconstpointer test_data)
736 {
737   g_assert_cmphex (*fix, ==, 0);
738   *fix = 0xdeadbeef;
739 }
740 static void
741 fixture_test (guint        *fix,
742               gconstpointer test_data)
743 {
744   g_assert_cmphex (*fix, ==, 0xdeadbeef);
745   g_test_message ("This is a test message API test message.");
746   g_test_bug_base ("http://www.example.com/bugtracker/");
747   g_test_bug ("123");
748   g_test_bug_base ("http://www.example.com/bugtracker?bugnum=%s;cmd=showbug");
749   g_test_bug ("456");
750   g_test_bug ("https://example.com/no-base-used");
751 }
752 static void
753 fixture_teardown (guint        *fix,
754                   gconstpointer test_data)
755 {
756   g_assert_cmphex (*fix, ==, 0xdeadbeef);
757 }
758
759 static int
760 main_selftest (int    argc,
761                char **argv)
762 {
763   /* gtester main() for --gtester-selftest invocations */
764   g_test_init (&argc, &argv, NULL);
765   g_test_add ("/gtester/fixture-test", guint, NULL, fixture_setup, fixture_test, fixture_teardown);
766   return g_test_run();
767 }