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;
190 guint8 buffer[READ_BUFFER_SIZE];
191 GError *error = NULL;
192 status = g_io_channel_read_chars (source, (gchar*) buffer, sizeof (buffer), &length, &error);
196 g_test_log_buffer_push (tlb, length, buffer);
199 msg = g_test_log_buffer_pop (tlb);
203 g_test_log_msg_free (msg);
208 g_clear_error (&error);
209 /* ignore the io channel status, which seems to be bogus especially for non blocking fds */
213 if (condition & (G_IO_ERR | G_IO_HUP))
215 /* if there's no data to read and select() reports an error or hangup,
216 * the fd must have been closed remotely
218 subtest_io_pending = FALSE;
221 return TRUE; /* keep polling */
225 child_watch_cb (GPid pid,
229 g_spawn_close_pid (pid);
230 if (WIFEXITED (status)) /* normal exit */
231 subtest_exitstatus = WEXITSTATUS (status);
232 else /* signal or core dump, etc */
233 subtest_exitstatus = 0xffffffff;
234 subtest_running = FALSE;
238 queue_gfree (GSList **slistp,
241 *slistp = g_slist_prepend (*slistp, string);
246 unset_cloexec_fdp (gpointer fdp_data)
248 int r, *fdp = fdp_data;
250 r = fcntl (*fdp, F_SETFD, 0 /* FD_CLOEXEC */);
251 while (r < 0 && errno == EINTR);
255 launch_test_binary (const char *binary,
259 GSList *slist, *free_list = NULL;
260 GError *error = NULL;
261 const gchar *argv[99 + g_slist_length (subtest_args) + g_slist_length (subtest_paths)];
263 gint report_pipe[2] = { -1, -1 };
266 if (pipe (report_pipe) < 0)
268 if (subtest_mode_fatal)
269 g_error ("Failed to open pipe for test binary: %s: %s", binary, g_strerror (errno));
271 g_warning ("Failed to open pipe for test binary: %s: %s", binary, g_strerror (errno));
277 for (slist = subtest_args; slist; slist = slist->next)
278 argv[i++] = (gchar*) slist->data;
279 // argv[i++] = "--debug-log";
281 argv[i++] = "--quiet";
283 argv[i++] = "--verbose";
284 if (!subtest_mode_fatal)
285 argv[i++] = "--keep-going";
286 if (subtest_mode_quick)
287 argv[i++] = "-m=quick";
289 argv[i++] = "-m=slow";
290 if (subtest_mode_perf)
291 argv[i++] = "-m=perf";
292 if (gtester_list_tests)
295 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--seed=%s", subtest_seedstr));
296 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestLogFD=%u", report_pipe[1]));
298 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestSkipCount=%u", skip_tests));
299 for (slist = subtest_paths; slist; slist = slist->next)
300 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("-p=%s", (gchar*) slist->data));
303 g_spawn_async_with_pipes (NULL, /* g_get_current_dir() */
306 G_SPAWN_DO_NOT_REAP_CHILD, /* G_SPAWN_SEARCH_PATH */
307 unset_cloexec_fdp, &report_pipe[1], /* pre-exec callback */
309 NULL, /* standard_input */
310 NULL, /* standard_output */
311 NULL, /* standard_error */
313 g_slist_foreach (free_list, (void(*)(void*,void*)) g_free, NULL);
314 g_slist_free (free_list);
316 close (report_pipe[1]);
319 g_print ("(pid=%lu)\n", (unsigned long) pid);
323 close (report_pipe[0]);
324 if (subtest_mode_fatal)
325 g_error ("Failed to execute test binary: %s: %s", argv[0], error->message);
327 g_warning ("Failed to execute test binary: %s: %s", argv[0], error->message);
328 g_clear_error (&error);
332 subtest_running = TRUE;
333 subtest_io_pending = TRUE;
334 tlb = g_test_log_buffer_new();
335 if (report_pipe[0] >= 0)
337 ioc_report = g_io_channel_unix_new (report_pipe[0]);
338 g_io_channel_set_flags (ioc_report, G_IO_FLAG_NONBLOCK, NULL);
339 g_io_channel_set_encoding (ioc_report, NULL, NULL);
340 g_io_channel_set_buffered (ioc_report, FALSE);
341 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);
342 g_io_channel_unref (ioc_report);
344 g_child_watch_add_full (G_PRIORITY_DEFAULT + 1, pid, child_watch_cb, NULL, NULL);
346 while (subtest_running || /* FALSE once child exits */
347 subtest_io_pending || /* FALSE once ioc_report closes */
348 g_main_context_pending (NULL)) /* TRUE while idler, etc are running */
349 g_main_context_iteration (NULL, TRUE);
351 close (report_pipe[0]);
352 g_test_log_buffer_free (tlb);
358 launch_test (const char *binary)
360 gboolean success = TRUE;
361 GTimer *btimer = g_timer_new();
362 gboolean need_restart;
364 testcase_fail_count = 0;
366 g_print ("TEST: %s... ", binary);
369 test_log_printfe ("%s<testbinary path=\"%s\">\n", sindent (log_indent), binary);
371 g_timer_start (btimer);
372 subtest_exitstatus = 0;
373 success &= launch_test_binary (binary, testcase_count);
374 success &= subtest_exitstatus == 0;
375 need_restart = testcase_open != 0;
377 testcase_close (0, -256, 0);
378 g_timer_stop (btimer);
379 test_log_printfe ("%s<duration>%.6f</duration>\n", sindent (log_indent), g_timer_elapsed (btimer, NULL));
381 test_log_printfe ("%s</testbinary>\n", sindent (log_indent));
382 g_free (subtest_last_seed);
383 subtest_last_seed = NULL;
386 /* restart test binary, skipping processed test cases */
391 g_print ("%s: %s\n", testcase_fail_count || !success ? "FAIL" : "PASS", binary);
392 g_timer_destroy (btimer);
393 if (subtest_mode_fatal && !success)
398 usage (gboolean just_version)
402 g_print ("gtester version %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
405 g_print ("Usage: gtester [OPTIONS] testprogram...\n");
406 /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
407 g_print ("Options:\n");
408 g_print (" -h, --help show this help message\n");
409 g_print (" -v, --version print version informations\n");
410 g_print (" --g-fatal-warnings make warnings fatal (abort)\n");
411 g_print (" -k, --keep-going continue running after tests failed\n");
412 g_print (" -l list paths of available test cases\n");
413 g_print (" -m=perf, -m=slow, -m=quick -m=thorough\n");
414 g_print (" run test cases in mode perf, slow/thorough or quick (default)\n");
415 g_print (" -p=TESTPATH only start test cases matching TESTPATH\n");
416 g_print (" --seed=SEEDSTRING start all tests with random number seed SEEDSTRING\n");
417 g_print (" -o=LOGFILE write the test log to LOGFILE\n");
418 g_print (" -q, --quiet suppress per test binary output\n");
419 g_print (" --verbose report success per testcase\n");
423 parse_args (gint *argc_p,
426 guint argc = *argc_p;
427 gchar **argv = *argv_p;
429 /* parse known args */
430 for (i = 1; i < argc; i++)
432 if (strcmp (argv[i], "--g-fatal-warnings") == 0)
434 GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
435 fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
436 g_log_set_always_fatal (fatal_mask);
439 else if (strcmp (argv[i], "--gtester-selftest") == 0)
441 gtester_selftest = TRUE;
443 break; // stop parsing regular gtester arguments
445 else if (strcmp (argv[i], "-h") == 0 || strcmp (argv[i], "--help") == 0)
451 else if (strcmp (argv[i], "-v") == 0 || strcmp (argv[i], "--version") == 0)
457 else if (strcmp (argv[i], "--keep-going") == 0 ||
458 strcmp (argv[i], "-k") == 0)
460 subtest_mode_fatal = FALSE;
463 else if (strcmp ("-p", argv[i]) == 0 || strncmp ("-p=", argv[i], 3) == 0)
465 gchar *equal = argv[i] + 2;
467 subtest_paths = g_slist_prepend (subtest_paths, equal + 1);
468 else if (i + 1 < argc)
471 subtest_paths = g_slist_prepend (subtest_paths, argv[i]);
475 else if (strcmp ("--test-arg", argv[i]) == 0 || strncmp ("--test-arg=", argv[i], 11) == 0)
477 gchar *equal = argv[i] + 10;
479 subtest_args = g_slist_prepend (subtest_args, equal + 1);
480 else if (i + 1 < argc)
483 subtest_args = g_slist_prepend (subtest_args, argv[i]);
487 else if (strcmp ("-o", argv[i]) == 0 || strncmp ("-o=", argv[i], 3) == 0)
489 gchar *equal = argv[i] + 2;
491 output_filename = equal + 1;
492 else if (i + 1 < argc)
495 output_filename = argv[i];
499 else if (strcmp ("-m", argv[i]) == 0 || strncmp ("-m=", argv[i], 3) == 0)
501 gchar *equal = argv[i] + 2;
502 const gchar *mode = "";
505 else if (i + 1 < argc)
510 if (strcmp (mode, "perf") == 0)
511 subtest_mode_perf = TRUE;
512 else if (strcmp (mode, "slow") == 0 || strcmp (mode, "thorough") == 0)
513 subtest_mode_quick = FALSE;
514 else if (strcmp (mode, "quick") == 0)
516 subtest_mode_quick = TRUE;
517 subtest_mode_perf = FALSE;
520 g_error ("unknown test mode: -m %s", mode);
523 else if (strcmp ("-q", argv[i]) == 0 || strcmp ("--quiet", argv[i]) == 0)
525 gtester_quiet = TRUE;
526 gtester_verbose = FALSE;
529 else if (strcmp ("--verbose", argv[i]) == 0)
531 gtester_quiet = FALSE;
532 gtester_verbose = TRUE;
535 else if (strcmp ("-l", argv[i]) == 0)
537 gtester_list_tests = TRUE;
540 else if (strcmp ("--seed", argv[i]) == 0 || strncmp ("--seed=", argv[i], 7) == 0)
542 gchar *equal = argv[i] + 6;
544 subtest_seedstr = equal + 1;
545 else if (i + 1 < argc)
548 subtest_seedstr = argv[i];
555 for (i = 1; i < argc; i++)
571 /* some unices need SA_RESTART for SIGCHLD to return -EAGAIN for io.
572 * we must fiddle with sigaction() *before* glib is used, otherwise
573 * we could revoke signal hanmdler setups from glib initialization code.
578 struct sigaction osa;
579 sa.sa_handler = SIG_DFL;
580 sigfillset (&sa.sa_mask);
581 sa.sa_flags = SA_RESTART;
582 sigaction (SIGCHLD, &sa, &osa);
585 g_set_prgname (argv[0]);
586 parse_args (&argc, &argv);
587 if (gtester_selftest)
588 return main_selftest (argc, argv);
598 log_fd = g_open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
600 g_error ("Failed to open log file '%s': %s", output_filename, g_strerror (errno));
603 test_log_printfe ("<?xml version=\"1.0\"?>\n");
604 test_log_printfe ("%s<gtester>\n", sindent (log_indent));
606 for (ui = 1; ui < argc; ui++)
608 const char *binary = argv[ui];
609 launch_test (binary);
610 /* we only get here on success or if !subtest_mode_fatal */
613 test_log_printfe ("%s</gtester>\n", sindent (log_indent));
621 fixture_setup (guint *fix,
622 gconstpointer test_data)
624 g_assert_cmphex (*fix, ==, 0);
628 fixture_test (guint *fix,
629 gconstpointer test_data)
631 g_assert_cmphex (*fix, ==, 0xdeadbeef);
632 g_test_message ("This is a test message API test message.");
633 g_test_bug_base ("http://www.example.com/bugtracker/");
635 g_test_bug_base ("http://www.example.com/bugtracker?bugnum=%s;cmd=showbug");
639 fixture_teardown (guint *fix,
640 gconstpointer test_data)
642 g_assert_cmphex (*fix, ==, 0xdeadbeef);
646 main_selftest (int argc,
649 /* gtester main() for --gtester-selftest invokations */
650 g_test_init (&argc, &argv, NULL);
651 g_test_add ("/gtester/fixture-test", guint, NULL, fixture_setup, fixture_test, fixture_teardown);