gtester.c 23.2 KB
Newer Older
1 2 3
/* GLib testing framework runner
 * Copyright (C) 2007 Sven Herzberg
 * Copyright (C) 2007 Tim Janik
4
 *
5 6 7 8
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
9
 *
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 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
14
 *
15 16 17 18
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
19
 */
20 21
#include "config.h"

22
#include <glib.h>
23
#include <glib-unix.h>
24
#include <gstdio.h>
25 26 27
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
Tim Janik's avatar
Tim Janik committed
28
#include <fcntl.h>
29
#include <sys/wait.h>
Tim Janik's avatar
Tim Janik committed
30 31
#include <errno.h>
#include <signal.h>
32

33
/* the read buffer size in bytes */
34 35 36
#define READ_BUFFER_SIZE 4096

/* --- prototypes --- */
37 38
static int      main_selftest   (int    argc,
                                 char **argv);
39 40
static void     parse_args      (gint           *argc_p,
                                 gchar        ***argv_p);
41

42
/* --- variables --- */
43
static GIOChannel  *ioc_report = NULL;
44
static gboolean     gtester_quiet = FALSE;
45 46
static gboolean     gtester_verbose = FALSE;
static gboolean     gtester_list_tests = FALSE;
47
static gboolean     gtester_selftest = FALSE;
48
static gboolean     subtest_running = FALSE;
49
static gint         subtest_exitstatus = 0;
50 51 52
static gboolean     subtest_io_pending = FALSE;
static gboolean     subtest_quiet = TRUE;
static gboolean     subtest_verbose = FALSE;
53
static gboolean     subtest_mode_fatal = TRUE;
54 55
static gboolean     subtest_mode_perf = FALSE;
static gboolean     subtest_mode_quick = TRUE;
56
static gboolean     subtest_mode_undefined = TRUE;
57
static const gchar *subtest_seedstr = NULL;
58
static gchar       *subtest_last_seed = NULL;
59
static GSList      *subtest_paths = NULL;
60
static GSList      *skipped_paths = NULL;
61
static GSList      *subtest_args = NULL;
62
static gboolean     testcase_open = FALSE;
63 64
static guint        testcase_count = 0;
static guint        testcase_fail_count = 0;
65 66 67
static const gchar *output_filename = NULL;
static guint        log_indent = 0;
static gint         log_fd = -1;
68

69
/* --- functions --- */
70 71 72 73
static const char*
sindent (guint n)
{
  static const char spaces[] = "                                                                                                    ";
74 75
  int l = sizeof (spaces) - 1;
  n = MIN (n, l);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  return spaces + l - n;
}

static void G_GNUC_PRINTF (1, 2)
test_log_printfe (const char *format,
                  ...)
{
  char *result;
  int r;
  va_list args;
  va_start (args, format);
  result = g_markup_vprintf_escaped (format, args);
  va_end (args);
  do
    r = write (log_fd, result, strlen (result));
  while (r < 0 && errno == EINTR);
  g_free (result);
}

95 96 97 98 99 100 101 102 103
static void
terminate (void)
{
  kill (getpid(), SIGTERM);
  abort();
}

static void
testcase_close (long double duration,
104
                gint        exit_status,
105 106 107 108
                guint       n_forks)
{
  g_return_if_fail (testcase_open > 0);
  test_log_printfe ("%s<duration>%.6Lf</duration>\n", sindent (log_indent), duration);
109 110 111
  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");
112 113 114 115 116
  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");
117 118
  if (exit_status && subtest_last_seed)
    g_print ("GTester: last random seed: %s\n", subtest_last_seed);
119 120
  if (exit_status)
    testcase_fail_count += 1;
121
  if (subtest_mode_fatal && exit_status)
122 123 124
    terminate();
}

