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);
35 strbuf_addstr(err, "preloaded output of a child\n");
40 static int no_job(struct child_process *cp,
45 strbuf_addstr(err, "no further jobs available\n");
49 static int task_finished(int result,
54 strbuf_addstr(err, "asking for a quick stop\n");
59 struct string_list tests, failed;
61 int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
63 #define TESTSUITE_INIT \
64 { STRING_LIST_INIT_DUP, STRING_LIST_INIT_DUP, -1, 0, 0, 0, 0, 0, 0 }
66 static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
69 struct testsuite *suite = cb;
71 if (suite->next >= suite->tests.nr)
74 test = suite->tests.items[suite->next++].string;
75 strvec_pushl(&cp->args, "sh", test, NULL);
77 strvec_push(&cp->args, "--quiet");
79 strvec_push(&cp->args, "-i");
81 strvec_push(&cp->args, "-v");
82 if (suite->verbose_log)
83 strvec_push(&cp->args, "-V");
85 strvec_push(&cp->args, "-x");
86 if (suite->write_junit_xml)
87 strvec_push(&cp->args, "--write-junit-xml");
89 strbuf_addf(err, "Output of '%s':\n", test);
90 *task_cb = (void *)test;
95 static int test_finished(int result, struct strbuf *err, void *cb,
98 struct testsuite *suite = cb;
99 const char *name = (const char *)task_cb;
102 string_list_append(&suite->failed, name);
104 strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
109 static int test_failed(struct strbuf *out, void *cb, void *task_cb)
111 struct testsuite *suite = cb;
112 const char *name = (const char *)task_cb;
114 string_list_append(&suite->failed, name);
115 strbuf_addf(out, "FAILED TO START: '%s'\n", name);
120 static const char * const testsuite_usage[] = {
121 "test-run-command testsuite [<options>] [<pattern>...]",
125 static int testsuite(int argc, const char **argv)
127 struct testsuite suite = TESTSUITE_INIT;
128 int max_jobs = 1, i, ret;
131 struct option options[] = {
132 OPT_BOOL('i', "immediate", &suite.immediate,
133 "stop at first failed test case(s)"),
134 OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
135 OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
136 OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
137 OPT_BOOL('V', "verbose-log", &suite.verbose_log,
138 "be verbose, redirected to a file"),
139 OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
140 OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
141 "write JUnit-style XML files"),
145 memset(&suite, 0, sizeof(suite));
146 suite.tests.strdup_strings = suite.failed.strdup_strings = 1;
148 argc = parse_options(argc, argv, NULL, options,
149 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
152 max_jobs = online_cpus();
156 die("Could not open the current directory");
157 while ((d = readdir(dir))) {
158 const char *p = d->d_name;
160 if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
161 !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
162 !ends_with(p, ".sh"))
165 /* No pattern: match all */
167 string_list_append(&suite.tests, p);
171 for (i = 0; i < argc; i++)
172 if (!wildmatch(argv[i], p, 0)) {
173 string_list_append(&suite.tests, p);
180 die("No tests match!");
181 if (max_jobs > suite.tests.nr)
182 max_jobs = suite.tests.nr;
184 fprintf(stderr, "Running %d tests (%d at a time)\n",
185 suite.tests.nr, max_jobs);
187 ret = run_processes_parallel(max_jobs, next_test, test_failed,
188 test_finished, &suite);
190 if (suite.failed.nr > 0) {
192 fprintf(stderr, "%d tests failed:\n\n", suite.failed.nr);
193 for (i = 0; i < suite.failed.nr; i++)
194 fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
197 string_list_clear(&suite.tests, 0);
198 string_list_clear(&suite.failed, 0);
203 static uint64_t my_random_next = 1234;
205 static uint64_t my_random(void)
207 uint64_t res = my_random_next;
208 my_random_next = my_random_next * 1103515245 + 12345;
212 static int quote_stress_test(int argc, const char **argv)
215 * We are running a quote-stress test.
216 * spawn a subprocess that runs quote-stress with a
217 * special option that echoes back the arguments that
220 char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
221 int i, j, k, trials = 100, skip = 0, msys2 = 0;
222 struct strbuf out = STRBUF_INIT;
223 struct strvec args = STRVEC_INIT;
224 struct option options[] = {
225 OPT_INTEGER('n', "trials", &trials, "Number of trials"),
226 OPT_INTEGER('s', "skip", &skip, "Skip <n> trials"),
227 OPT_BOOL('m', "msys2", &msys2, "Test quoting for MSYS2's sh"),
230 const char * const usage[] = {
231 "test-tool run-command quote-stress-test <options>",
235 argc = parse_options(argc, argv, NULL, options, usage, 0);
237 setenv("MSYS_NO_PATHCONV", "1", 0);
239 for (i = 0; i < trials; i++) {
240 struct child_process cp = CHILD_PROCESS_INIT;
241 size_t arg_count, arg_offset;
246 strvec_pushl(&args, "sh", "-c",
247 "printf %s\\\\0 \"$@\"", "skip", NULL);
249 strvec_pushl(&args, "test-tool", "run-command",
251 arg_offset = args.nr;
256 for (j = 0; j < arg_count; j++)
257 strvec_push(&args, argv[j]);
259 arg_count = 1 + (my_random() % 5);
260 for (j = 0; j < arg_count; j++) {
263 size_t arg_len = min_len +
264 (my_random() % (ARRAY_SIZE(buf) - min_len));
266 for (k = 0; k < arg_len; k++)
267 buf[k] = special[my_random() %
268 ARRAY_SIZE(special)];
271 strvec_push(&args, buf);
280 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
281 return error("Failed to spawn child process");
283 for (j = 0, k = 0; j < arg_count; j++) {
284 const char *arg = args.v[j + arg_offset];
286 if (strcmp(arg, out.buf + k))
287 ret = error("incorrectly quoted arg: '%s', "
288 "echoed back as '%s'",
290 k += strlen(out.buf + k) + 1;
294 ret = error("got %d bytes, but consumed only %d",
295 (int)out.len, (int)k);
298 fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
299 for (j = 0; j < arg_count; j++)
300 fprintf(stderr, "arg #%d: '%s'\n",
301 (int)j, args.v[j + arg_offset]);
303 strbuf_release(&out);
309 if (i && (i % 100) == 0)
310 fprintf(stderr, "Trials completed: %d\n", (int)i);
313 strbuf_release(&out);
319 static int quote_echo(int argc, const char **argv)
322 fwrite(argv[1], strlen(argv[1]), 1, stdout);
331 static int inherit_handle(const char *argv0)
333 struct child_process cp = CHILD_PROCESS_INIT;
337 /* First, open an inheritable handle */
338 xsnprintf(path, sizeof(path), "out-XXXXXX");
339 tmp = xmkstemp(path);
341 strvec_pushl(&cp.args,
342 "test-tool", argv0, "inherited-handle-child", NULL);
344 cp.no_stdout = cp.no_stderr = 1;
345 if (start_command(&cp) < 0)
346 die("Could not start child process");
348 /* Then close it, and try to delete it. */
351 die("Could not delete '%s'", path);
353 if (close(cp.in) < 0 || finish_command(&cp) < 0)
354 die("Child did not finish");
359 static int inherit_handle_child(void)
361 struct strbuf buf = STRBUF_INIT;
363 if (strbuf_read(&buf, 0, 0) < 0)
364 die("Could not read stdin");
365 printf("Received %s\n", buf.buf);
366 strbuf_release(&buf);
371 int cmd__run_command(int argc, const char **argv)
373 struct child_process proc = CHILD_PROCESS_INIT;
376 if (argc > 1 && !strcmp(argv[1], "testsuite"))
377 exit(testsuite(argc - 1, argv + 1));
378 if (!strcmp(argv[1], "inherited-handle"))
379 exit(inherit_handle(argv[0]));
380 if (!strcmp(argv[1], "inherited-handle-child"))
381 exit(inherit_handle_child());
383 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
384 return !!quote_stress_test(argc - 1, argv + 1);
386 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
387 return !!quote_echo(argc - 1, argv + 1);
391 while (!strcmp(argv[1], "env")) {
393 die("env specifier without a value");
394 strvec_push(&proc.env_array, argv[2]);
400 proc.argv = (const char **)argv + 2;
402 if (!strcmp(argv[1], "start-command-ENOENT")) {
403 if (start_command(&proc) < 0 && errno == ENOENT)
405 fprintf(stderr, "FAIL %s\n", argv[1]);
408 if (!strcmp(argv[1], "run-command"))
409 exit(run_command(&proc));
411 jobs = atoi(argv[2]);
412 proc.argv = (const char **)argv + 3;
414 if (!strcmp(argv[1], "run-command-parallel"))
415 exit(run_processes_parallel(jobs, parallel_next,
418 if (!strcmp(argv[1], "run-command-abort"))
419 exit(run_processes_parallel(jobs, parallel_next,
420 NULL, task_finished, &proc));
422 if (!strcmp(argv[1], "run-command-no-jobs"))
423 exit(run_processes_parallel(jobs, no_job,
424 NULL, task_finished, &proc));
426 fprintf(stderr, "check usage\n");