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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
30 /* the read buffer size in bytes */
31 #define READ_BUFFER_SIZE 4096
33 /* --- prototypes --- */
34 static int main_selftest (int argc,
36 static void parse_args (gint *argc_p,
39 /* --- variables --- */
40 static GIOChannel *ioc_report = NULL;
41 static gboolean gtester_quiet = FALSE;
42 static gboolean gtester_verbose = FALSE;
43 static gboolean gtester_list_tests = FALSE;
44 static gboolean gtester_selftest = FALSE;
45 static gboolean subtest_running = FALSE;
46 static gint subtest_exitstatus = 0;
47 static gboolean subtest_io_pending = FALSE;
48 static gboolean subtest_quiet = TRUE;
49 static gboolean subtest_verbose = FALSE;
50 static gboolean subtest_mode_fatal = TRUE;
51 static gboolean subtest_mode_perf = FALSE;
52 static gboolean subtest_mode_quick = TRUE;
53 static const gchar *subtest_seedstr = NULL;
54 static gchar *subtest_last_seed = NULL;
55 static GSList *subtest_paths = NULL;
56 static GSList *subtest_args = NULL;
57 static gboolean testcase_open = FALSE;
58 static guint testcase_count = 0;
59 static guint testcase_fail_count = 0;
60 static const gchar *output_filename = NULL;
61 static guint log_indent = 0;
62 static gint log_fd = -1;
64 /* --- functions --- */
68 static const char spaces[] = " ";
69 int l = sizeof (spaces) - 1;
71 return spaces + l - n;
74 static void G_GNUC_PRINTF (1, 2)
75 test_log_printfe (const char *format,
81 va_start (args, format);
82 result = g_markup_vprintf_escaped (format, args);
85 r = write (log_fd, result, strlen (result));
86 while (r < 0 && errno == EINTR);
93 kill (getpid(), SIGTERM);
98 testcase_close (long double duration,
102 g_return_if_fail (testcase_open > 0);
103 test_log_printfe ("%s<duration>%.6Lf</duration>\n", sindent (log_indent), duration);
104 test_log_printfe ("%s<status exit-status=\"%d\" n-forks=\"%d\" result=\"%s\"/>\n",
105 sindent (log_indent), exit_status, n_forks,
106 exit_status ? "failed" : "success");
108 test_log_printfe ("%s</testcase>\n", sindent (log_indent));
111 g_print ("%s\n", exit_status ? "FAIL" : "OK");
112 if (exit_status && subtest_last_seed)
113 g_print ("GTester: last random seed: %s\n", subtest_last_seed);
115 testcase_fail_count += 1;
116 if (subtest_mode_fatal && testcase_fail_count)
121 test_log_msg (GTestLogMsg *msg)
123 switch (msg->log_type)
125 case G_TEST_LOG_NONE:
127 case G_TEST_LOG_ERROR:
128 g_printerr ("%s\n", msg->strings[0]);
130 case G_TEST_LOG_START_BINARY:
131 test_log_printfe ("%s<binary file=\"%s\"/>\n", sindent (log_indent), msg->strings[0]);
132 subtest_last_seed = g_strdup (msg->strings[1]);
133 test_log_printfe ("%s<random-seed>%s</random-seed>\n", sindent (log_indent), subtest_last_seed);
135 case G_TEST_LOG_LIST_CASE:
136 g_print ("%s\n", msg->strings[0]);
138 case G_TEST_LOG_START_CASE:
142 gchar *sc = g_strconcat (msg->strings[0], ":", NULL);
143 gchar *sleft = g_strdup_printf ("%-68s", sc);
145 g_print ("%70s ", sleft);
148 g_return_if_fail (testcase_open == 0);
150 test_log_printfe ("%s<testcase path=\"%s\">\n", sindent (log_indent), msg->strings[0]);
153 case G_TEST_LOG_SKIP_CASE:
154 if (FALSE && gtester_verbose) // enable to debug test case skipping logic
156 gchar *sc = g_strconcat (msg->strings[0], ":", NULL);
157 gchar *sleft = g_strdup_printf ("%-68s", sc);
159 g_print ("%70s SKIPPED\n", sleft);
162 test_log_printfe ("%s<testcase path=\"%s\" skipped=\"1\"/>\n", sindent (log_indent), msg->strings[0]);
164 case G_TEST_LOG_STOP_CASE:
165 testcase_close (msg->nums[2], (int) msg->nums[0], (int) msg->nums[1]);
167 case G_TEST_LOG_MIN_RESULT:
168 case G_TEST_LOG_MAX_RESULT:
169 test_log_printfe ("%s<performance minimize=\"%d\" maximize=\"%d\" value=\"%.16Lg\">\n",
170 sindent (log_indent), msg->log_type == G_TEST_LOG_MIN_RESULT, msg->log_type == G_TEST_LOG_MAX_RESULT, msg->nums[0]);
171 test_log_printfe ("%s%s\n", sindent (log_indent + 2), msg->strings[0]);
172 test_log_printfe ("%s</performance>\n", sindent (log_indent));
174 case G_TEST_LOG_MESSAGE:
175 test_log_printfe ("%s<message>\n%s\n%s</message>\n", sindent (log_indent), msg->strings[0], sindent (log_indent));
181 child_report_cb (GIOChannel *source,
182 GIOCondition condition,
185 GTestLogBuffer *tlb = data;
186 GIOStatus status = G_IO_STATUS_NORMAL;
187 gboolean first_read_eof = FALSE, first_read = TRUE;
191 guint8 buffer[READ_BUFFER_SIZE];
192 GError *error = NULL;
193 status = g_io_channel_read_chars (source, (gchar*) buffer, sizeof (buffer), &length, &error);
194 if (first_read && (condition & G_IO_IN))
196 /* on some unixes (MacOS) we need to detect non-blocking fd EOF
197 * by an IO_IN select/poll followed by read()==0.
199 first_read_eof = length == 0;
205 g_test_log_buffer_push (tlb, length, buffer);
208 msg = g_test_log_buffer_pop (tlb);
212 g_test_log_msg_free (msg);
217 g_clear_error (&error);
218 /* ignore the io channel status, which will report intermediate EOFs for non blocking fds */
222 if (first_read_eof || (condition & (G_IO_ERR | G_IO_HUP)))
224 /* if there's no data to read and select() reports an error or hangup,
225 * the fd must have been closed remotely
227 subtest_io_pending = FALSE;
230 return TRUE; /* keep polling */
234 child_watch_cb (GPid pid,
238 g_spawn_close_pid (pid);
239 if (WIFEXITED (status)) /* normal exit */
240 subtest_exitstatus = WEXITSTATUS (status);
241 else /* signal or core dump, etc */
242 subtest_exitstatus = 0xffffffff;
243 subtest_running = FALSE;
247 queue_gfree (GSList **slistp,
250 *slistp = g_slist_prepend (*slistp, string);
255 unset_cloexec_fdp (gpointer fdp_data)
257 int r, *fdp = fdp_data;
259 r = fcntl (*fdp, F_SETFD, 0 /* FD_CLOEXEC */);
260 while (r < 0 && errno == EINTR);
264 launch_test_binary (const char *binary,
268 GSList *slist, *free_list = NULL;
269 GError *error = NULL;
270 const gchar *argv[99 + g_slist_length (subtest_args) + g_slist_length (subtest_paths)];
272 gint report_pipe[2] = { -1, -1 };
275 if (pipe (report_pipe) < 0)
277 if (subtest_mode_fatal)
278 g_error ("Failed to open pipe for test binary: %s: %s", binary, g_strerror (errno));
280 g_warning ("Failed to open pipe for test binary: %s: %s", binary, g_strerror (errno));
286 for (slist = subtest_args; slist; slist = slist->next)
287 argv[i++] = (gchar*) slist->data;
288 // argv[i++] = "--debug-log";
290 argv[i++] = "--quiet";
292 argv[i++] = "--verbose";
293 if (!subtest_mode_fatal)
294 argv[i++] = "--keep-going";
295 if (subtest_mode_quick)
296 argv[i++] = "-m=quick";
298 argv[i++] = "-m=slow";
299 if (subtest_mode_perf)
300 argv[i++] = "-m=perf";
301 if (gtester_list_tests)
304 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--seed=%s", subtest_seedstr));
305 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestLogFD=%u", report_pipe[1]));
307 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestSkipCount=%u", skip_tests));
308 for (slist = subtest_paths; slist; slist = slist->next)
309 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("-p=%s", (gchar*) slist->data));
312 g_spawn_async_with_pipes (NULL, /* g_get_current_dir() */
315 G_SPAWN_DO_NOT_REAP_CHILD, /* G_SPAWN_SEARCH_PATH */
316 unset_cloexec_fdp, &report_pipe[1], /* pre-exec callback */
318 NULL, /* standard_input */
319 NULL, /* standard_output */
320 NULL, /* standard_error */
322 g_slist_foreach (free_list, (void(*)(void*,void*)) g_free, NULL);
323 g_slist_free (free_list);
325 close (report_pipe[1]);
328 g_print ("(pid=%lu)\n", (unsigned long) pid);
332 close (report_pipe[0]);
333 if (subtest_mode_fatal)
334 g_error ("Failed to execute test binary: %s: %s", argv[0], error->message);
336 g_warning ("Failed to execute test binary: %s: %s", argv[0], error->message);
337 g_clear_error (&error);
341 subtest_running = TRUE;
342 subtest_io_pending = TRUE;
343 tlb = g_test_log_buffer_new();
344 if (report_pipe[0] >= 0)
346 ioc_report = g_io_channel_unix_new (report_pipe[0]);
347 g_io_channel_set_flags (ioc_report, G_IO_FLAG_NONBLOCK, NULL);
348 g_io_channel_set_encoding (ioc_report, NULL, NULL);
349 g_io_channel_set_buffered (ioc_report, FALSE);
350 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);
351 g_io_channel_unref (ioc_report);
353 g_child_watch_add_full (G_PRIORITY_DEFAULT + 1, pid, child_watch_cb, NULL, NULL);
355 while (subtest_running || /* FALSE once child exits */
356 subtest_io_pending || /* FALSE once ioc_report closes */
357 g_main_context_pending (NULL)) /* TRUE while idler, etc are running */
358 g_main_context_iteration (NULL, TRUE);
360 close (report_pipe[0]);
361 g_test_log_buffer_free (tlb);
367 launch_test (const char *binary)
369 gboolean success = TRUE;
370 GTimer *btimer = g_timer_new();
371 gboolean need_restart;
373 testcase_fail_count = 0;
375 g_print ("TEST: %s... ", binary);
378 test_log_printfe ("%s<testbinary path=\"%s\">\n", sindent (log_indent), binary);
380 g_timer_start (btimer);
381 subtest_exitstatus = 0;
382 success &= launch_test_binary (binary, testcase_count);
383 success &= subtest_exitstatus == 0;
384 need_restart = testcase_open != 0;
386 testcase_close (0, -256, 0);
387 g_timer_stop (btimer);
388 test_log_printfe ("%s<duration>%.6f</duration>\n", sindent (log_indent), g_timer_elapsed (btimer, NULL));
390 test_log_printfe ("%s</testbinary>\n", sindent (log_indent));
391 g_free (subtest_last_seed);
392 subtest_last_seed = NULL;
395 /* restart test binary, skipping processed test cases */
400 g_print ("%s: %s\n", testcase_fail_count || !success ? "FAIL" : "PASS", binary);
401 g_timer_destroy (btimer);
402 if (subtest_mode_fatal && !success)
407 usage (gboolean just_version)
411 g_print ("gtester version %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
414 g_print ("Usage: gtester [OPTIONS] testprogram...\n");
415 /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
416 g_print ("Options:\n");
417 g_print (" -h, --help show this help message\n");
418 g_print (" -v, --version print version informations\n");
419 g_print (" --g-fatal-warnings make warnings fatal (abort)\n");
420 g_print (" -k, --keep-going continue running after tests failed\n");
421 g_print (" -l list paths of available test cases\n");
422 g_print (" -m=perf, -m=slow, -m=quick -m=thorough\n");
423 g_print (" run test cases in mode perf, slow/thorough or quick (default)\n");
424 g_print (" -p=TESTPATH only start test cases matching TESTPATH\n");
425 g_print (" --seed=SEEDSTRING start all tests with random number seed SEEDSTRING\n");
426 g_print (" -o=LOGFILE write the test log to LOGFILE\n");
427 g_print (" -q, --quiet suppress per test binary output\n");
428 g_print (" --verbose report success per testcase\n");
432 parse_args (gint *argc_p,
435 guint argc = *argc_p;
436 gchar **argv = *argv_p;
438 /* parse known args */
439 for (i = 1; i < argc; i++)
441 if (strcmp (argv[i], "--g-fatal-warnings") == 0)
443 GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
444 fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
445 g_log_set_always_fatal (fatal_mask);
448 else if (strcmp (argv[i], "--gtester-selftest") == 0)
450 gtester_selftest = TRUE;
452 break; // stop parsing regular gtester arguments
454 else if (strcmp (argv[i], "-h") == 0 || strcmp (argv[i], "--help") == 0)
460 else if (strcmp (argv[i], "-v") == 0 || strcmp (argv[i], "--version") == 0)
466 else if (strcmp (argv[i], "--keep-going") == 0 ||
467 strcmp (argv[i], "-k") == 0)
469 subtest_mode_fatal = FALSE;
472 else if (strcmp ("-p", argv[i]) == 0 || strncmp ("-p=", argv[i], 3) == 0)
474 gchar *equal = argv[i] + 2;
476 subtest_paths = g_slist_prepend (subtest_paths, equal + 1);
477 else if (i + 1 < argc)
480 subtest_paths = g_slist_prepend (subtest_paths, argv[i]);
484 else if (strcmp ("--test-arg", argv[i]) == 0 || strncmp ("--test-arg=", argv[i], 11) == 0)
486 gchar *equal = argv[i] + 10;
488 subtest_args = g_slist_prepend (subtest_args, equal + 1);
489 else if (i + 1 < argc)
492 subtest_args = g_slist_prepend (subtest_args, argv[i]);
496 else if (strcmp ("-o", argv[i]) == 0 || strncmp ("-o=", argv[i], 3) == 0)
498 gchar *equal = argv[i] + 2;
500 output_filename = equal + 1;
501 else if (i + 1 < argc)
504 output_filename = argv[i];
508 else if (strcmp ("-m", argv[i]) == 0 || strncmp ("-m=", argv[i], 3) == 0)
510 gchar *equal = argv[i] + 2;
511 const gchar *mode = "";
514 else if (i + 1 < argc)
519 if (strcmp (mode, "perf") == 0)
520 subtest_mode_perf = TRUE;
521 else if (strcmp (mode, "slow") == 0 || strcmp (mode, "thorough") == 0)
522 subtest_mode_quick = FALSE;
523 else if (strcmp (mode, "quick") == 0)
525 subtest_mode_quick = TRUE;
526 subtest_mode_perf = FALSE;
529 g_error ("unknown test mode: -m %s", mode);
532 else if (strcmp ("-q", argv[i]) == 0 || strcmp ("--quiet", argv[i]) == 0)
534 gtester_quiet = TRUE;
535 gtester_verbose = FALSE;
538 else if (strcmp ("--verbose", argv[i]) == 0)
540 gtester_quiet = FALSE;
541 gtester_verbose = TRUE;
544 else if (strcmp ("-l", argv[i]) == 0)
546 gtester_list_tests = TRUE;
549 else if (strcmp ("--seed", argv[i]) == 0 || strncmp ("--seed=", argv[i], 7) == 0)
551 gchar *equal = argv[i] + 6;
553 subtest_seedstr = equal + 1;
554 else if (i + 1 < argc)
557 subtest_seedstr = argv[i];
564 for (i = 1; i < argc; i++)
580 /* some unices need SA_RESTART for SIGCHLD to return -EAGAIN for io.
581 * we must fiddle with sigaction() *before* glib is used, otherwise
582 * we could revoke signal hanmdler setups from glib initialization code.
587 struct sigaction osa;
588 sa.sa_handler = SIG_DFL;
589 sigfillset (&sa.sa_mask);
590 sa.sa_flags = SA_RESTART;
591 sigaction (SIGCHLD, &sa, &osa);
594 g_set_prgname (argv[0]);
595 parse_args (&argc, &argv);
596 if (gtester_selftest)
597 return main_selftest (argc, argv);
607 log_fd = g_open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
609 g_error ("Failed to open log file '%s': %s", output_filename, g_strerror (errno));
612 test_log_printfe ("<?xml version=\"1.0\"?>\n");
613 test_log_printfe ("%s<gtester>\n", sindent (log_indent));
615 for (ui = 1; ui < argc; ui++)
617 const char *binary = argv[ui];
618 launch_test (binary);
619 /* we only get here on success or if !subtest_mode_fatal */
622 test_log_printfe ("%s</gtester>\n", sindent (log_indent));
630 fixture_setup (guint *fix,
631 gconstpointer test_data)
633 g_assert_cmphex (*fix, ==, 0);
637 fixture_test (guint *fix,
638 gconstpointer test_data)
640 g_assert_cmphex (*fix, ==, 0xdeadbeef);
641 g_test_message ("This is a test message API test message.");
642 g_test_bug_base ("http://www.example.com/bugtracker/");
644 g_test_bug_base ("http://www.example.com/bugtracker?bugnum=%s;cmd=showbug");
648 fixture_teardown (guint *fix,
649 gconstpointer test_data)
651 g_assert_cmphex (*fix, ==, 0xdeadbeef);
655 main_selftest (int argc,
658 /* gtester main() for --gtester-selftest invokations */
659 g_test_init (&argc, &argv, NULL);
660 g_test_add ("/gtester/fixture-test", guint, NULL, fixture_setup, fixture_test, fixture_teardown);