125 126 127 128 129
static void
test_log_msg (GTestLogMsg *msg)
{
  switch (msg->log_type)
    {
130 131
      guint i;
      gchar **strv;
132
    case G_TEST_LOG_NONE:
133 134
    case G_TEST_LOG_START_SUITE:
    case G_TEST_LOG_STOP_SUITE:
135 136
      break;
    case G_TEST_LOG_ERROR:
137 138 139 140
      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);
141 142 143
      break;
    case G_TEST_LOG_START_BINARY:
      test_log_printfe ("%s<binary file=\"%s\"/>\n", sindent (log_indent), msg->strings[0]);
144 145
      subtest_last_seed = g_strdup (msg->strings[1]);
      test_log_printfe ("%s<random-seed>%s</random-seed>\n", sindent (log_indent), subtest_last_seed);
146 147 148 149 150
      break;
    case G_TEST_LOG_LIST_CASE:
      g_print ("%s\n", msg->strings[0]);
      break;
    case G_TEST_LOG_START_CASE:
151
      testcase_count++;
152 153 154 155 156 157 158 159 160 161 162 163 164
      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;
165
    case G_TEST_LOG_SKIP_CASE:
166
      if (FALSE && gtester_verbose) /* enable to debug test case skipping logic */
167 168 169 170 171 172 173 174 175
        {
          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;
176
    case G_TEST_LOG_STOP_CASE:
177
      testcase_close (msg->nums[2], (int) msg->nums[0], (int) msg->nums[1]);
178 179 180 181 182 183 184 185
      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;
186 187 188
    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;
189 190 191
    }
}

192
static gboolean
193 194 195
child_report_cb (GIOChannel  *source,
                 GIOCondition condition,
                 gpointer     data)
196
{
Tim Janik's avatar
Tim Janik committed
197
  GTestLogBuffer *tlb = data;
198
  GIOStatus status = G_IO_STATUS_NORMAL;
199
  gboolean first_read_eof = FALSE, first_read = TRUE;
Tim Janik's avatar
Tim Janik committed
200 201
  gsize length = 0;
  do
202
    {
Tim Janik's avatar
Tim Janik committed
203
      guint8 buffer[READ_BUFFER_SIZE];
204
      GError *error = NULL;
Tim Janik's avatar
Tim Janik committed
205
      status = g_io_channel_read_chars (source, (gchar*) buffer, sizeof (buffer), &length, &error);
206 207 208 209 210 211 212 213
      if (first_read && (condition & G_IO_IN))
        {
          /* on some unixes (MacOS) we need to detect non-blocking fd EOF
           * by an IO_IN select/poll followed by read()==0.
           */
          first_read_eof = length == 0;
        }
      first_read = FALSE;
Tim Janik's avatar
Tim Janik committed
214
      if (length)
215
        {
Tim Janik's avatar
Tim Janik committed
216 217 218 219 220 221 222
          GTestLogMsg *msg;
          g_test_log_buffer_push (tlb, length, buffer);
          do
            {
              msg = g_test_log_buffer_pop (tlb);
              if (msg)
                {
223
                  test_log_msg (msg);
Tim Janik's avatar
Tim Janik committed
224 225 226 227
                  g_test_log_msg_free (msg);
                }
            }
          while (msg);
228
        }
229
      g_clear_error (&error);
230
      /* ignore the io channel status, which will report intermediate EOFs for non blocking fds */
Tim Janik's avatar
Tim Janik committed
231
      (void) status;
232
    }
Tim Janik's avatar
Tim Janik committed
233
  while (length > 0);
234
  /* g_print ("LASTIOSTATE: first_read_eof=%d condition=%d\n", first_read_eof, condition); */
235
  if (first_read_eof || (condition & (G_IO_ERR | G_IO_HUP)))
Tim Janik's avatar
Tim Janik committed
236 237 238 239 240 241 242 243
    {
      /* if there's no data to read and select() reports an error or hangup,
       * the fd must have been closed remotely
       */
      subtest_io_pending = FALSE;
      return FALSE;
    }
  return TRUE; /* keep polling */
244 245 246 247 248 249 250 251
}

static void
child_watch_cb (GPid     pid,
		gint     status,
		gpointer data)
{
  g_spawn_close_pid (pid);
252 253 254 255
  if (WIFEXITED (status)) /* normal exit */
    subtest_exitstatus = WEXITSTATUS (status);
  else /* signal or core dump, etc */
    subtest_exitstatus = 0xffffffff;
256 257
  subtest_running = FALSE;
}
258

259 260 261 262 263 264
static gchar*
queue_gfree (GSList **slistp,
             gchar   *string)
{
  *slistp = g_slist_prepend (*slistp, string);
  return string;
265 266
}

Tim Janik's avatar
Tim Janik committed
267 268 269 270 271 272 273 274 275
static void
unset_cloexec_fdp (gpointer fdp_data)
{
  int r, *fdp = fdp_data;
  do
    r = fcntl (*fdp, F_SETFD, 0 /* FD_CLOEXEC */);
  while (r < 0 && errno == EINTR);
}

276
static gboolean
277 278
launch_test_binary (const char *binary,
                    guint       skip_tests)
