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/>.
21 #include <glib-unix.h>
31 /* the read buffer size in bytes */
32 #define READ_BUFFER_SIZE 4096
34 /* --- prototypes --- */
35 static int main_selftest (int argc
,
37 static void parse_args (gint
*argc_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 --- */
71 static const char spaces
[] = " ";
72 int l
= sizeof (spaces
) - 1;
74 return spaces
+ l
- n
;
77 static void G_GNUC_PRINTF (1, 2)
78 test_log_printfe (const char *format
,
84 va_start (args
, format
);
85 result
= g_markup_vprintf_escaped (format
, args
);
88 r
= write (log_fd
, result
, strlen (result
));
89 while (r
< 0 && errno
== EINTR
);
96 kill (getpid(), SIGTERM
);
101 testcase_close (long double duration
,
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");
114 test_log_printfe ("%s</testcase>\n", sindent (log_indent
));
120 case G_TEST_RUN_SUCCESS
:
123 case G_TEST_RUN_SKIPPED
:
131 if (!success
&& subtest_last_seed
)
132 g_print ("GTester: last random seed: %s\n", subtest_last_seed
);
134 testcase_fail_count
+= 1;
135 if (subtest_mode_fatal
&& !success
)
140 test_log_msg (GTestLogMsg
*msg
)
142 switch (msg
->log_type
)
146 case G_TEST_LOG_NONE
:
147 case G_TEST_LOG_START_SUITE
:
148 case G_TEST_LOG_STOP_SUITE
:
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
]);
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
);
161 case G_TEST_LOG_LIST_CASE
:
162 g_print ("%s\n", msg
->strings
[0]);
164 case G_TEST_LOG_START_CASE
:
168 gchar
*sc
= g_strconcat (msg
->strings
[0], ":", NULL
);
169 gchar
*sleft
= g_strdup_printf ("%-68s", sc
);
171 g_print ("%70s ", sleft
);
174 g_return_if_fail (testcase_open
== 0);
176 test_log_printfe ("%s<testcase path=\"%s\">\n", sindent (log_indent
), msg
->strings
[0]);
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
);
185 g_print ("%70s SKIPPED\n", sleft
);
188 test_log_printfe ("%s<testcase path=\"%s\" skipped=\"1\"/>\n", sindent (log_indent
), msg
->strings
[0]);
190 case G_TEST_LOG_STOP_CASE
:
191 testcase_close (msg
->nums
[2], (int) msg
->nums
[0], (int) msg
->nums
[1]);
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
));
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
));
207 child_report_cb (GIOChannel
*source
,
208 GIOCondition condition
,
211 GTestLogBuffer
*tlb
= data
;
212 GIOStatus status
= G_IO_STATUS_NORMAL
;
213 gboolean first_read_eof
= FALSE
, first_read
= TRUE
;
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;
231 g_test_log_buffer_push (tlb
, length
, buffer
);
234 msg
= g_test_log_buffer_pop (tlb
);
238 g_test_log_msg_free (msg
);
243 g_clear_error (&error
);
244 /* ignore the io channel status, which will report intermediate EOFs for non blocking fds */
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
;
257 return TRUE
; /* keep polling */
261 child_watch_cb (GPid pid
,
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
;
274 queue_gfree (GSList
**slistp
,
277 *slistp
= g_slist_prepend (*slistp
, string
);
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
);
291 launch_test_binary (const char *binary
,
295 GSList
*slist
, *free_list
= NULL
;
296 GError
*error
= NULL
;
300 gint report_pipe
[2] = { -1, -1 };
301 guint child_report_cb_id
= 0;
302 gboolean loop_pending
;
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
);
310 g_warning ("Failed to open pipe for test binary: %s: %s", binary
, error
->message
);
311 g_clear_error (&error
);
316 for (slist
= subtest_args
; slist
; slist
= slist
->next
)
323 if (!subtest_mode_fatal
)
325 /* Either -m=quick or -m=slow is always appended. */
327 if (subtest_mode_perf
)
329 if (!subtest_mode_undefined
)
331 if (gtester_list_tests
)
338 for (slist
= subtest_paths
; slist
; slist
= slist
->next
)
340 for (slist
= skipped_paths
; slist
; slist
= slist
->next
)
344 argv
= g_malloc ((argc
+ 2) * sizeof(gchar
*));
346 for (slist
= subtest_args
; slist
; slist
= slist
->next
)
347 argv
[i
++] = (gchar
*) slist
->data
;
348 /* argv[i++] = "--debug-log"; */
350 argv
[i
++] = "--quiet";
352 argv
[i
++] = "--verbose";
353 if (!subtest_mode_fatal
)
354 argv
[i
++] = "--keep-going";
355 if (subtest_mode_quick
)
356 argv
[i
++] = "-m=quick";
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
)
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]));
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
));
376 g_spawn_async_with_pipes (NULL
, /* g_get_current_dir() */
379 G_SPAWN_DO_NOT_REAP_CHILD
, /* G_SPAWN_SEARCH_PATH */
380 unset_cloexec_fdp
, &report_pipe
[1], /* pre-exec callback */
382 NULL
, /* standard_input */
383 NULL
, /* standard_output */
384 NULL
, /* standard_error */
386 g_slist_foreach (free_list
, (void(*)(void*,void*)) g_free
, NULL
);
387 g_slist_free (free_list
);
389 close (report_pipe
[1]);
392 g_print ("(pid=%lu)\n", (unsigned long) pid
);
396 close (report_pipe
[0]);
397 if (subtest_mode_fatal
)
398 g_error ("Failed to execute test binary: %s: %s", argv
[0], error
->message
);
400 g_warning ("Failed to execute test binary: %s: %s", argv
[0], error
->message
);
401 g_clear_error (&error
);
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 */
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
);
446 launch_test (const char *binary
)
448 gboolean success
= TRUE
;
449 GTimer
*btimer
= g_timer_new();
450 gboolean need_restart
;
454 g_print ("TEST: %s... ", binary
);
457 test_log_printfe ("%s<testbinary path=\"%s\">\n", sindent (log_indent
), binary
);
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;
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
));
469 test_log_printfe ("%s</testbinary>\n", sindent (log_indent
));
470 g_free (subtest_last_seed
);
471 subtest_last_seed
= NULL
;
474 /* restart test binary, skipping processed test cases */
478 /* count the inability to run a test as a failure */
479 if (!success
&& testcase_count
== 0)
480 testcase_fail_count
++;
483 g_print ("%s: %s\n", !success
? "FAIL" : "PASS", binary
);
484 g_timer_destroy (btimer
);
485 if (subtest_mode_fatal
&& !success
)
490 usage (gboolean just_version
)
494 g_print ("gtester version %d.%d.%d\n", GLIB_MAJOR_VERSION
, GLIB_MINOR_VERSION
, GLIB_MICRO_VERSION
);
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");
518 parse_args (gint
*argc_p
,
521 guint argc
= *argc_p
;
522 gchar
**argv
= *argv_p
;
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
);
534 else if (strcmp (argv
[i
], "--gtester-selftest") == 0)
536 gtester_selftest
= TRUE
;
538 break; /* stop parsing regular gtester arguments */
540 else if (strcmp (argv
[i
], "-h") == 0 || strcmp (argv
[i
], "--help") == 0)
546 else if (strcmp (argv
[i
], "-v") == 0 || strcmp (argv
[i
], "--version") == 0)
552 else if (strcmp (argv
[i
], "--keep-going") == 0 ||
553 strcmp (argv
[i
], "-k") == 0)
555 subtest_mode_fatal
= FALSE
;
558 else if (strcmp ("-p", argv
[i
]) == 0 || strncmp ("-p=", argv
[i
], 3) == 0)
560 gchar
*equal
= argv
[i
] + 2;
562 subtest_paths
= g_slist_prepend (subtest_paths
, equal
+ 1);
563 else if (i
+ 1 < argc
)
566 subtest_paths
= g_slist_prepend (subtest_paths
, argv
[i
]);
570 else if (strcmp ("-s", argv
[i
]) == 0 || strncmp ("-s=", argv
[i
], 3) == 0)
572 gchar
*equal
= argv
[i
] + 2;
574 skipped_paths
= g_slist_prepend (skipped_paths
, equal
+ 1);
575 else if (i
+ 1 < argc
)
578 skipped_paths
= g_slist_prepend (skipped_paths
, argv
[i
]);
582 else if (strcmp ("--test-arg", argv
[i
]) == 0 || strncmp ("--test-arg=", argv
[i
], 11) == 0)
584 gchar
*equal
= argv
[i
] + 10;
586 subtest_args
= g_slist_prepend (subtest_args
, equal
+ 1);
587 else if (i
+ 1 < argc
)
590 subtest_args
= g_slist_prepend (subtest_args
, argv
[i
]);
594 else if (strcmp ("-o", argv
[i
]) == 0 || strncmp ("-o=", argv
[i
], 3) == 0)
596 gchar
*equal
= argv
[i
] + 2;
598 output_filename
= equal
+ 1;
599 else if (i
+ 1 < argc
)
602 output_filename
= argv
[i
];
606 else if (strcmp ("-m", argv
[i
]) == 0 || strncmp ("-m=", argv
[i
], 3) == 0)
608 gchar
*equal
= argv
[i
] + 2;
609 const gchar
*mode
= "";
612 else if (i
+ 1 < argc
)
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
;
631 g_error ("unknown test mode: -m %s", mode
);
634 else if (strcmp ("-q", argv
[i
]) == 0 || strcmp ("--quiet", argv
[i
]) == 0)
636 gtester_quiet
= TRUE
;
637 gtester_verbose
= FALSE
;
640 else if (strcmp ("--verbose", argv
[i
]) == 0)
642 gtester_quiet
= FALSE
;
643 gtester_verbose
= TRUE
;
646 else if (strcmp ("-l", argv
[i
]) == 0)
648 gtester_list_tests
= TRUE
;
651 else if (strcmp ("--seed", argv
[i
]) == 0 || strncmp ("--seed=", argv
[i
], 7) == 0)
653 gchar
*equal
= argv
[i
] + 6;
655 subtest_seedstr
= equal
+ 1;
656 else if (i
+ 1 < argc
)
659 subtest_seedstr
= argv
[i
];
666 for (i
= 1; i
< argc
; i
++)
682 g_set_prgname (argv
[0]);
683 parse_args (&argc
, &argv
);
684 if (gtester_selftest
)
685 return main_selftest (argc
, argv
);
696 log_fd
= g_open (output_filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
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
));
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 */
712 test_log_printfe ("%s</gtester>\n", sindent (log_indent
));
716 return testcase_fail_count
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
720 fixture_setup (guint
*fix
,
721 gconstpointer test_data
)
723 g_assert_cmphex (*fix
, ==, 0);
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/");
734 g_test_bug_base ("http://www.example.com/bugtracker?bugnum=%s;cmd=showbug");
738 fixture_teardown (guint
*fix
,
739 gconstpointer test_data
)
741 g_assert_cmphex (*fix
, ==, 0xdeadbeef);
745 main_selftest (int argc
,
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
);