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.
29 /* the read buffer size in bytes */
30 #define READ_BUFFER_SIZE 4096
32 /* --- prototypes --- */
33 static void parse_args (gint *argc_p,
36 /* --- variables --- */
37 static GIOChannel *ioc_report = NULL;
38 static gboolean gtester_quiet = FALSE;
39 static gboolean gtester_verbose = FALSE;
40 static gboolean gtester_list_tests = FALSE;
41 static gboolean subtest_running = FALSE;
42 static gboolean subtest_io_pending = FALSE;
43 static gboolean subtest_faill = TRUE;
44 static gboolean subtest_quiet = TRUE;
45 static gboolean subtest_verbose = FALSE;
46 static gboolean subtest_mode_fatal = FALSE;
47 static gboolean subtest_mode_perf = FALSE;
48 static gboolean subtest_mode_quick = TRUE;
49 static const gchar *subtest_seedstr = NULL;
50 static GSList *subtest_paths = NULL;
51 static gboolean testcase_open = FALSE;
52 static const gchar *output_filename = NULL;
53 static guint log_indent = 0;
54 static gint log_fd = -1;
56 /* --- functions --- */
60 static const char spaces[] = " ";
61 int l = sizeof (spaces);
63 return spaces + l - n;
66 static void G_GNUC_PRINTF (1, 2)
67 test_log_printfe (const char *format,
73 va_start (args, format);
74 result = g_markup_vprintf_escaped (format, args);
77 r = write (log_fd, result, strlen (result));
78 while (r < 0 && errno == EINTR);
83 test_log_msg (GTestLogMsg *msg)
85 switch (msg->log_type)
89 case G_TEST_LOG_ERROR:
90 g_printerr ("%s\n", msg->strings[0]);
92 case G_TEST_LOG_START_BINARY:
93 test_log_printfe ("%s<binary file=\"%s\"/>\n", sindent (log_indent), msg->strings[0]);
94 test_log_printfe ("%s<random-seed>%s</random-seed>\n", sindent (log_indent), msg->strings[1]);
96 case G_TEST_LOG_LIST_CASE:
97 g_print ("%s\n", msg->strings[0]);
99 case G_TEST_LOG_START_CASE:
102 gchar *sc = g_strconcat (msg->strings[0], ":", NULL);
103 gchar *sleft = g_strdup_printf ("%-68s", sc);
105 g_print ("%70s ", sleft);
108 g_return_if_fail (testcase_open == 0);
110 test_log_printfe ("%s<testcase path=\"%s\">\n", sindent (log_indent), msg->strings[0]);
113 case G_TEST_LOG_STOP_CASE:
114 g_return_if_fail (testcase_open > 0);
115 test_log_printfe ("%s<duration>%.6Lf</duration>\n", sindent (log_indent), msg->nums[2]);
116 test_log_printfe ("%s<status exit-status=\"%d\" n-forks=\"%d\"/>\n",
117 sindent (log_indent), (int) msg->nums[0], (int) msg->nums[1]);
119 test_log_printfe ("%s</testcase>\n", sindent (log_indent));
124 case G_TEST_LOG_MIN_RESULT:
125 case G_TEST_LOG_MAX_RESULT:
126 test_log_printfe ("%s<performance minimize=\"%d\" maximize=\"%d\" value=\"%.16Lg\">\n",
127 sindent (log_indent), msg->log_type == G_TEST_LOG_MIN_RESULT, msg->log_type == G_TEST_LOG_MAX_RESULT, msg->nums[0]);
128 test_log_printfe ("%s%s\n", sindent (log_indent + 2), msg->strings[0]);
129 test_log_printfe ("%s</performance>\n", sindent (log_indent));
135 child_report_cb (GIOChannel *source,
136 GIOCondition condition,
139 GTestLogBuffer *tlb = data;
140 GIOStatus status = G_IO_STATUS_NORMAL;
144 guint8 buffer[READ_BUFFER_SIZE];
145 GError *error = NULL;
146 status = g_io_channel_read_chars (source, (gchar*) buffer, sizeof (buffer), &length, &error);
150 g_test_log_buffer_push (tlb, length, buffer);
153 msg = g_test_log_buffer_pop (tlb);
157 g_test_log_msg_free (msg);
162 g_clear_error (&error);
163 /* ignore the io channel status, which seems to be bogus especially for non blocking fds */
167 if (condition & (G_IO_ERR | G_IO_HUP))
169 /* if there's no data to read and select() reports an error or hangup,
170 * the fd must have been closed remotely
172 subtest_io_pending = FALSE;
175 return TRUE; /* keep polling */
179 child_watch_cb (GPid pid,
183 g_spawn_close_pid (pid);
184 subtest_running = FALSE;
188 queue_gfree (GSList **slistp,
191 *slistp = g_slist_prepend (*slistp, string);
196 unset_cloexec_fdp (gpointer fdp_data)
198 int r, *fdp = fdp_data;
200 r = fcntl (*fdp, F_SETFD, 0 /* FD_CLOEXEC */);
201 while (r < 0 && errno == EINTR);
205 launch_test_binary (const char *binary)
208 GSList *slist, *free_list = NULL;
209 GError *error = NULL;
210 const gchar *argv[20 + g_slist_length (subtest_paths)];
212 gint report_pipe[2] = { -1, -1 };
215 if (pipe (report_pipe) < 0)
217 if (subtest_mode_fatal)
218 g_error ("Failed to open pipe for test binary: %s: %s", binary, g_strerror (errno));
220 g_warning ("Failed to open pipe for test binary: %s: %s", binary, g_strerror (errno));
227 argv[i++] = "--quiet";
229 argv[i++] = "--verbose";
230 // argv[i++] = "--debug-log";
231 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestLogFD=%u", report_pipe[1]));
232 if (!subtest_mode_fatal)
233 argv[i++] = "--keep-going";
234 if (subtest_mode_quick)
235 argv[i++] = "-m=quick";
237 argv[i++] = "-m=slow";
238 if (subtest_mode_perf)
239 argv[i++] = "-m=perf";
241 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--seed=%s", subtest_seedstr));
242 for (slist = subtest_paths; slist; slist = slist->next)
243 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("-p=%s", (gchar*) slist->data));
244 if (gtester_list_tests)
248 g_spawn_async_with_pipes (NULL, /* g_get_current_dir() */
251 G_SPAWN_DO_NOT_REAP_CHILD, /* G_SPAWN_SEARCH_PATH */
252 unset_cloexec_fdp, &report_pipe[1], /* pre-exec callback */
254 NULL, /* standard_input */
255 NULL, /* standard_output */
256 NULL, /* standard_error */
258 g_slist_foreach (free_list, (void(*)(void*,void*)) g_free, NULL);
259 g_slist_free (free_list);
261 close (report_pipe[1]);
264 g_print ("(pid=%lu)\n", (unsigned long) pid);
268 close (report_pipe[0]);
269 if (subtest_mode_fatal)
270 g_error ("Failed to execute test binary: %s: %s", argv[0], error->message);
272 g_warning ("Failed to execute test binary: %s: %s", argv[0], error->message);
273 g_clear_error (&error);
277 subtest_running = TRUE;
278 subtest_io_pending = TRUE;
279 tlb = g_test_log_buffer_new();
280 if (report_pipe[0] >= 0)
282 ioc_report = g_io_channel_unix_new (report_pipe[0]);
283 g_io_channel_set_flags (ioc_report, G_IO_FLAG_NONBLOCK, NULL);
284 g_io_channel_set_encoding (ioc_report, NULL, NULL);
285 g_io_channel_set_buffered (ioc_report, FALSE);
286 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);
287 g_io_channel_unref (ioc_report);
289 g_child_watch_add_full (G_PRIORITY_DEFAULT + 1, pid, child_watch_cb, NULL, NULL);
291 while (subtest_running || /* FALSE once child exits */
292 subtest_io_pending || /* FALSE once ioc_report closes */
293 g_main_context_pending (NULL)) /* TRUE while idler, etc are running */
294 g_main_context_iteration (NULL, TRUE);
296 close (report_pipe[0]);
297 g_test_log_buffer_free (tlb);
303 launch_test (const char *binary)
306 GTimer *btimer = g_timer_new();
307 subtest_faill = FALSE;
309 g_print ("TEST: %s... ", binary);
310 test_log_printfe ("%s<testbinary path=\"%s\">\n", sindent (log_indent), binary);
312 g_timer_start (btimer);
313 success = launch_test_binary (binary);
314 g_timer_stop (btimer);
315 test_log_printfe ("%s<duration>%.6f</duration>\n", sindent (log_indent), g_timer_elapsed (btimer, NULL));
317 test_log_printfe ("%s</testbinary>\n", sindent (log_indent));
319 g_print ("%s: %s\n", subtest_faill || !success ? "FAIL" : "PASS", binary);
320 g_timer_destroy (btimer);
324 usage (gboolean just_version)
328 g_print ("gtester version %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
331 g_print ("Usage: gtester [OPTIONS] testprogram...\n");
332 /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
333 g_print ("Options:\n");
334 g_print (" -h, --help show this help message\n");
335 g_print (" -v, --version print version informations\n");
336 g_print (" --g-fatal-warnings make warnings fatal (abort)\n");
337 g_print (" -k, --keep-going continue running after tests failed\n");
338 g_print (" -l list paths of available test cases\n");
339 g_print (" -m=perf, -m=slow, -m=quick run test cases in mode perf, slow or quick (default)\n");
340 g_print (" -p=TESTPATH only start test cases matching TESTPATH\n");
341 g_print (" --seed=SEEDSTRING start all tests with random number seed SEEDSTRING\n");
342 g_print (" -o=LOGFILE write the test log to LOGFILE\n");
343 g_print (" -q, --quiet suppress per test binary output\n");
344 g_print (" --verbose report success per testcase\n");
348 parse_args (gint *argc_p,
351 guint argc = *argc_p;
352 gchar **argv = *argv_p;
354 /* parse known args */
355 for (i = 1; i < argc; i++)
357 if (strcmp (argv[i], "--g-fatal-warnings") == 0)
359 GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
360 fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
361 g_log_set_always_fatal (fatal_mask);
364 else if (strcmp (argv[i], "-h") == 0 || strcmp (argv[i], "--help") == 0)
370 else if (strcmp (argv[i], "-v") == 0 || strcmp (argv[i], "--version") == 0)
376 else if (strcmp (argv[i], "--keep-going") == 0 ||
377 strcmp (argv[i], "-k") == 0)
379 subtest_mode_fatal = FALSE;
382 else if (strcmp ("-p", argv[i]) == 0 || strncmp ("-p=", argv[i], 3) == 0)
384 gchar *equal = argv[i] + 2;
386 subtest_paths = g_slist_prepend (subtest_paths, equal + 1);
387 else if (i + 1 < argc)
390 subtest_paths = g_slist_prepend (subtest_paths, argv[i]);
394 else if (strcmp ("-o", argv[i]) == 0 || strncmp ("-o=", argv[i], 3) == 0)
396 gchar *equal = argv[i] + 2;
398 output_filename = equal + 1;
399 else if (i + 1 < argc)
402 output_filename = argv[i];
406 else if (strcmp ("-m", argv[i]) == 0 || strncmp ("-m=", argv[i], 3) == 0)
408 gchar *equal = argv[i] + 2;
409 const gchar *mode = "";
412 else if (i + 1 < argc)
417 if (strcmp (mode, "perf") == 0)
418 subtest_mode_perf = TRUE;
419 else if (strcmp (mode, "slow") == 0)
420 subtest_mode_quick = FALSE;
421 else if (strcmp (mode, "quick") == 0)
423 subtest_mode_quick = TRUE;
424 subtest_mode_perf = FALSE;
427 g_error ("unknown test mode: -m %s", mode);
430 else if (strcmp ("-q", argv[i]) == 0 || strcmp ("--quiet", argv[i]) == 0)
432 gtester_quiet = TRUE;
433 gtester_verbose = FALSE;
436 else if (strcmp ("--verbose", argv[i]) == 0)
438 gtester_quiet = FALSE;
439 gtester_verbose = TRUE;
442 else if (strcmp ("-l", argv[i]) == 0)
444 gtester_list_tests = TRUE;
447 else if (strcmp ("--seed", argv[i]) == 0 || strncmp ("--seed=", argv[i], 7) == 0)
449 gchar *equal = argv[i] + 6;
451 subtest_seedstr = equal + 1;
452 else if (i + 1 < argc)
455 subtest_seedstr = argv[i];
462 for (i = 1; i < argc; i++)
478 /* some unices need SA_RESTART for SIGCHLD to return -EAGAIN for io.
479 * we must fiddle with sigaction() *before* glib is used, otherwise
480 * we could revoke signal hanmdler setups from glib initialization code.
485 struct sigaction osa;
486 sa.sa_handler = SIG_DFL;
487 sigfillset (&sa.sa_mask);
488 sa.sa_flags = SA_RESTART;
489 sigaction (SIGCHLD, &sa, &osa);
492 g_set_prgname (argv[0]);
493 parse_args (&argc, &argv);
503 log_fd = g_open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
505 g_error ("Failed to open log file '%s': %s", output_filename, g_strerror (errno));
508 test_log_printfe ("<?xml version=\"1.0\"?>\n");
509 test_log_printfe ("%s<gtester>\n", sindent (log_indent));
511 for (ui = 1; ui < argc; ui++)
513 const char *binary = argv[ui];
514 launch_test (binary);
515 /* we only get here on success or if !subtest_mode_fatal */
518 test_log_printfe ("%s</gtester>\n", sindent (log_indent));