279
{
Tim Janik's avatar
Tim Janik committed
280
  GTestLogBuffer *tlb;
281 282
  GSList *slist, *free_list = NULL;
  GError *error = NULL;
283 284
  int argc = 0;
  const gchar **argv;
285
  GPid pid = 0;
Tim Janik's avatar
Tim Janik committed
286
  gint report_pipe[2] = { -1, -1 };
287 288
  guint child_report_cb_id = 0;
  gboolean loop_pending;
Tim Janik's avatar
Tim Janik committed
289 290
  gint i = 0;

291
  if (!g_unix_open_pipe (report_pipe, FD_CLOEXEC, &error))
Tim Janik's avatar
Tim Janik committed
292 293
    {
      if (subtest_mode_fatal)
294
        g_error ("Failed to open pipe for test binary: %s: %s", binary, error->message);
Tim Janik's avatar
Tim Janik committed
295
      else
296 297
        g_warning ("Failed to open pipe for test binary: %s: %s", binary, error->message);
      g_clear_error (&error);
298
      return FALSE;
Tim Janik's avatar
Tim Janik committed
299
    }
300

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
  /* setup argc */
  for (slist = subtest_args; slist; slist = slist->next)
    argc++;
  /* argc++; */
  if (subtest_quiet)
    argc++;
  if (subtest_verbose)
    argc++;
  if (!subtest_mode_fatal)
    argc++;
  if (subtest_mode_quick)
    argc++;
  else
    argc++;
  if (subtest_mode_perf)
    argc++;
317 318
  if (!subtest_mode_undefined)
    argc++;
319 320 321 322 323 324 325 326 327
  if (gtester_list_tests)
    argc++;
  if (subtest_seedstr)
    argc++;
  argc++;
  if (skip_tests)
    argc++;
  for (slist = subtest_paths; slist; slist = slist->next)
    argc++;
328 329
  for (slist = skipped_paths; slist; slist = slist->next)
    argc++;
330

331
  /* setup argv */
332
  argv = g_malloc ((argc + 2) * sizeof(gchar *));
333
  argv[i++] = binary;
334 335
  for (slist = subtest_args; slist; slist = slist->next)
    argv[i++] = (gchar*) slist->data;
336
  /* argv[i++] = "--debug-log"; */
337
  if (subtest_quiet)
Tim Janik's avatar
Tim Janik committed
338
    argv[i++] = "--quiet";
339
  if (subtest_verbose)
Tim Janik's avatar
Tim Janik committed
340
    argv[i++] = "--verbose";
341 342 343 344 345 346 347 348
  if (!subtest_mode_fatal)
    argv[i++] = "--keep-going";
  if (subtest_mode_quick)
    argv[i++] = "-m=quick";
  else
    argv[i++] = "-m=slow";
  if (subtest_mode_perf)
    argv[i++] = "-m=perf";
349 350
  if (!subtest_mode_undefined)
    argv[i++] = "-m=no-undefined";
351 352
  if (gtester_list_tests)
    argv[i++] = "-l";
353 354
  if (subtest_seedstr)
    argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--seed=%s", subtest_seedstr));
355 356 357
  argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestLogFD=%u", report_pipe[1]));
  if (skip_tests)
    argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestSkipCount=%u", skip_tests));
358 359
  for (slist = subtest_paths; slist; slist = slist->next)
    argv[i++] = queue_gfree (&free_list, g_strdup_printf ("-p=%s", (gchar*) slist->data));
360 361
  for (slist = skipped_paths; slist; slist = slist->next)
    argv[i++] = queue_gfree (&free_list, g_strdup_printf ("-s=%s", (gchar*) slist->data));
362 363 364 365 366 367
  argv[i++] = NULL;

  g_spawn_async_with_pipes (NULL, /* g_get_current_dir() */
                            (gchar**) argv,
                            NULL, /* envp */
                            G_SPAWN_DO_NOT_REAP_CHILD, /* G_SPAWN_SEARCH_PATH */
Tim Janik's avatar
Tim Janik committed
368
                            unset_cloexec_fdp, &report_pipe[1], /* pre-exec callback */
369
                            &pid,
370
                            NULL,       /* standard_input */
Tim Janik's avatar
Tim Janik committed
371
                            NULL,       /* standard_output */
372
                            NULL,       /* standard_error */
373
                            &error);
374 375 376
  g_slist_foreach (free_list, (void(*)(void*,void*)) g_free, NULL);
  g_slist_free (free_list);
  free_list = NULL;
Tim Janik's avatar
Tim Janik committed
377
  close (report_pipe[1]);
378

379 380 381
  if (!gtester_quiet)
    g_print ("(pid=%lu)\n", (unsigned long) pid);

382 383
  if (error)
    {
Tim Janik's avatar
Tim Janik committed
384
      close (report_pipe[0]);
385 386 387 388 389
      if (subtest_mode_fatal)
        g_error ("Failed to execute test binary: %s: %s", argv[0], error->message);
      else
        g_warning ("Failed to execute test binary: %s: %s", argv[0], error->message);
      g_clear_error (&error);
390
      g_free (argv);
391
      return FALSE;
392
    }
393
  g_free (argv);
Tim Janik's avatar
Tim Janik committed
394

395 396
  subtest_running = TRUE;
  subtest_io_pending = TRUE;
Tim Janik's avatar
Tim Janik committed
397 398
  tlb = g_test_log_buffer_new();
  if (report_pipe[0] >= 0)
399
    {
Tim Janik's avatar
Tim Janik committed
400
      ioc_report = g_io_channel_unix_new (report_pipe[0]);
401
      g_io_channel_set_flags (ioc_report, G_IO_FLAG_NONBLOCK, NULL);
Tim Janik's avatar
Tim Janik committed
402 403
      g_io_channel_set_encoding (ioc_report, NULL, NULL);
      g_io_channel_set_buffered (ioc_report, FALSE);
404
      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);
405 406 407
      g_io_channel_unref (ioc_report);
    }
  g_child_watch_add_full (G_PRIORITY_DEFAULT + 1, pid, child_watch_cb, NULL, NULL);
