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.
25 /* the read buffer size in bytes */
26 #define READ_BUFFER_SIZE 4096
28 /* --- prototypes --- */
29 static void parse_args (gint *argc_p,
32 /* --- variables --- */
33 static GIOChannel *ioc_report = NULL;
34 static gboolean subtest_running = FALSE;
35 static gboolean subtest_io_pending = FALSE;
36 static gboolean gtester_quiet = FALSE;
37 static gboolean gtester_verbose = FALSE;
38 static gboolean gtester_list_tests = FALSE;
39 static gboolean subtest_mode_fatal = FALSE;
40 static gboolean subtest_mode_perf = FALSE;
41 static gboolean subtest_mode_quick = TRUE;
42 static const gchar *subtest_seedstr = NULL;
43 static GSList *subtest_paths = NULL;
44 static const gchar *outpu_filename = NULL;
46 /* --- functions --- */
48 child_report_cb (GIOChannel *source,
49 GIOCondition condition,
52 GIOStatus status = G_IO_STATUS_NORMAL;
54 while (status == G_IO_STATUS_NORMAL)
56 gchar buffer[READ_BUFFER_SIZE];
59 status = g_io_channel_read_chars (source, buffer, sizeof (buffer), &length, &error);
62 case G_IO_STATUS_NORMAL:
63 write (2, buffer, length); /* passthrough child's stdout */
65 case G_IO_STATUS_AGAIN:
68 case G_IO_STATUS_ERROR:
69 /* ignore, child closed fd or similar g_warning ("Error while reading data: %s", error->message); */
70 /* fall through into EOF */
72 subtest_io_pending = FALSE;
75 g_clear_error (&error);
81 child_watch_cb (GPid pid,
85 g_spawn_close_pid (pid);
86 subtest_running = FALSE;
90 queue_gfree (GSList **slistp,
93 *slistp = g_slist_prepend (*slistp, string);
98 launch_test (const char *binary)
100 GSList *slist, *free_list = NULL;
101 GError *error = NULL;
102 const gchar *argv[20 + g_slist_length (subtest_paths)];
104 gint i = 0, child_report = -1;
108 // argv[i++] = "--quiet";
109 if (!subtest_mode_fatal)
110 argv[i++] = "--keep-going";
111 if (subtest_mode_quick)
112 argv[i++] = "-m=quick";
114 argv[i++] = "-m=slow";
115 if (subtest_mode_perf)
116 argv[i++] = "-m=perf";
118 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("--seed=%s", subtest_seedstr));
119 for (slist = subtest_paths; slist; slist = slist->next)
120 argv[i++] = queue_gfree (&free_list, g_strdup_printf ("-p=%s", (gchar*) slist->data));
123 /* child_report will be used to capture logging information from the
124 * child binary. for the moment, we just use it to replicate stdout.
127 g_spawn_async_with_pipes (NULL, /* g_get_current_dir() */
130 G_SPAWN_DO_NOT_REAP_CHILD, /* G_SPAWN_SEARCH_PATH */
131 NULL, NULL, /* child_setup, user_data */
133 NULL, /* standard_input */
134 &child_report, /* standard_output */
135 NULL, /* standard_error */
137 g_slist_foreach (free_list, (void(*)(void*,void*)) g_free, NULL);
138 g_slist_free (free_list);
143 if (subtest_mode_fatal)
144 g_error ("Failed to execute test binary: %s: %s", argv[0], error->message);
146 g_warning ("Failed to execute test binary: %s: %s", argv[0], error->message);
147 g_clear_error (&error);
150 subtest_running = TRUE;
151 subtest_io_pending = TRUE;
153 if (child_report >= 0)
155 ioc_report = g_io_channel_unix_new (child_report);
156 g_io_channel_set_flags (ioc_report, G_IO_FLAG_NONBLOCK, NULL);
157 g_io_add_watch_full (ioc_report, G_PRIORITY_DEFAULT - 1, G_IO_IN | G_IO_ERR | G_IO_HUP, child_report_cb, NULL, NULL);
158 g_io_channel_unref (ioc_report);
160 g_child_watch_add_full (G_PRIORITY_DEFAULT + 1, pid, child_watch_cb, NULL, NULL);
162 while (subtest_running || /* FALSE once child exits */
163 subtest_io_pending || /* FALSE once ioc_report closes */
164 g_main_context_pending (NULL)) /* TRUE while idler, etc are running */
165 g_main_context_iteration (NULL, TRUE);
169 usage (gboolean just_version)
173 g_print ("gtester version %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
176 g_print ("Usage: gtester [OPTIONS] testprogram...\n");
177 /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
178 g_print ("Options:\n");
179 g_print (" -h, --help show this help message\n");
180 g_print (" -v, --version print version informations\n");
181 g_print (" --g-fatal-warnings make warnings fatal (abort)\n");
182 g_print (" -k, --keep-going continue running after tests failed\n");
183 g_print (" -l list paths of available test cases\n");
184 g_print (" -m=perf, -m=slow, -m=quick run test cases in mode perf, slow or quick (default)\n");
185 g_print (" -p=TESTPATH only start test cases matching TESTPATH\n");
186 g_print (" --seed=SEEDSTRING start all tests with random number seed SEEDSTRING\n");
187 g_print (" -o=LOGFILE write the test log to LOGFILE\n");
188 g_print (" -q, --quiet suppress unnecessary output\n");
189 g_print (" --verbose produce additional output\n");
193 parse_args (gint *argc_p,
196 guint argc = *argc_p;
197 gchar **argv = *argv_p;
199 /* parse known args */
200 for (i = 1; i < argc; i++)
202 if (strcmp (argv[i], "--g-fatal-warnings") == 0)
204 GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
205 fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
206 g_log_set_always_fatal (fatal_mask);
209 else if (strcmp (argv[i], "-h") == 0 || strcmp (argv[i], "--help") == 0)
215 else if (strcmp (argv[i], "-v") == 0 || strcmp (argv[i], "--version") == 0)
221 else if (strcmp (argv[i], "--keep-going") == 0 ||
222 strcmp (argv[i], "-k") == 0)
224 subtest_mode_fatal = FALSE;
227 else if (strcmp ("-p", argv[i]) == 0 || strncmp ("-p=", argv[i], 3) == 0)
229 gchar *equal = argv[i] + 2;
231 subtest_paths = g_slist_prepend (subtest_paths, equal + 1);
232 else if (i + 1 < argc)
235 subtest_paths = g_slist_prepend (subtest_paths, argv[i]);
239 else if (strcmp ("-o", argv[i]) == 0 || strncmp ("-o=", argv[i], 3) == 0)
241 gchar *equal = argv[i] + 2;
243 outpu_filename = equal + 1;
244 else if (i + 1 < argc)
247 outpu_filename = argv[i];
251 else if (strcmp ("-m", argv[i]) == 0 || strncmp ("-m=", argv[i], 3) == 0)
253 gchar *equal = argv[i] + 2;
254 const gchar *mode = "";
257 else if (i + 1 < argc)
262 if (strcmp (mode, "perf") == 0)
263 subtest_mode_perf = TRUE;
264 else if (strcmp (mode, "slow") == 0)
265 subtest_mode_quick = FALSE;
266 else if (strcmp (mode, "quick") == 0)
268 subtest_mode_quick = TRUE;
269 subtest_mode_perf = FALSE;
272 g_error ("unknown test mode: -m %s", mode);
275 else if (strcmp ("-q", argv[i]) == 0 || strcmp ("--quiet", argv[i]) == 0)
277 gtester_quiet = TRUE;
278 gtester_verbose = FALSE;
281 else if (strcmp ("--verbose", argv[i]) == 0)
283 gtester_quiet = FALSE;
284 gtester_verbose = TRUE;
287 else if (strcmp ("-l", argv[i]) == 0)
289 gtester_list_tests = TRUE;
292 else if (strcmp ("--seed", argv[i]) == 0 || strncmp ("--seed=", argv[i], 7) == 0)
294 gchar *equal = argv[i] + 6;
296 subtest_seedstr = equal + 1;
297 else if (i + 1 < argc)
300 subtest_seedstr = argv[i];
307 for (i = 1; i < argc; i++)
323 g_set_prgname (argv[0]);
324 parse_args (&argc, &argv);
332 for (ui = 1; ui < argc; ui++)
334 const char *binary = argv[ui];
335 launch_test (binary);
338 /* we only get here on success or if !subtest_mode_fatal */