gtester.c 12.9 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
#include <glib.h>
21
22
23
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
Tim Janik's avatar
Tim Janik committed
24
25
26
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
27

28
/* the read buffer size in bytes */
29
30
31
32
33
#define READ_BUFFER_SIZE 4096

/* --- prototypes --- */
static void     parse_args      (gint           *argc_p,
                                 gchar        ***argv_p);
34

35
/* --- variables --- */
36
37
38
static GIOChannel  *ioc_report = NULL;
static gboolean     subtest_running = FALSE;
static gboolean     subtest_io_pending = FALSE;
Tim Janik's avatar
Tim Janik committed
39
static gboolean     gtester_quiet = TRUE;
40
41
42
43
44
45
46
47
static gboolean     gtester_verbose = FALSE;
static gboolean     gtester_list_tests = FALSE;
static gboolean     subtest_mode_fatal = FALSE;
static gboolean     subtest_mode_perf = FALSE;
static gboolean     subtest_mode_quick = TRUE;
static const gchar *subtest_seedstr = NULL;
static GSList      *subtest_paths = NULL;
static const gchar *outpu_filename = NULL;
48

49
/* --- functions --- */
50
static gboolean
51
52
53
child_report_cb (GIOChannel  *source,
                 GIOCondition condition,
                 gpointer     data)
54
{
Tim Janik's avatar
Tim Janik committed
55
  GTestLogBuffer *tlb = data;
56
  GIOStatus status = G_IO_STATUS_NORMAL;
Tim Janik's avatar
Tim Janik committed
57
58
  gsize length = 0;
  do
59
    {
Tim Janik's avatar
Tim Janik committed
60
      guint8 buffer[READ_BUFFER_SIZE];
61
      GError *error = NULL;
Tim Janik's avatar
Tim Janik committed
62
63
      status = g_io_channel_read_chars (source, (gchar*) buffer, sizeof (buffer), &length, &error);
      if (length)
64
        {
Tim Janik's avatar
Tim Janik committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
          GTestLogMsg *msg;
          g_test_log_buffer_push (tlb, length, buffer);
          do
            {
              msg = g_test_log_buffer_pop (tlb);
              if (msg)
                {
                  guint ui;
                  /* print message, this should be written to an XML log file */
                  g_printerr ("{*GTLOG(%s)", g_test_log_type_name (msg->log_type));
                  for (ui = 0; ui < msg->n_strings; ui++)
                    g_printerr (":{%s}", msg->strings[ui]);
                  if (msg->n_nums)
                    {
                      g_printerr (":(");
                      for (ui = 0; ui < msg->n_nums; ui++)
                        g_printerr ("%s%.16Lg", ui ? ";" : "", msg->nums[ui]);
                      g_printerr (")");
                    }
                  g_printerr (":GTLOG*}\n");
                  g_test_log_msg_free (msg);
                }
            }
          while (msg);
89
        }
90
      g_clear_error (&error);
Tim Janik's avatar
Tim Janik committed
91
92
      /* ignore the io channel status, which seems to be bogus especially for non blocking fds */
      (void) status;
93
    }
Tim Janik's avatar
Tim Janik committed
94
95
96
97
98
99
100
101
102
103
  while (length > 0);
  if (condition & (G_IO_ERR | G_IO_HUP))
    {
      /* 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 */
104
105
106
107
108
109
110
111
}

static void
child_watch_cb (GPid     pid,
		gint     status,
		gpointer data)
{
  g_spawn_close_pid (pid);
112
113
  subtest_running = FALSE;
}
114

115
116
117
118
119
120
static gchar*
queue_gfree (GSList **slistp,
             gchar   *string)
{
  *slistp = g_slist_prepend (*slistp, string);
  return string;
121
122
}

Tim Janik's avatar
Tim Janik committed
123
124
125
126
127
128
129
130
131
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);
}