408

409 410 411 412 413
  loop_pending = g_main_context_pending (NULL);
  while (subtest_running ||     /* FALSE once child exits */
         subtest_io_pending ||  /* FALSE once ioc_report closes */
         loop_pending)          /* TRUE while idler, etc are running */
    {
414
      /* g_print ("LOOPSTATE: subtest_running=%d subtest_io_pending=%d\n", subtest_running, subtest_io_pending); */
415 416 417 418 419 420 421 422
      /* check for unexpected hangs that are not signalled on report_pipe */
      if (!subtest_running &&   /* child exited */
          subtest_io_pending && /* no EOF detected on report_pipe */
          !loop_pending)        /* no IO events pending however */
        break;
      g_main_context_iteration (NULL, TRUE);
      loop_pending = g_main_context_pending (NULL);
    }
Tim Janik's avatar
Tim Janik committed
423

424
  g_source_remove (child_report_cb_id);
Tim Janik's avatar
Tim Janik committed
425 426
  close (report_pipe[0]);
  g_test_log_buffer_free (tlb);
427 428 429 430 431 432 433

  return TRUE;
}

static void
launch_test (const char *binary)
{
434
  gboolean success = TRUE;
435
  GTimer *btimer = g_timer_new();
436
  gboolean need_restart;
437

438
  testcase_count = 0;
439 440
  if (!gtester_quiet)
    g_print ("TEST: %s... ", binary);
441 442

 retry:
443 444 445
  test_log_printfe ("%s<testbinary path=\"%s\">\n", sindent (log_indent), binary);
  log_indent += 2;
  g_timer_start (btimer);
446 447 448 449 450
  subtest_exitstatus = 0;
  success &= launch_test_binary (binary, testcase_count);
  success &= subtest_exitstatus == 0;
  need_restart = testcase_open != 0;
  if (testcase_open)
451
    testcase_close (0, -256, 0);
452 453 454 455
  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));
456 457
  g_free (subtest_last_seed);
  subtest_last_seed = NULL;
458 459 460 461 462 463
  if (need_restart)
    {
      /* restart test binary, skipping processed test cases */
      goto retry;
    }

464 465 466 467
  /* count the inability to run a test as a failure */
  if (!success && testcase_count == 0)
    testcase_fail_count++;

468
  if (!gtester_quiet)
469
    g_print ("%s: %s\n", !success ? "FAIL" : "PASS", binary);
470
  g_timer_destroy (btimer);
471 472
  if (subtest_mode_fatal && !success)
    terminate();
473
}
474

