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