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