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 GSList *subtest_paths = NULL;
55 static GSList *subtest_args = NULL;
56 static gboolean testcase_open = FALSE;
57 static guint testcase_count = 0;
58 static guint testcase_fail_count = 0;
59 static const gchar *output_filename = NULL;
60 static guint log_indent = 0;
61 static gint log_fd = -1;
63 /* --- functions --- */
67 static const char spaces[] = " ";
68 int l = sizeof (spaces) - 1;
70 return spaces + l - n;
73 static void G_GNUC_PRINTF (1, 2)
74 test_log_printfe (const char *format,
80 va_start (args, format);
81 result = g_markup_vprintf_escaped (format, args);
84 r = write (log_fd, result, strlen (result));
85 while (r < 0 && errno == EINTR);
92 kill (getpid(), SIGTERM);
97 testcase_close (long double duration,
101 g_return_if_fail (testcase_open > 0);
102 test_log_printfe ("%s<duration>%.6Lf</duration>\n", sindent (log_indent), duration);
103 test_log_printfe ("%s<status exit-status=\"%d\" n-forks=\"%d\"/>\n",
104 sindent (log_indent), exit_status, n_forks);
106 test_log_printfe ("%s</testcase>\n", sindent (log_indent));
109 g_print ("%s\n", exit_status ? "FAIL" : "OK");
111 testcase_fail_count += 1;
112 if (subtest_mode_fatal && testcase_fail_count)
117 test_log_msg (GTestLogMsg *msg)
119 switch (msg->log_type)
121 case G_TEST_LOG_NONE:
123 case G_TEST_LOG_ERROR:
124 g_printerr ("%s\n", msg->strings[0]);
126 case G_TEST_LOG_START_BINARY:
127 test_log_printfe ("%s<binary file=\"%s\"/>\n", sindent (log_indent), msg->strings[0]);
128 test_log_printfe ("%s<random-seed>%s</random-seed>\n", sindent (log_indent), msg->strings[1]);
130 case G_TEST_LOG_LIST_CASE:
131 g_print ("%s\n", msg->strings[0]);
133 case G_TEST_LOG_START_CASE:
137 gchar *sc = g_strconcat (msg->strings[0], ":", NULL);
138 gchar *sleft = g_strdup_printf ("%-68s", sc);
140 g_print ("%70s ", sleft);
143 g_return_if_fail (testcase_open == 0);
145 test_log_printfe ("%s<testcase path=\"%s\">\n", sindent (log_indent), msg->strings[0]);
148 case G_TEST_LOG_SKIP_CASE:
149 if (TRUE && gtester_verbose) // enable to debug test case skipping logic
151 gchar *sc = g_strconcat (msg->strings[0], ":", NULL);
152 gchar *sleft = g_strdup_printf ("%-68s", sc);
154 g_print ("%70s SKIPPED\n", sleft);
157 test_log_printfe ("%s<testcase path=\"%s\" skipped=\"1\"/>\n", sindent (log_indent), msg->strings[0]);
159 case G_TEST_LOG_STOP_CASE:
160 testcase_close (msg->nums[2], (int) msg->nums[0], (int) msg->nums[1]);
162 case G_TEST_LOG_MIN_RESULT:
163 case G_TEST_LOG_MAX_RESULT:
164 test_log_printfe ("%s<performance minimize=\"%d\" maximize=\"%d\" value=\"%.16Lg\">\n",
165 sindent (log_indent), msg->log_type == G_TEST_LOG_MIN_RESULT, msg->log_type == G_TEST_LOG_MAX_RESULT, msg->nums[0]);
166 test_log_printfe ("%s%s\n", sindent (log_indent + 2), msg->strings[0]);
167 test_log_printfe ("%s</performance>\n", sindent (log_indent));
169 case G_TEST_LOG_MESSAGE:
170 test_log_printfe ("%s<message>\n%s\n%s</message>\n", sindent (log_indent), msg->strings[0], sindent (log_indent));
176 child_report_cb (GIOChannel *source,
177 GIOCondition condition,
180 GTestLogBuffer *tlb = data;
181 GIOStatus status = G_IO_STATUS_NORMAL;
185 guint8 buffer[READ_BUFFER_SIZE];
186 GError *error = NULL;
187 status = g_io_channel_read_chars (source, (gchar*) buffer, sizeof (buffer), &length, &error);
191 g_test_log_buffer_push (tlb, length, buffer);
194 msg = g_test_log_buffer_pop (tlb);
198 g_test_log_msg_free (msg);
203 g_clear_error (&error);
204 /* ignore the io channel status, which seems to be bogus especially for non blocking fds */
208 if (condition & (G_IO_ERR | G_IO_HUP))
210 /* if there's no data to read and select() reports an error or hangup,
211 * the fd must have been closed remotely
213 subtest_io_pending = FALSE;
216 return TRUE; /* keep polling */
220 child_watch_cb (GPid pid,
224 g_spawn_close_pid (pid);
225 if (WIFEXITED (status)) /* normal exit */
226 subtest_exitstatus = WEXITSTATUS (status);
227 else /* signal or core dump, etc */
228 subtest_exitstatus = 0xffffffff;
229 subtest_running = FALSE;
233 queue_gfree (GSList **slistp,
236 *slistp = g_slist_prepend (*slistp, string);
241 unset_cloexec_fdp (gpointer fdp_data)
243 int r, *fdp = fdp_data;
245 r = fcntl (*fdp, F_SETFD, 0 /* FD_CLOEXEC */);
246 while (r < 0 && errno == EINTR);
250 launch_test_binary (const char *binary,
254 GSList *slist, *free_list = NULL;
255 GError *error = NULL;
256 const gchar *argv[99 + g_slist_length (subtest_args) + g_slist_length (subtest_paths)];
258 gint report_pipe[2] = { -1, -1 };
261 if (pipe (report_pipe) < 0)
263 if (subtest_mode_fatal)
264 g_error ("Failed to open pipe for test binary: %s: %s", binary, g_strerror (errno));
266 g_warning ("Failed to open pipe for test binary: %s: %s", binary, g_strerror (errno));
272 for (slist = subtest_args; slist; slist = slist->next)
273 argv[i++] = (gchar*) slist->data;
274 // argv[i++] = "--debug-log";
276 argv[i++] = "--quiet";
278 argv[i++] = "--verbose";
279 if (!subtest_mode_fatal)
280 argv[i++] = "--keep-going";
281 if (subtest_mode_quick)
282 argv[i++] = "-m=quick";
284 argv[i++] = "-m=slow";
285 if (subtest_mode_perf)
286 argv[i++] = "-m=perf";
287 if (gtester_list_tests)
290 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--seed=%s", subtest_seedstr));
291 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestLogFD=%u", report_pipe[1]));
293 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestSkipCount=%u", skip_tests));
294 for (slist = subtest_paths; slist; slist = slist->next)
295 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("-p=%s", (gchar*) slist->data));
298 g_spawn_async_with_pipes (NULL, /* g_get_current_dir() */
301 G_SPAWN_DO_NOT_REAP_CHILD, /* G_SPAWN_SEARCH_PATH */
302 unset_cloexec_fdp, &report_pipe[1], /* pre-exec callback */
304 NULL, /* standard_input */
305 NULL, /* standard_output */
306 NULL, /* standard_error */
308 g_slist_foreach (free_list, (void(*)(void*,void*)) g_free, NULL);
309 g_slist_free (free_list);
311 close (report_pipe[1]);
314 g_print ("(pid=%lu)\n", (unsigned long) pid);
318 close (report_pipe[0]);
319 if (subtest_mode_fatal)
320 g_error ("Failed to execute test binary: %s: %s", argv[0], error->message);
322 g_warning ("Failed to execute test binary: %s: %s", argv[0], error->message);
323 g_clear_error (&error);
327 subtest_running = TRUE;
328 subtest_io_pending = TRUE;
329 tlb = g_test_log_buffer_new();
330 if (report_pipe[0] >= 0)
332 ioc_report = g_io_channel_unix_new (report_pipe[0]);
333 g_io_channel_set_flags (ioc_report, G_IO_FLAG_NONBLOCK, NULL);
334 g_io_channel_set_encoding (ioc_report, NULL, NULL);
335 g_io_channel_set_buffered (ioc_report, FALSE);
336 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);
337 g_io_channel_unref (ioc_report);
339 g_child_watch_add_full (G_PRIORITY_DEFAULT + 1, pid, child_watch_cb, NULL, NULL);
341 while (subtest_running || /* FALSE once child exits */
342 subtest_io_pending || /* FALSE once ioc_report closes */
343 g_main_context_pending (NULL)) /* TRUE while idler, etc are running */
344 g_main_context_iteration (NULL, TRUE);
346 close (report_pipe[0]);
347 g_test_log_buffer_free (tlb);
353 launch_test (const char *binary)
355 gboolean success = TRUE;
356 GTimer *btimer = g_timer_new();
357 gboolean need_restart;
359 testcase_fail_count = 0;
361 g_print ("TEST: %s... ", binary);
364 test_log_printfe ("%s<testbinary path=\"%s\">\n", sindent (log_indent), binary);
366 g_timer_start (btimer);
367 subtest_exitstatus = 0;
368 success &= launch_test_binary (binary, testcase_count);
369 success &= subtest_exitstatus == 0;
370 need_restart = testcase_open != 0;
372 testcase_close (0, -999, 0);
373 g_timer_stop (btimer);
374 test_log_printfe ("%s<duration>%.6f</duration>\n", sindent (log_indent), g_timer_elapsed (btimer, NULL));
376 test_log_printfe ("%s</testbinary>\n", sindent (log_indent));
379 /* restart test binary, skipping processed test cases */
384 g_print ("%s: %s\n", testcase_fail_count || !success ? "FAIL" : "PASS", binary);
385 g_timer_destroy (btimer);
386 if (subtest_mode_fatal && !success)
391 usage (gboolean just_version)
395 g_print ("gtester version %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
398 g_print ("Usage: gtester [OPTIONS] testprogram...\n");
399 /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
400 g_print ("Options:\n");
401 g_print (" -h, --help show this help message\n");
402 g_print (" -v, --version print version informations\n");
403 g_print (" --g-fatal-warnings make warnings fatal (abort)\n");
404 g_print (" -k, --keep-going continue running after tests failed\n");
405 g_print (" -l list paths of available test cases\n");
406 g_print (" -m=perf, -m=slow, -m=quick run test cases in mode perf, slow or quick (default)\n");
407 g_print (" -p=TESTPATH only start test cases matching TESTPATH\n");
408 g_print (" --seed=SEEDSTRING start all tests with random number seed SEEDSTRING\n");
409 g_print (" -o=LOGFILE write the test log to LOGFILE\n");
410 g_print (" -q, --quiet suppress per test binary output\n");
411 g_print (" --verbose report success per testcase\n");
415 parse_args (gint *argc_p,
418 guint argc = *argc_p;
419 gchar **argv = *argv_p;
421 /* parse known args */
422 for (i = 1; i < argc; i++)
424 if (strcmp (argv[i], "--g-fatal-warnings") == 0)
426 GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
427 fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
428 g_log_set_always_fatal (fatal_mask);
431 else if (strcmp (argv[i], "--gtester-selftest") == 0)
433 gtester_selftest = TRUE;
435 break; // stop parsing regular gtester arguments
437 else if (strcmp (argv[i], "-h") == 0 || strcmp (argv[i], "--help") == 0)
443 else if (strcmp (argv[i], "-v") == 0 || strcmp (argv[i], "--version") == 0)
449 else if (strcmp (argv[i], "--keep-going") == 0 ||
450 strcmp (argv[i], "-k") == 0)
452 subtest_mode_fatal = FALSE;
455 else if (strcmp ("-p", argv[i]) == 0 || strncmp ("-p=", argv[i], 3) == 0)
457 gchar *equal = argv[i] + 2;
459 subtest_paths = g_slist_prepend (subtest_paths, equal + 1);
460 else if (i + 1 < argc)
463 subtest_paths = g_slist_prepend (subtest_paths, argv[i]);
467 else if (strcmp ("--test-arg", argv[i]) == 0 || strncmp ("--test-arg=", argv[i], 11) == 0)
469 gchar *equal = argv[i] + 10;
471 subtest_args = g_slist_prepend (subtest_args, equal + 1);
472 else if (i + 1 < argc)
475 subtest_args = g_slist_prepend (subtest_args, argv[i]);
479 else if (strcmp ("-o", argv[i]) == 0 || strncmp ("-o=", argv[i], 3) == 0)
481 gchar *equal = argv[i] + 2;
483 output_filename = equal + 1;
484 else if (i + 1 < argc)
487 output_filename = argv[i];
491 else if (strcmp ("-m", argv[i]) == 0 || strncmp ("-m=", argv[i], 3) == 0)
493 gchar *equal = argv[i] + 2;
494 const gchar *mode = "";
497 else if (i + 1 < argc)
502 if (strcmp (mode, "perf") == 0)
503 subtest_mode_perf = TRUE;
504 else if (strcmp (mode, "slow") == 0)
505 subtest_mode_quick = FALSE;
506 else if (strcmp (mode, "quick") == 0)
508 subtest_mode_quick = TRUE;
509 subtest_mode_perf = FALSE;
512 g_error ("unknown test mode: -m %s", mode);
515 else if (strcmp ("-q", argv[i]) == 0 || strcmp ("--quiet", argv[i]) == 0)
517 gtester_quiet = TRUE;
518 gtester_verbose = FALSE;
521 else if (strcmp ("--verbose", argv[i]) == 0)
523 gtester_quiet = FALSE;
524 gtester_verbose = TRUE;
527 else if (strcmp ("-l", argv[i]) == 0)
529 gtester_list_tests = TRUE;
532 else if (strcmp ("--seed", argv[i]) == 0 || strncmp ("--seed=", argv[i], 7) == 0)
534 gchar *equal = argv[i] + 6;
536 subtest_seedstr = equal + 1;
537 else if (i + 1 < argc)
540 subtest_seedstr = argv[i];
547 for (i = 1; i < argc; i++)
563 /* some unices need SA_RESTART for SIGCHLD to return -EAGAIN for io.
564 * we must fiddle with sigaction() *before* glib is used, otherwise
565 * we could revoke signal hanmdler setups from glib initialization code.
570 struct sigaction osa;
571 sa.sa_handler = SIG_DFL;
572 sigfillset (&sa.sa_mask);
573 sa.sa_flags = SA_RESTART;
574 sigaction (SIGCHLD, &sa, &osa);
577 g_set_prgname (argv[0]);
578 parse_args (&argc, &argv);
579 if (gtester_selftest)
580 return main_selftest (argc, argv);
590 log_fd = g_open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
592 g_error ("Failed to open log file '%s': %s", output_filename, g_strerror (errno));
595 test_log_printfe ("<?xml version=\"1.0\"?>\n");
596 test_log_printfe ("%s<gtester>\n", sindent (log_indent));
598 for (ui = 1; ui < argc; ui++)
600 const char *binary = argv[ui];
601 launch_test (binary);
602 /* we only get here on success or if !subtest_mode_fatal */
605 test_log_printfe ("%s</gtester>\n", sindent (log_indent));
613 fixture_setup (guint *fix,
614 gconstpointer test_data)
616 g_assert_cmphex (*fix, ==, 0);
620 fixture_test (guint *fix,
621 gconstpointer test_data)
623 g_assert_cmphex (*fix, ==, 0xdeadbeef);
624 g_test_message ("This is a test message API test message.");
625 g_test_bug_base ("http://www.example.com/bugtracker/");
627 g_test_bug_base ("http://www.example.com/bugtracker?bugnum=%s;cmd=showbug");
631 fixture_teardown (guint *fix,
632 gconstpointer test_data)
634 g_assert_cmphex (*fix, ==, 0xdeadbeef);
638 main_selftest (int argc,
641 /* gtester main() for --gtester-selftest invokations */
642 g_test_init (&argc, &argv, NULL);
643 g_test_add ("/gtester/fixture-test", guint, NULL, fixture_setup, fixture_test, fixture_teardown);