2 * test-run-command.c: test run command API.
4 * (C) 2009 Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
6 * This code is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include "test-tool.h"
12 #include "git-compat-util.h"
14 #include "run-command.h"
17 #include "parse-options.h"
18 #include "string-list.h"
19 #include "thread-utils.h"
20 #include "wildmatch.h"
22 #include "parse-options.h"
24 static int number_callbacks;
25 static int parallel_next(struct child_process *cp,
30 struct child_process *d = cb;
31 if (number_callbacks >= 4)
34 strvec_pushv(&cp->args, d->argv);
36 cp->no_stdin = d->no_stdin;
37 strbuf_addstr(err, "preloaded output of a child\n");
40 *task_cb = xmalloc(sizeof(int));
41 *(int*)(*task_cb) = 2;
45 static int no_job(struct child_process *cp,
50 strbuf_addstr(err, "no further jobs available\n");
54 static int task_finished(int result,
59 strbuf_addstr(err, "asking for a quick stop\n");
63 static int test_stdin(struct strbuf *pipe, void *cb, void *task_cb)
65 int *lines_remaining = task_cb;
68 strbuf_addf(pipe, "sample stdin %d\n", --(*lines_remaining));
70 return !(*lines_remaining);
75 struct string_list tests, failed;
77 int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
79 #define TESTSUITE_INIT \
80 { STRING_LIST_INIT_DUP, STRING_LIST_INIT_DUP, -1, 0, 0, 0, 0, 0, 0 }
82 static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
85 struct testsuite *suite = cb;
87 if (suite->next >= suite->tests.nr)
90 test = suite->tests.items[suite->next++].string;
91 strvec_pushl(&cp->args, "sh", test, NULL);
93 strvec_push(&cp->args, "--quiet");
95 strvec_push(&cp->args, "-i");
97 strvec_push(&cp->args, "-v");
98 if (suite->verbose_log)
99 strvec_push(&cp->args, "-V");
101 strvec_push(&cp->args, "-x");
102 if (suite->write_junit_xml)
103 strvec_push(&cp->args, "--write-junit-xml");
105 strbuf_addf(err, "Output of '%s':\n", test);
106 *task_cb = (void *)test;
111 static int test_finished(int result, struct strbuf *err, void *cb,
114 struct testsuite *suite = cb;
115 const char *name = (const char *)task_cb;
118 string_list_append(&suite->failed, name);
120 strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
125 static int test_failed(struct strbuf *out, void *cb, void *task_cb)
127 struct testsuite *suite = cb;
128 const char *name = (const char *)task_cb;
130 string_list_append(&suite->failed, name);
131 strbuf_addf(out, "FAILED TO START: '%s'\n", name);
136 static const char * const testsuite_usage[] = {
137 "test-run-command testsuite [<options>] [<pattern>...]",
141 static int testsuite(int argc, const char **argv)
143 struct testsuite suite = TESTSUITE_INIT;
144 int max_jobs = 1, i, ret;
147 struct option options[] = {
148 OPT_BOOL('i', "immediate", &suite.immediate,
149 "stop at first failed test case(s)"),
150 OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
151 OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
152 OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
153 OPT_BOOL('V', "verbose-log", &suite.verbose_log,
154 "be verbose, redirected to a file"),
155 OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
156 OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
157 "write JUnit-style XML files"),
161 memset(&suite, 0, sizeof(suite));
162 suite.tests.strdup_strings = suite.failed.strdup_strings = 1;
164 argc = parse_options(argc, argv, NULL, options,
165 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
168 max_jobs = online_cpus();
172 die("Could not open the current directory");
173 while ((d = readdir(dir))) {
174 const char *p = d->d_name;
176 if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
177 !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
178 !ends_with(p, ".sh"))
181 /* No pattern: match all */
183 string_list_append(&suite.tests, p);
187 for (i = 0; i < argc; i++)
188 if (!wildmatch(argv[i], p, 0)) {
189 string_list_append(&suite.tests, p);
196 die("No tests match!");
197 if (max_jobs > suite.tests.nr)
198 max_jobs = suite.tests.nr;
200 fprintf(stderr, "Running %d tests (%d at a time)\n",
201 suite.tests.nr, max_jobs);
203 ret = run_processes_parallel(max_jobs, next_test, test_failed,
204 test_stdin, test_finished, &suite);
206 if (suite.failed.nr > 0) {
208 fprintf(stderr, "%d tests failed:\n\n", suite.failed.nr);
209 for (i = 0; i < suite.failed.nr; i++)
210 fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
213 string_list_clear(&suite.tests, 0);
214 string_list_clear(&suite.failed, 0);
219 static uint64_t my_random_next = 1234;
221 static uint64_t my_random(void)
223 uint64_t res = my_random_next;
224 my_random_next = my_random_next * 1103515245 + 12345;
228 static int quote_stress_test(int argc, const char **argv)
231 * We are running a quote-stress test.
232 * spawn a subprocess that runs quote-stress with a
233 * special option that echoes back the arguments that
236 char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
237 int i, j, k, trials = 100, skip = 0, msys2 = 0;
238 struct strbuf out = STRBUF_INIT;
239 struct strvec args = STRVEC_INIT;
240 struct option options[] = {
241 OPT_INTEGER('n', "trials", &trials, "Number of trials"),
242 OPT_INTEGER('s', "skip", &skip, "Skip <n> trials"),
243 OPT_BOOL('m', "msys2", &msys2, "Test quoting for MSYS2's sh"),
246 const char * const usage[] = {
247 "test-tool run-command quote-stress-test <options>",
251 argc = parse_options(argc, argv, NULL, options, usage, 0);
253 setenv("MSYS_NO_PATHCONV", "1", 0);
255 for (i = 0; i < trials; i++) {
256 struct child_process cp = CHILD_PROCESS_INIT;
257 size_t arg_count, arg_offset;
262 strvec_pushl(&args, "sh", "-c",
263 "printf %s\\\\0 \"$@\"", "skip", NULL);
265 strvec_pushl(&args, "test-tool", "run-command",
267 arg_offset = args.nr;
272 for (j = 0; j < arg_count; j++)
273 strvec_push(&args, argv[j]);
275 arg_count = 1 + (my_random() % 5);
276 for (j = 0; j < arg_count; j++) {
279 size_t arg_len = min_len +
280 (my_random() % (ARRAY_SIZE(buf) - min_len));
282 for (k = 0; k < arg_len; k++)
283 buf[k] = special[my_random() %
284 ARRAY_SIZE(special)];
287 strvec_push(&args, buf);
296 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
297 return error("Failed to spawn child process");
299 for (j = 0, k = 0; j < arg_count; j++) {
300 const char *arg = args.v[j + arg_offset];
302 if (strcmp(arg, out.buf + k))
303 ret = error("incorrectly quoted arg: '%s', "
304 "echoed back as '%s'",
306 k += strlen(out.buf + k) + 1;
310 ret = error("got %d bytes, but consumed only %d",
311 (int)out.len, (int)k);
314 fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
315 for (j = 0; j < arg_count; j++)
316 fprintf(stderr, "arg #%d: '%s'\n",
317 (int)j, args.v[j + arg_offset]);
319 strbuf_release(&out);
325 if (i && (i % 100) == 0)
326 fprintf(stderr, "Trials completed: %d\n", (int)i);
329 strbuf_release(&out);
335 static int quote_echo(int argc, const char **argv)
338 fwrite(argv[1], strlen(argv[1]), 1, stdout);
347 static int inherit_handle(const char *argv0)
349 struct child_process cp = CHILD_PROCESS_INIT;
353 /* First, open an inheritable handle */
354 xsnprintf(path, sizeof(path), "out-XXXXXX");
355 tmp = xmkstemp(path);
357 strvec_pushl(&cp.args,
358 "test-tool", argv0, "inherited-handle-child", NULL);
360 cp.no_stdout = cp.no_stderr = 1;
361 if (start_command(&cp) < 0)
362 die("Could not start child process");
364 /* Then close it, and try to delete it. */
367 die("Could not delete '%s'", path);
369 if (close(cp.in) < 0 || finish_command(&cp) < 0)
370 die("Child did not finish");
375 static int inherit_handle_child(void)
377 struct strbuf buf = STRBUF_INIT;
379 if (strbuf_read(&buf, 0, 0) < 0)
380 die("Could not read stdin");
381 printf("Received %s\n", buf.buf);
382 strbuf_release(&buf);
387 int cmd__run_command(int argc, const char **argv)
389 struct child_process proc = CHILD_PROCESS_INIT;
392 if (argc > 1 && !strcmp(argv[1], "testsuite"))
393 exit(testsuite(argc - 1, argv + 1));
394 if (!strcmp(argv[1], "inherited-handle"))
395 exit(inherit_handle(argv[0]));
396 if (!strcmp(argv[1], "inherited-handle-child"))
397 exit(inherit_handle_child());
399 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
400 return !!quote_stress_test(argc - 1, argv + 1);
402 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
403 return !!quote_echo(argc - 1, argv + 1);
407 while (!strcmp(argv[1], "env")) {
409 die("env specifier without a value");
410 strvec_push(&proc.env_array, argv[2]);
416 proc.argv = (const char **)argv + 2;
418 if (!strcmp(argv[1], "start-command-ENOENT")) {
419 if (start_command(&proc) < 0 && errno == ENOENT)
421 fprintf(stderr, "FAIL %s\n", argv[1]);
424 if (!strcmp(argv[1], "run-command"))
425 exit(run_command(&proc));
427 jobs = atoi(argv[2]);
428 proc.argv = (const char **)argv + 3;
430 if (!strcmp(argv[1], "run-command-parallel"))
431 exit(run_processes_parallel(jobs, parallel_next,
432 NULL, NULL, NULL, &proc));
434 if (!strcmp(argv[1], "run-command-abort"))
435 exit(run_processes_parallel(jobs, parallel_next,
436 NULL, NULL, task_finished, &proc));
438 if (!strcmp(argv[1], "run-command-no-jobs"))
439 exit(run_processes_parallel(jobs, no_job,
440 NULL, NULL, task_finished, &proc));
442 if (!strcmp(argv[1], "run-command-stdin")) {
445 exit (run_processes_parallel(jobs, parallel_next, NULL,
446 test_stdin, NULL, &proc));
449 fprintf(stderr, "check usage\n");