475 476 477 478 479 480 481 482
static void
usage (gboolean just_version)
{
  if (just_version)
    {
      g_print ("gtester version %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
      return;
    }
483 484
  g_print ("Usage:\n");
  g_print ("gtester [OPTIONS] testprogram...\n\n");
485
  /*        12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  g_print ("Help Options:\n");
  g_print ("  -h, --help                    Show this help message\n\n");
  g_print ("Utility Options:\n");
  g_print ("  -v, --version                 Print version informations\n");
  g_print ("  --g-fatal-warnings            Make warnings fatal (abort)\n");
  g_print ("  -k, --keep-going              Continue running after tests failed\n");
  g_print ("  -l                            List paths of available test cases\n");
  g_print ("  -m {perf|slow|thorough|quick} Run test cases according to mode\n");
  g_print ("  -m {undefined|no-undefined}   Run test cases according to mode\n");
  g_print ("  -p=TESTPATH                   Only start test cases matching TESTPATH\n");
  g_print ("  -s=TESTPATH                   Skip test cases matching TESTPATH\n");
  g_print ("  --seed=SEEDSTRING             Start tests with random seed SEEDSTRING\n");
  g_print ("  -o=LOGFILE                    Write the test log to LOGFILE\n");
  g_print ("  -q, --quiet                   Suppress per test binary output\n");
  g_print ("  --verbose                     Report success per testcase\n");
501
}
502

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
static void
parse_args (gint    *argc_p,
            gchar ***argv_p)
{
  guint argc = *argc_p;
  gchar **argv = *argv_p;
  guint i, e;
  /* parse known args */
  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[i], "--g-fatal-warnings") == 0)
        {
          GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
          fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
          g_log_set_always_fatal (fatal_mask);
          argv[i] = NULL;
        }
520 521 522 523
      else if (strcmp (argv[i], "--gtester-selftest") == 0)
        {
          gtester_selftest = TRUE;
          argv[i] = NULL;
524
          break;        /* stop parsing regular gtester arguments */
525
        }
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
      else if (strcmp (argv[i], "-h") == 0 || strcmp (argv[i], "--help") == 0)
        {
          usage (FALSE);
          exit (0);
          argv[i] = NULL;
        }
      else if (strcmp (argv[i], "-v") == 0 || strcmp (argv[i], "--version") == 0)
        {
          usage (TRUE);
          exit (0);
          argv[i] = NULL;
        }
      else if (strcmp (argv[i], "--keep-going") == 0 ||
               strcmp (argv[i], "-k") == 0)
        {
          subtest_mode_fatal = FALSE;
          argv[i] = NULL;
        }
      else if (strcmp ("-p", argv[i]) == 0 || strncmp ("-p=", argv[i], 3) == 0)
        {
          gchar *equal = argv[i] + 2;
          if (*equal == '=')
            subtest_paths = g_slist_prepend (subtest_paths, equal + 1);
          else if (i + 1 < argc)
            {
              argv[i++] = NULL;
              subtest_paths = g_slist_prepend (subtest_paths, argv[i]);
            }
          argv[i] = NULL;
        }
556 557 558 559 560 561 562 563 564 565 566 567
      else if (strcmp ("-s", argv[i]) == 0 || strncmp ("-s=", argv[i], 3) == 0)
        {
          gchar *equal = argv[i] + 2;
          if (*equal == '=')
            skipped_paths = g_slist_prepend (skipped_paths, equal + 1);
          else if (i + 1 < argc)
            {
              argv[i++] = NULL;
              skipped_paths = g_slist_prepend (skipped_paths, argv[i]);
            }
          argv[i] = NULL;
        }
568 569 570 571 572 573 574 575 576 577 578 579
      else if (strcmp ("--test-arg", argv[i]) == 0 || strncmp ("--test-arg=", argv[i], 11) == 0)
        {
          gchar *equal = argv[i] + 10;
          if (*equal == '=')
            subtest_args = g_slist_prepend (subtest_args, equal + 1);
          else if (i + 1 < argc)
            {
              argv[i++] = NULL;
              subtest_args = g_slist_prepend (subtest_args, argv[i]);
            }
          argv[i] = NULL;
        }
