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
*skipped_paths
= NULL
;
57 static GSList
*subtest_args
= NULL
;
58 static gboolean testcase_open
= FALSE
;
59 static guint testcase_count
= 0;
60 static guint testcase_fail_count
= 0;
61 static const gchar
*output_filename
= NULL
;
62 static guint log_indent
= 0;
63 static gint log_fd
= -1;
65 /* --- functions --- */
69 static const char spaces
[] = " ";
70 int l
= sizeof (spaces
) - 1;
72 return spaces
+ l
- n
;
75 static void G_GNUC_PRINTF (1, 2)
76 test_log_printfe (const char *format
,
82 va_start (args
, format
);
83 result
= g_markup_vprintf_escaped (format
, args
);
86 r
= write (log_fd
, result
, strlen (result
));
87 while (r
< 0 && errno
== EINTR
);
94 kill (getpid(), SIGTERM
);
99 testcase_close (long double duration
,
103 g_return_if_fail (testcase_open
> 0);
104 test_log_printfe ("%s<duration>%.6Lf</duration>\n", sindent (log_indent
), duration
);
105 test_log_printfe ("%s<status exit-status=\"%d\" n-forks=\"%d\" result=\"%s\"/>\n",
106 sindent (log_indent
), exit_status
, n_forks
,
107 exit_status
? "failed" : "success");
109 test_log_printfe ("%s</testcase>\n", sindent (log_indent
));
112 g_print ("%s\n", exit_status
? "FAIL" : "OK");
113 if (exit_status
&& subtest_last_seed
)
114 g_print ("GTester: last random seed: %s\n", subtest_last_seed
);
116 testcase_fail_count
+= 1;
117 if (subtest_mode_fatal
&& exit_status
)
122 test_log_msg (GTestLogMsg
*msg
)
124 switch (msg
->log_type
)
128 case G_TEST_LOG_NONE
:
130 case G_TEST_LOG_ERROR
:
131 strv
= g_strsplit (msg
->strings
[0], "\n", -1);
132 for (i
= 0; strv
[i
]; i
++)
133 test_log_printfe ("%s<error>%s</error>\n", sindent (log_indent
), strv
[i
]);
136 case G_TEST_LOG_START_BINARY
:
137 test_log_printfe ("%s<binary file=\"%s\"/>\n", sindent (log_indent
), msg
->strings
[0]);
138 subtest_last_seed
= g_strdup (msg
->strings
[1]);
139 test_log_printfe ("%s<random-seed>%s</random-seed>\n", sindent (log_indent
), subtest_last_seed
);
141 case G_TEST_LOG_LIST_CASE
:
142 g_print ("%s\n", msg
->strings
[0]);
144 case G_TEST_LOG_START_CASE
:
148 gchar
*sc
= g_strconcat (msg
->strings
[0], ":", NULL
);
149 gchar
*sleft
= g_strdup_printf ("%-68s", sc
);
151 g_print ("%70s ", sleft
);
154 g_return_if_fail (testcase_open
== 0);
156 test_log_printfe ("%s<testcase path=\"%s\">\n", sindent (log_indent
), msg
->strings
[0]);
159 case G_TEST_LOG_SKIP_CASE
:
160 if (FALSE
&& gtester_verbose
) /* enable to debug test case skipping logic */
162 gchar
*sc
= g_strconcat (msg
->strings
[0], ":", NULL
);
163 gchar
*sleft
= g_strdup_printf ("%-68s", sc
);
165 g_print ("%70s SKIPPED\n", sleft
);
168 test_log_printfe ("%s<testcase path=\"%s\" skipped=\"1\"/>\n", sindent (log_indent
), msg
->strings
[0]);
170 case G_TEST_LOG_STOP_CASE
:
171 testcase_close (msg
->nums
[2], (int) msg
->nums
[0], (int) msg
->nums
[1]);
173 case G_TEST_LOG_MIN_RESULT
:
174 case G_TEST_LOG_MAX_RESULT
:
175 test_log_printfe ("%s<performance minimize=\"%d\" maximize=\"%d\" value=\"%.16Lg\">\n",
176 sindent (log_indent
), msg
->log_type
== G_TEST_LOG_MIN_RESULT
, msg
->log_type
== G_TEST_LOG_MAX_RESULT
, msg
->nums
[0]);
177 test_log_printfe ("%s%s\n", sindent (log_indent
+ 2), msg
->strings
[0]);
178 test_log_printfe ("%s</performance>\n", sindent (log_indent
));
180 case G_TEST_LOG_MESSAGE
:
181 test_log_printfe ("%s<message>\n%s\n%s</message>\n", sindent (log_indent
), msg
->strings
[0], sindent (log_indent
));
187 child_report_cb (GIOChannel
*source
,
188 GIOCondition condition
,
191 GTestLogBuffer
*tlb
= data
;
192 GIOStatus status
= G_IO_STATUS_NORMAL
;
193 gboolean first_read_eof
= FALSE
, first_read
= TRUE
;
197 guint8 buffer
[READ_BUFFER_SIZE
];
198 GError
*error
= NULL
;
199 status
= g_io_channel_read_chars (source
, (gchar
*) buffer
, sizeof (buffer
), &length
, &error
);
200 if (first_read
&& (condition
& G_IO_IN
))
202 /* on some unixes (MacOS) we need to detect non-blocking fd EOF
203 * by an IO_IN select/poll followed by read()==0.
205 first_read_eof
= length
== 0;
211 g_test_log_buffer_push (tlb
, length
, buffer
);
214 msg
= g_test_log_buffer_pop (tlb
);
218 g_test_log_msg_free (msg
);
223 g_clear_error (&error
);
224 /* ignore the io channel status, which will report intermediate EOFs for non blocking fds */
228 /* g_print ("LASTIOSTATE: first_read_eof=%d condition=%d\n", first_read_eof, condition); */
229 if (first_read_eof
|| (condition
& (G_IO_ERR
| G_IO_HUP
)))
231 /* if there's no data to read and select() reports an error or hangup,
232 * the fd must have been closed remotely
234 subtest_io_pending
= FALSE
;
237 return TRUE
; /* keep polling */
241 child_watch_cb (GPid pid
,
245 g_spawn_close_pid (pid
);
246 if (WIFEXITED (status
)) /* normal exit */
247 subtest_exitstatus
= WEXITSTATUS (status
);
248 else /* signal or core dump, etc */
249 subtest_exitstatus
= 0xffffffff;
250 subtest_running
= FALSE
;
254 queue_gfree (GSList
**slistp
,
257 *slistp
= g_slist_prepend (*slistp
, string
);
262 unset_cloexec_fdp (gpointer fdp_data
)
264 int r
, *fdp
= fdp_data
;
266 r
= fcntl (*fdp
, F_SETFD
, 0 /* FD_CLOEXEC */);
267 while (r
< 0 && errno
== EINTR
);
271 launch_test_binary (const char *binary
,
275 GSList
*slist
, *free_list
= NULL
;
276 GError
*error
= NULL
;
280 gint report_pipe
[2] = { -1, -1 };
281 guint child_report_cb_id
= 0;
282 gboolean loop_pending
;
285 if (pipe (report_pipe
) < 0)
287 if (subtest_mode_fatal
)
288 g_error ("Failed to open pipe for test binary: %s: %s", binary
, g_strerror (errno
));
290 g_warning ("Failed to open pipe for test binary: %s: %s", binary
, g_strerror (errno
));
295 for (slist
= subtest_args
; slist
; slist
= slist
->next
)
302 if (!subtest_mode_fatal
)
304 if (subtest_mode_quick
)
308 if (subtest_mode_perf
)
310 if (gtester_list_tests
)
317 for (slist
= subtest_paths
; slist
; slist
= slist
->next
)
319 for (slist
= skipped_paths
; slist
; slist
= slist
->next
)
323 argv
= g_malloc ((argc
+ 2) * sizeof(gchar
*));
325 for (slist
= subtest_args
; slist
; slist
= slist
->next
)
326 argv
[i
++] = (gchar
*) slist
->data
;
327 /* argv[i++] = "--debug-log"; */
329 argv
[i
++] = "--quiet";
331 argv
[i
++] = "--verbose";
332 if (!subtest_mode_fatal
)
333 argv
[i
++] = "--keep-going";
334 if (subtest_mode_quick
)
335 argv
[i
++] = "-m=quick";
337 argv
[i
++] = "-m=slow";
338 if (subtest_mode_perf
)
339 argv
[i
++] = "-m=perf";
340 if (gtester_list_tests
)
343 argv
[i
++] = queue_gfree (&free_list
, g_strdup_printf ("--seed=%s", subtest_seedstr
));
344 argv
[i
++] = queue_gfree (&free_list
, g_strdup_printf ("--GTestLogFD=%u", report_pipe
[1]));
346 argv
[i
++] = queue_gfree (&free_list
, g_strdup_printf ("--GTestSkipCount=%u", skip_tests
));
347 for (slist
= subtest_paths
; slist
; slist
= slist
->next
)
348 argv
[i
++] = queue_gfree (&free_list
, g_strdup_printf ("-p=%s", (gchar
*) slist
->data
));
349 for (slist
= skipped_paths
; slist
; slist
= slist
->next
)
350 argv
[i
++] = queue_gfree (&free_list
, g_strdup_printf ("-s=%s", (gchar
*) slist
->data
));
353 g_spawn_async_with_pipes (NULL
, /* g_get_current_dir() */
356 G_SPAWN_DO_NOT_REAP_CHILD
, /* G_SPAWN_SEARCH_PATH */
357 unset_cloexec_fdp
, &report_pipe
[1], /* pre-exec callback */
359 NULL
, /* standard_input */
360 NULL
, /* standard_output */
361 NULL
, /* standard_error */
363 g_slist_foreach (free_list
, (void(*)(void*,void*)) g_free
, NULL
);
364 g_slist_free (free_list
);
366 close (report_pipe
[1]);
369 g_print ("(pid=%lu)\n", (unsigned long) pid
);
373 close (report_pipe
[0]);
374 if (subtest_mode_fatal
)
375 g_error ("Failed to execute test binary: %s: %s", argv
[0], error
->message
);
377 g_warning ("Failed to execute test binary: %s: %s", argv
[0], error
->message
);
378 g_clear_error (&error
);
384 subtest_running
= TRUE
;
385 subtest_io_pending
= TRUE
;
386 tlb
= g_test_log_buffer_new();
387 if (report_pipe
[0] >= 0)
389 ioc_report
= g_io_channel_unix_new (report_pipe
[0]);
390 g_io_channel_set_flags (ioc_report
, G_IO_FLAG_NONBLOCK
, NULL
);
391 g_io_channel_set_encoding (ioc_report
, NULL
, NULL
);
392 g_io_channel_set_buffered (ioc_report
, FALSE
);
393 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
);
394 g_io_channel_unref (ioc_report
);
396 g_child_watch_add_full (G_PRIORITY_DEFAULT
+ 1, pid
, child_watch_cb
, NULL
, NULL
);
398 loop_pending
= g_main_context_pending (NULL
);
399 while (subtest_running
|| /* FALSE once child exits */
400 subtest_io_pending
|| /* FALSE once ioc_report closes */
401 loop_pending
) /* TRUE while idler, etc are running */
403 /* g_print ("LOOPSTATE: subtest_running=%d subtest_io_pending=%d\n", subtest_running, subtest_io_pending); */
404 /* check for unexpected hangs that are not signalled on report_pipe */
405 if (!subtest_running
&& /* child exited */
406 subtest_io_pending
&& /* no EOF detected on report_pipe */
407 !loop_pending
) /* no IO events pending however */
409 g_main_context_iteration (NULL
, TRUE
);
410 loop_pending
= g_main_context_pending (NULL
);
413 g_source_remove (child_report_cb_id
);
414 close (report_pipe
[0]);
415 g_test_log_buffer_free (tlb
);
421 launch_test (const char *binary
)
423 gboolean success
= TRUE
;
424 GTimer
*btimer
= g_timer_new();
425 gboolean need_restart
;
429 g_print ("TEST: %s... ", binary
);
432 test_log_printfe ("%s<testbinary path=\"%s\">\n", sindent (log_indent
), binary
);
434 g_timer_start (btimer
);
435 subtest_exitstatus
= 0;
436 success
&= launch_test_binary (binary
, testcase_count
);
437 success
&= subtest_exitstatus
== 0;
438 need_restart
= testcase_open
!= 0;
440 testcase_close (0, -256, 0);
441 g_timer_stop (btimer
);
442 test_log_printfe ("%s<duration>%.6f</duration>\n", sindent (log_indent
), g_timer_elapsed (btimer
, NULL
));
444 test_log_printfe ("%s</testbinary>\n", sindent (log_indent
));
445 g_free (subtest_last_seed
);
446 subtest_last_seed
= NULL
;
449 /* restart test binary, skipping processed test cases */
453 /* count the inability to run a test as a failure */
454 if (!success
&& testcase_count
== 0)
455 testcase_fail_count
++;
458 g_print ("%s: %s\n", !success
? "FAIL" : "PASS", binary
);
459 g_timer_destroy (btimer
);
460 if (subtest_mode_fatal
&& !success
)
465 usage (gboolean just_version
)
469 g_print ("gtester version %d.%d.%d\n", GLIB_MAJOR_VERSION
, GLIB_MINOR_VERSION
, GLIB_MICRO_VERSION
);
472 g_print ("Usage: gtester [OPTIONS] testprogram...\n");
473 /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
474 g_print ("Options:\n");
475 g_print (" -h, --help show this help message\n");
476 g_print (" -v, --version print version informations\n");
477 g_print (" --g-fatal-warnings make warnings fatal (abort)\n");
478 g_print (" -k, --keep-going continue running after tests failed\n");
479 g_print (" -l list paths of available test cases\n");
480 g_print (" -m=perf, -m=slow, -m=quick -m=thorough\n");
481 g_print (" run test cases in mode perf, slow/thorough or quick (default)\n");
482 g_print (" -p=TESTPATH only start test cases matching TESTPATH\n");
483 g_print (" -s=TESTPATH skip test cases matching TESTPATH\n");
484 g_print (" --seed=SEEDSTRING start all tests with random number seed SEEDSTRING\n");
485 g_print (" -o=LOGFILE write the test log to LOGFILE\n");
486 g_print (" -q, --quiet suppress per test binary output\n");
487 g_print (" --verbose report success per testcase\n");
491 parse_args (gint
*argc_p
,
494 guint argc
= *argc_p
;
495 gchar
**argv
= *argv_p
;
497 /* parse known args */
498 for (i
= 1; i
< argc
; i
++)
500 if (strcmp (argv
[i
], "--g-fatal-warnings") == 0)
502 GLogLevelFlags fatal_mask
= (GLogLevelFlags
) g_log_set_always_fatal ((GLogLevelFlags
) G_LOG_FATAL_MASK
);
503 fatal_mask
= (GLogLevelFlags
) (fatal_mask
| G_LOG_LEVEL_WARNING
| G_LOG_LEVEL_CRITICAL
);
504 g_log_set_always_fatal (fatal_mask
);
507 else if (strcmp (argv
[i
], "--gtester-selftest") == 0)
509 gtester_selftest
= TRUE
;
511 break; /* stop parsing regular gtester arguments */
513 else if (strcmp (argv
[i
], "-h") == 0 || strcmp (argv
[i
], "--help") == 0)
519 else if (strcmp (argv
[i
], "-v") == 0 || strcmp (argv
[i
], "--version") == 0)
525 else if (strcmp (argv
[i
], "--keep-going") == 0 ||
526 strcmp (argv
[i
], "-k") == 0)
528 subtest_mode_fatal
= FALSE
;
531 else if (strcmp ("-p", argv
[i
]) == 0 || strncmp ("-p=", argv
[i
], 3) == 0)
533 gchar
*equal
= argv
[i
] + 2;
535 subtest_paths
= g_slist_prepend (subtest_paths
, equal
+ 1);
536 else if (i
+ 1 < argc
)
539 subtest_paths
= g_slist_prepend (subtest_paths
, argv
[i
]);
543 else if (strcmp ("-s", argv
[i
]) == 0 || strncmp ("-s=", argv
[i
], 3) == 0)
545 gchar
*equal
= argv
[i
] + 2;
547 skipped_paths
= g_slist_prepend (skipped_paths
, equal
+ 1);
548 else if (i
+ 1 < argc
)
551 skipped_paths
= g_slist_prepend (skipped_paths
, argv
[i
]);
555 else if (strcmp ("--test-arg", argv
[i
]) == 0 || strncmp ("--test-arg=", argv
[i
], 11) == 0)
557 gchar
*equal
= argv
[i
] + 10;
559 subtest_args
= g_slist_prepend (subtest_args
, equal
+ 1);
560 else if (i
+ 1 < argc
)
563 subtest_args
= g_slist_prepend (subtest_args
, argv
[i
]);
567 else if (strcmp ("-o", argv
[i
]) == 0 || strncmp ("-o=", argv
[i
], 3) == 0)
569 gchar
*equal
= argv
[i
] + 2;
571 output_filename
= equal
+ 1;
572 else if (i
+ 1 < argc
)
575 output_filename
= argv
[i
];
579 else if (strcmp ("-m", argv
[i
]) == 0 || strncmp ("-m=", argv
[i
], 3) == 0)
581 gchar
*equal
= argv
[i
] + 2;
582 const gchar
*mode
= "";
585 else if (i
+ 1 < argc
)
590 if (strcmp (mode
, "perf") == 0)
591 subtest_mode_perf
= TRUE
;
592 else if (strcmp (mode
, "slow") == 0 || strcmp (mode
, "thorough") == 0)
593 subtest_mode_quick
= FALSE
;
594 else if (strcmp (mode
, "quick") == 0)
596 subtest_mode_quick
= TRUE
;
597 subtest_mode_perf
= FALSE
;
600 g_error ("unknown test mode: -m %s", mode
);
603 else if (strcmp ("-q", argv
[i
]) == 0 || strcmp ("--quiet", argv
[i
]) == 0)
605 gtester_quiet
= TRUE
;
606 gtester_verbose
= FALSE
;
609 else if (strcmp ("--verbose", argv
[i
]) == 0)
611 gtester_quiet
= FALSE
;
612 gtester_verbose
= TRUE
;
615 else if (strcmp ("-l", argv
[i
]) == 0)
617 gtester_list_tests
= TRUE
;
620 else if (strcmp ("--seed", argv
[i
]) == 0 || strncmp ("--seed=", argv
[i
], 7) == 0)
622 gchar
*equal
= argv
[i
] + 6;
624 subtest_seedstr
= equal
+ 1;
625 else if (i
+ 1 < argc
)
628 subtest_seedstr
= argv
[i
];
635 for (i
= 1; i
< argc
; i
++)
651 g_set_prgname (argv
[0]);
652 parse_args (&argc
, &argv
);
653 if (gtester_selftest
)
654 return main_selftest (argc
, argv
);
664 log_fd
= g_open (output_filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
666 g_error ("Failed to open log file '%s': %s", output_filename
, g_strerror (errno
));
669 test_log_printfe ("<?xml version=\"1.0\"?>\n");
670 test_log_printfe ("%s<gtester>\n", sindent (log_indent
));
672 for (ui
= 1; ui
< argc
; ui
++)
674 const char *binary
= argv
[ui
];
675 launch_test (binary
);
676 /* we only get here on success or if !subtest_mode_fatal */
679 test_log_printfe ("%s</gtester>\n", sindent (log_indent
));
683 return testcase_fail_count
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
687 fixture_setup (guint
*fix
,
688 gconstpointer test_data
)
690 g_assert_cmphex (*fix
, ==, 0);
694 fixture_test (guint
*fix
,
695 gconstpointer test_data
)
697 g_assert_cmphex (*fix
, ==, 0xdeadbeef);
698 g_test_message ("This is a test message API test message.");
699 g_test_bug_base ("http://www.example.com/bugtracker/");
701 g_test_bug_base ("http://www.example.com/bugtracker?bugnum=%s;cmd=showbug");
705 fixture_teardown (guint
*fix
,
706 gconstpointer test_data
)
708 g_assert_cmphex (*fix
, ==, 0xdeadbeef);
712 main_selftest (int argc
,
715 /* gtester main() for --gtester-selftest invokations */
716 g_test_init (&argc
, &argv
, NULL
);
717 g_test_add ("/gtester/fixture-test", guint
, NULL
, fixture_setup
, fixture_test
, fixture_teardown
);