int main (int argc, char **argv) { guint ui; /* See #578295 */ g_timeout_add_seconds (1, do_nothing, NULL); /* some unices need SA_RESTART for SIGCHLD to return -EAGAIN for io. * we must fiddle with sigaction() *before* glib is used, otherwise * we could revoke signal hanmdler setups from glib initialization code. */ if (TRUE) { struct sigaction sa; struct sigaction osa; sa.sa_handler = SIG_DFL; sigfillset (&sa.sa_mask); sa.sa_flags = SA_RESTART; sigaction (SIGCHLD, &sa, &osa); } g_set_prgname (argv[0]); parse_args (&argc, &argv); if (gtester_selftest) return main_selftest (argc, argv); if (argc <= 1) { usage (FALSE); return 1; } if (output_filename) { log_fd = g_open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); if (log_fd < 0) g_error ("Failed to open log file '%s': %s", output_filename, g_strerror (errno)); } test_log_printfe ("<?xml version=\"1.0\"?>\n"); test_log_printfe ("%s<gtester>\n", sindent (log_indent)); log_indent += 2; for (ui = 1; ui < argc; ui++) { const char *binary = argv[ui]; launch_test (binary); /* we only get here on success or if !subtest_mode_fatal */ } log_indent -= 2; test_log_printfe ("%s</gtester>\n", sindent (log_indent)); close (log_fd); return testcase_fail_count == 0 ? 0 : 1; }
static void launch_test (const char *binary) { gboolean success = TRUE; GTimer *btimer = g_timer_new(); gboolean need_restart; testcase_count = 0; if (!gtester_quiet) g_print ("TEST: %s... ", binary); retry: test_log_printfe ("%s<testbinary path=\"%s\">\n", sindent (log_indent), binary); log_indent += 2; g_timer_start (btimer); subtest_exitstatus = 0; success &= launch_test_binary (binary, testcase_count); success &= subtest_exitstatus == 0; need_restart = testcase_open != 0; if (testcase_open) testcase_close (0, -256, 0); g_timer_stop (btimer); test_log_printfe ("%s<duration>%.6f</duration>\n", sindent (log_indent), g_timer_elapsed (btimer, NULL)); log_indent -= 2; test_log_printfe ("%s</testbinary>\n", sindent (log_indent)); g_free (subtest_last_seed); subtest_last_seed = NULL; if (need_restart) { /* restart test binary, skipping processed test cases */ goto retry; } /* count the inability to run a test as a failure */ if (!success && testcase_count == 0) testcase_fail_count++; if (!gtester_quiet) g_print ("%s: %s\n", !success ? "FAIL" : "PASS", binary); g_timer_destroy (btimer); if (subtest_mode_fatal && !success) terminate(); }
int main (int argc, char **argv) { guint ui; g_set_prgname (argv[0]); parse_args (&argc, &argv); if (gtester_selftest) return main_selftest (argc, argv); if (argc <= 1) { usage (FALSE); return 1; } if (output_filename) { int errsv; log_fd = g_open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); errsv = errno; if (log_fd < 0) g_error ("Failed to open log file '%s': %s", output_filename, g_strerror (errsv)); } test_log_printfe ("<?xml version=\"1.0\"?>\n"); test_log_printfe ("%s<gtester>\n", sindent (log_indent)); log_indent += 2; for (ui = 1; ui < argc; ui++) { const char *binary = argv[ui]; launch_test (binary); /* we only get here on success or if !subtest_mode_fatal */ } log_indent -= 2; test_log_printfe ("%s</gtester>\n", sindent (log_indent)); close (log_fd); return testcase_fail_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE; }
static void testcase_close (long double duration, gint exit_status, guint n_forks) { g_return_if_fail (testcase_open > 0); test_log_printfe ("%s<duration>%.6Lf</duration>\n", sindent (log_indent), duration); test_log_printfe ("%s<status exit-status=\"%d\" n-forks=\"%d\" result=\"%s\"/>\n", sindent (log_indent), exit_status, n_forks, exit_status ? "failed" : "success"); log_indent -= 2; test_log_printfe ("%s</testcase>\n", sindent (log_indent)); testcase_open--; if (gtester_verbose) g_print ("%s\n", exit_status ? "FAIL" : "OK"); if (exit_status && subtest_last_seed) g_print ("GTester: last random seed: %s\n", subtest_last_seed); if (exit_status) testcase_fail_count += 1; if (subtest_mode_fatal && testcase_fail_count) terminate(); }
static void testcase_close (long double duration, gint exit_status, guint n_forks) { gboolean success; g_return_if_fail (testcase_open > 0); test_log_printfe ("%s<duration>%.6Lf</duration>\n", sindent (log_indent), duration); success = exit_status == G_TEST_RUN_SUCCESS || exit_status == G_TEST_RUN_SKIPPED; test_log_printfe ("%s<status exit-status=\"%d\" n-forks=\"%d\" result=\"%s\"/>\n", sindent (log_indent), exit_status, n_forks, success ? "success" : "failed"); log_indent -= 2; test_log_printfe ("%s</testcase>\n", sindent (log_indent)); testcase_open--; if (gtester_verbose) { switch (exit_status) { case G_TEST_RUN_SUCCESS: g_print ("OK\n"); break; case G_TEST_RUN_SKIPPED: g_print ("SKIP\n"); break; default: g_print ("FAIL\n"); break; } } if (!success && subtest_last_seed) g_print ("GTester: last random seed: %s\n", subtest_last_seed); if (!success) testcase_fail_count += 1; if (subtest_mode_fatal && !success) terminate(); }
static void test_log_msg (GTestLogMsg *msg) { switch (msg->log_type) { guint i; gchar **strv; case G_TEST_LOG_NONE: case G_TEST_LOG_START_SUITE: case G_TEST_LOG_STOP_SUITE: break; case G_TEST_LOG_ERROR: strv = g_strsplit (msg->strings[0], "\n", -1); for (i = 0; strv[i]; i++) test_log_printfe ("%s<error>%s</error>\n", sindent (log_indent), strv[i]); g_strfreev (strv); break; case G_TEST_LOG_START_BINARY: test_log_printfe ("%s<binary file=\"%s\"/>\n", sindent (log_indent), msg->strings[0]); subtest_last_seed = g_strdup (msg->strings[1]); test_log_printfe ("%s<random-seed>%s</random-seed>\n", sindent (log_indent), subtest_last_seed); break; case G_TEST_LOG_LIST_CASE: g_print ("%s\n", msg->strings[0]); break; case G_TEST_LOG_START_CASE: testcase_count++; if (gtester_verbose) { gchar *sc = g_strconcat (msg->strings[0], ":", NULL); gchar *sleft = g_strdup_printf ("%-68s", sc); g_free (sc); g_print ("%70s ", sleft); g_free (sleft); } g_return_if_fail (testcase_open == 0); testcase_open++; test_log_printfe ("%s<testcase path=\"%s\">\n", sindent (log_indent), msg->strings[0]); log_indent += 2; break; case G_TEST_LOG_SKIP_CASE: if (FALSE && gtester_verbose) /* enable to debug test case skipping logic */ { gchar *sc = g_strconcat (msg->strings[0], ":", NULL); gchar *sleft = g_strdup_printf ("%-68s", sc); g_free (sc); g_print ("%70s SKIPPED\n", sleft); g_free (sleft); } test_log_printfe ("%s<testcase path=\"%s\" skipped=\"1\"/>\n", sindent (log_indent), msg->strings[0]); break; case G_TEST_LOG_STOP_CASE: testcase_close (msg->nums[2], (int) msg->nums[0], (int) msg->nums[1]); break; case G_TEST_LOG_MIN_RESULT: case G_TEST_LOG_MAX_RESULT: test_log_printfe ("%s<performance minimize=\"%d\" maximize=\"%d\" value=\"%.16Lg\">\n", sindent (log_indent), msg->log_type == G_TEST_LOG_MIN_RESULT, msg->log_type == G_TEST_LOG_MAX_RESULT, msg->nums[0]); test_log_printfe ("%s%s\n", sindent (log_indent + 2), msg->strings[0]); test_log_printfe ("%s</performance>\n", sindent (log_indent)); break; case G_TEST_LOG_MESSAGE: test_log_printfe ("%s<message>\n%s\n%s</message>\n", sindent (log_indent), msg->strings[0], sindent (log_indent)); break; } }