580 581 582 583
      else if (strcmp ("-o", argv[i]) == 0 || strncmp ("-o=", argv[i], 3) == 0)
        {
          gchar *equal = argv[i] + 2;
          if (*equal == '=')
584
            output_filename = equal + 1;
585 586 587
          else if (i + 1 < argc)
            {
              argv[i++] = NULL;
588
              output_filename = argv[i];
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
            }
          argv[i] = NULL;
        }
      else if (strcmp ("-m", argv[i]) == 0 || strncmp ("-m=", argv[i], 3) == 0)
        {
          gchar *equal = argv[i] + 2;
          const gchar *mode = "";
          if (*equal == '=')
            mode = equal + 1;
          else if (i + 1 < argc)
            {
              argv[i++] = NULL;
              mode = argv[i];
            }
          if (strcmp (mode, "perf") == 0)
            subtest_mode_perf = TRUE;
605
          else if (strcmp (mode, "slow") == 0 || strcmp (mode, "thorough") == 0)
606 607 608 609 610 611
            subtest_mode_quick = FALSE;
          else if (strcmp (mode, "quick") == 0)
            {
              subtest_mode_quick = TRUE;
              subtest_mode_perf = FALSE;
            }
612 613 614 615
          else if (strcmp (mode, "undefined") == 0)
            subtest_mode_undefined = TRUE;
          else if (strcmp (mode, "no-undefined") == 0)
            subtest_mode_undefined = FALSE;
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
          else
            g_error ("unknown test mode: -m %s", mode);
          argv[i] = NULL;
        }
      else if (strcmp ("-q", argv[i]) == 0 || strcmp ("--quiet", argv[i]) == 0)
        {
          gtester_quiet = TRUE;
          gtester_verbose = FALSE;
          argv[i] = NULL;
        }
      else if (strcmp ("--verbose", argv[i]) == 0)
        {
          gtester_quiet = FALSE;
          gtester_verbose = TRUE;
          argv[i] = NULL;
        }
      else if (strcmp ("-l", argv[i]) == 0)
        {
          gtester_list_tests = TRUE;
          argv[i] = NULL;
        }
      else if (strcmp ("--seed", argv[i]) == 0 || strncmp ("--seed=", argv[i], 7) == 0)
        {
          gchar *equal = argv[i] + 6;
          if (*equal == '=')
            subtest_seedstr = equal + 1;
          else if (i + 1 < argc)
            {
              argv[i++] = NULL;
              subtest_seedstr = argv[i];
            }
          argv[i] = NULL;
        }
    }
  /* collapse argv */
  e = 1;
  for (i = 1; i < argc; i++)
    if (argv[i])
      {
        argv[e++] = argv[i];
        if (i >= e)
          argv[i] = NULL;
      }
  *argc_p = e;
660 661
}

662 663 664 665 666 667 668 669
int
main (int    argc,
      char **argv)
{
  guint ui;

  g_set_prgname (argv[0]);
  parse_args (&argc, &argv);
670 671
  if (gtester_selftest)
    return main_selftest (argc, argv);
672 673 674 675 676 677 678

  if (argc <= 1)
    {
      usage (FALSE);
      return 1;
    }

679 680 681 682 683 684 685 686 687 688
  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;
689 690 691 692
  for (ui = 1; ui < argc; ui++)
    {
      const char *binary = argv[ui];
      launch_test (binary);
693
      /* we only get here on success or if !subtest_mode_fatal */
694
    }
695 696 697 698
  log_indent -= 2;
  test_log_printfe ("%s</gtester>\n", sindent (log_indent));

  close (log_fd);
699

700
  return testcase_fail_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
701
}
702 703

static void
704 705
fixture_setup (guint        *fix,
               gconstpointer test_data)
706 707 708 709 710
{
  g_assert_cmphex (*fix, ==, 0);
  *fix = 0xdeadbeef;
}
static void
711 712
fixture_test (guint        *fix,
              gconstpointer test_data)
713 714
{
  g_assert_cmphex (*fix, ==, 0xdeadbeef);
715 716 717 718 719
  g_test_message ("This is a test message API test message.");
  g_test_bug_base ("http://www.example.com/bugtracker/");
  g_test_bug ("123");
  g_test_bug_base ("http://www.example.com/bugtracker?bugnum=%s;cmd=showbug");
  g_test_bug ("456");
720 721
}
static void
722 723
fixture_teardown (guint        *fix,
                  gconstpointer test_data)
724 725 726 727 728 729 730 731
{
  g_assert_cmphex (*fix, ==, 0xdeadbeef);
}

static int
main_selftest (int    argc,
               char **argv)
{
732
  /* gtester main() for --gtester-selftest invocations */
733
  g_test_init (&argc, &argv, NULL);
734
  g_test_add ("/gtester/fixture-test", guint, NULL, fixture_setup, fixture_test, fixture_teardown);
735 736
  return g_test_run();
}