132
133
static void
launch_test (const char *binary)
134
{
Tim Janik's avatar
Tim Janik committed
135
  GTestLogBuffer *tlb;
136
137
138
139
  GSList *slist, *free_list = NULL;
  GError *error = NULL;
  const gchar *argv[20 + g_slist_length (subtest_paths)];
  GPid pid = 0;
Tim Janik's avatar
Tim Janik committed
140
141
142
143
144
145
146
147
148
149
150
  gint report_pipe[2] = { -1, -1 };
  gint i = 0;

  if (pipe (report_pipe) < 0)
    {
      if (subtest_mode_fatal)
        g_error ("Failed to open pipe for test binary: %s: %s", binary, g_strerror (errno));
      else
        g_warning ("Failed to open pipe for test binary: %s: %s", binary, g_strerror (errno));
      return;
    }
151
152
153

  /* setup argv */
  argv[i++] = binary;
Tim Janik's avatar
Tim Janik committed
154
155
156
157
158
159
  if (gtester_quiet)
    argv[i++] = "--quiet";
  if (gtester_verbose)
    argv[i++] = "--verbose";
  // argv[i++] = "--debug-log";
  argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--GTestLogFD=%u", report_pipe[1]));
160
161
162
163
164
165
166
167
168
169
170
171
  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";
  if (subtest_seedstr)
    argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--seed=%s", subtest_seedstr));
  for (slist = subtest_paths; slist; slist = slist->next)
    argv[i++] = queue_gfree (&free_list, g_strdup_printf ("-p=%s", (gchar*) slist->data));
172
173
  if (gtester_list_tests)
    argv[i++] = "-l";
174
175
176
177
178
179
  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
180
                            unset_cloexec_fdp, &report_pipe[1], /* pre-exec callback */
181
                            &pid,
182
                            NULL,       /* standard_input */
Tim Janik's avatar
Tim Janik committed
183
                            NULL,       /* standard_output */
184
                            NULL,       /* standard_error */
185
                            &error);
186
187
188
  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
189
  close (report_pipe[1]);
190
191
192

  if (error)
    {
Tim Janik's avatar
Tim Janik committed
193
      close (report_pipe[0]);
194
195
196
197
198
199
      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);
      return;
200
    }
Tim Janik's avatar
Tim Janik committed
201

202
203
  subtest_running = TRUE;
  subtest_io_pending = TRUE;
Tim Janik's avatar
Tim Janik committed
204
205
  tlb = g_test_log_buffer_new();
  if (report_pipe[0] >= 0)
206
    {
Tim Janik's avatar
Tim Janik committed
207
      ioc_report = g_io_channel_unix_new (report_pipe[0]);
208
      g_io_channel_set_flags (ioc_report, G_IO_FLAG_NONBLOCK, NULL);
Tim Janik's avatar
Tim Janik committed
209
210
211
      g_io_channel_set_encoding (ioc_report, NULL, NULL);
      g_io_channel_set_buffered (ioc_report, FALSE);
      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);
212
213
214
      g_io_channel_unref (ioc_report);
    }
  g_child_watch_add_full (G_PRIORITY_DEFAULT + 1, pid, child_watch_cb, NULL, NULL);
215

216
217
218
219
  while (subtest_running ||             /* FALSE once child exits */
         subtest_io_pending ||          /* FALSE once ioc_report closes */
         g_main_context_pending (NULL)) /* TRUE while idler, etc are running */
    g_main_context_iteration (NULL, TRUE);
Tim Janik's avatar
Tim Janik committed
220
221
222

  close (report_pipe[0]);
  g_test_log_buffer_free (tlb);
223
}
224

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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;
    }
  g_print ("Usage: gtester [OPTIONS] testprogram...\n");
  /*        12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
  g_print ("Options:\n");
  g_print ("  -h, --help                  show this help message\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, -m=slow, -m=quick  run test cases in mode perf, slow or quick (default)\n");
  g_print ("  -p=TESTPATH                 only start test cases matching TESTPATH\n");
  g_print ("  --seed=SEEDSTRING           start all tests with random number seed SEEDSTRING\n");
  g_print ("  -o=LOGFILE                  write the test log to LOGFILE\n");
  g_print ("  -q, --quiet                 suppress unnecessary output\n");
  g_print ("  --verbose                   produce additional output\n");
}
248

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
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;
        }
      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;
        }
      else if (strcmp ("-o", argv[i]) == 0 || strncmp ("-o=", argv[i], 3) == 0)
        {
          gchar *equal = argv[i] + 2;
          if (*equal == '=')
            outpu_filename = equal + 1;
          else if (i + 1 < argc)
            {
              argv[i++] = NULL;
              outpu_filename = argv[i];
            }
          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;
          else if (strcmp (mode, "slow") == 0)
            subtest_mode_quick = FALSE;
          else if (strcmp (mode, "quick") == 0)
            {
              subtest_mode_quick = TRUE;
              subtest_mode_perf = FALSE;
            }
          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;
372
373
}

374
375
376
377
378
379
int
main (int    argc,
      char **argv)
{
  guint ui;

Tim Janik's avatar
Tim Janik committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  /* 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 * 0)
    {
      struct sigaction sa;
      struct sigaction osa;
      sa.sa_handler = SIG_DFL;
      sigfillset (&sa.sa_mask);
      sa.sa_flags = SA_RESTART;
      sigaction (SIGCHLD, &sa, &osa);
    }

394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  g_set_prgname (argv[0]);
  parse_args (&argc, &argv);

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

  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 */
  return 0;
}