The twelfth batch
[git.git] / t / helper / test-run-command.c
blob61eb1175fe53f482b92bd4cda75ea34ce3d62cba
1 /*
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.
9 */
11 #include "test-tool.h"
12 #include "run-command.h"
13 #include "strvec.h"
14 #include "strbuf.h"
15 #include "parse-options.h"
16 #include "string-list.h"
17 #include "thread-utils.h"
18 #include "wildmatch.h"
20 static int number_callbacks;
21 static int parallel_next(struct child_process *cp,
22 struct strbuf *err,
23 void *cb,
24 void **task_cb UNUSED)
26 struct child_process *d = cb;
27 if (number_callbacks >= 4)
28 return 0;
30 strvec_pushv(&cp->args, d->args.v);
31 if (err)
32 strbuf_addstr(err, "preloaded output of a child\n");
33 else
34 fprintf(stderr, "preloaded output of a child\n");
36 number_callbacks++;
37 return 1;
40 static int no_job(struct child_process *cp UNUSED,
41 struct strbuf *err,
42 void *cb UNUSED,
43 void **task_cb UNUSED)
45 if (err)
46 strbuf_addstr(err, "no further jobs available\n");
47 else
48 fprintf(stderr, "no further jobs available\n");
49 return 0;
52 static int task_finished(int result UNUSED,
53 struct strbuf *err,
54 void *pp_cb UNUSED,
55 void *pp_task_cb UNUSED)
57 if (err)
58 strbuf_addstr(err, "asking for a quick stop\n");
59 else
60 fprintf(stderr, "asking for a quick stop\n");
61 return 1;
64 struct testsuite {
65 struct string_list tests, failed;
66 int next;
67 int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
68 const char *shell_path;
70 #define TESTSUITE_INIT { \
71 .tests = STRING_LIST_INIT_DUP, \
72 .failed = STRING_LIST_INIT_DUP, \
75 static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
76 void **task_cb)
78 struct testsuite *suite = cb;
79 const char *test;
80 if (suite->next >= suite->tests.nr)
81 return 0;
83 test = suite->tests.items[suite->next++].string;
84 if (suite->shell_path)
85 strvec_push(&cp->args, suite->shell_path);
86 strvec_push(&cp->args, test);
87 if (suite->quiet)
88 strvec_push(&cp->args, "--quiet");
89 if (suite->immediate)
90 strvec_push(&cp->args, "-i");
91 if (suite->verbose)
92 strvec_push(&cp->args, "-v");
93 if (suite->verbose_log)
94 strvec_push(&cp->args, "-V");
95 if (suite->trace)
96 strvec_push(&cp->args, "-x");
97 if (suite->write_junit_xml)
98 strvec_push(&cp->args, "--write-junit-xml");
100 strbuf_addf(err, "Output of '%s':\n", test);
101 *task_cb = (void *)test;
103 return 1;
106 static int test_finished(int result, struct strbuf *err, void *cb,
107 void *task_cb)
109 struct testsuite *suite = cb;
110 const char *name = (const char *)task_cb;
112 if (result)
113 string_list_append(&suite->failed, name);
115 strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
117 return 0;
120 static int test_failed(struct strbuf *out, void *cb, void *task_cb)
122 struct testsuite *suite = cb;
123 const char *name = (const char *)task_cb;
125 string_list_append(&suite->failed, name);
126 strbuf_addf(out, "FAILED TO START: '%s'\n", name);
128 return 0;
131 static const char * const testsuite_usage[] = {
132 "test-run-command testsuite [<options>] [<pattern>...]",
133 NULL
136 static int testsuite(int argc, const char **argv)
138 struct testsuite suite = TESTSUITE_INIT;
139 int max_jobs = 1, i, ret = 0;
140 DIR *dir;
141 struct dirent *d;
142 struct option options[] = {
143 OPT_BOOL('i', "immediate", &suite.immediate,
144 "stop at first failed test case(s)"),
145 OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
146 OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
147 OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
148 OPT_BOOL('V', "verbose-log", &suite.verbose_log,
149 "be verbose, redirected to a file"),
150 OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
151 OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
152 "write JUnit-style XML files"),
153 OPT_END()
155 struct run_process_parallel_opts opts = {
156 .get_next_task = next_test,
157 .start_failure = test_failed,
158 .task_finished = test_finished,
159 .data = &suite,
161 struct strbuf progpath = STRBUF_INIT;
162 size_t path_prefix_len;
164 argc = parse_options(argc, argv, NULL, options,
165 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
167 if (max_jobs <= 0)
168 max_jobs = online_cpus();
171 * If we run without a shell, execute the programs directly from CWD.
173 suite.shell_path = getenv("TEST_SHELL_PATH");
174 if (!suite.shell_path)
175 strbuf_addstr(&progpath, "./");
176 path_prefix_len = progpath.len;
178 dir = opendir(".");
179 if (!dir)
180 die("Could not open the current directory");
181 while ((d = readdir(dir))) {
182 const char *p = d->d_name;
184 if (!strcmp(p, ".") || !strcmp(p, ".."))
185 continue;
187 /* No pattern: match all */
188 if (!argc) {
189 strbuf_setlen(&progpath, path_prefix_len);
190 strbuf_addstr(&progpath, p);
191 string_list_append(&suite.tests, progpath.buf);
192 continue;
195 for (i = 0; i < argc; i++)
196 if (!wildmatch(argv[i], p, 0)) {
197 strbuf_setlen(&progpath, path_prefix_len);
198 strbuf_addstr(&progpath, p);
199 string_list_append(&suite.tests, progpath.buf);
200 break;
203 closedir(dir);
205 if (!suite.tests.nr)
206 die("No tests match!");
207 if (max_jobs > suite.tests.nr)
208 max_jobs = suite.tests.nr;
210 fprintf(stderr, "Running %"PRIuMAX" tests (%d at a time)\n",
211 (uintmax_t)suite.tests.nr, max_jobs);
213 opts.processes = max_jobs;
214 run_processes_parallel(&opts);
216 if (suite.failed.nr > 0) {
217 ret = 1;
218 fprintf(stderr, "%"PRIuMAX" tests failed:\n\n",
219 (uintmax_t)suite.failed.nr);
220 for (i = 0; i < suite.failed.nr; i++)
221 fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
224 string_list_clear(&suite.tests, 0);
225 string_list_clear(&suite.failed, 0);
226 strbuf_release(&progpath);
228 return ret;
231 static uint64_t my_random_next = 1234;
233 static uint64_t my_random(void)
235 uint64_t res = my_random_next;
236 my_random_next = my_random_next * 1103515245 + 12345;
237 return res;
240 static int quote_stress_test(int argc, const char **argv)
243 * We are running a quote-stress test.
244 * spawn a subprocess that runs quote-stress with a
245 * special option that echoes back the arguments that
246 * were passed in.
248 char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
249 int i, j, k, trials = 100, skip = 0, msys2 = 0;
250 struct strbuf out = STRBUF_INIT;
251 struct strvec args = STRVEC_INIT;
252 struct option options[] = {
253 OPT_INTEGER('n', "trials", &trials, "number of trials"),
254 OPT_INTEGER('s', "skip", &skip, "skip <n> trials"),
255 OPT_BOOL('m', "msys2", &msys2, "test quoting for MSYS2's sh"),
256 OPT_END()
258 const char * const usage[] = {
259 "test-tool run-command quote-stress-test <options>",
260 NULL
263 argc = parse_options(argc, argv, NULL, options, usage, 0);
265 setenv("MSYS_NO_PATHCONV", "1", 0);
267 for (i = 0; i < trials; i++) {
268 struct child_process cp = CHILD_PROCESS_INIT;
269 size_t arg_count, arg_offset;
270 int ret = 0;
272 strvec_clear(&args);
273 if (msys2)
274 strvec_pushl(&args, "sh", "-c",
275 "printf %s\\\\0 \"$@\"", "skip", NULL);
276 else
277 strvec_pushl(&args, "test-tool", "run-command",
278 "quote-echo", NULL);
279 arg_offset = args.nr;
281 if (argc > 0) {
282 trials = 1;
283 arg_count = argc;
284 for (j = 0; j < arg_count; j++)
285 strvec_push(&args, argv[j]);
286 } else {
287 arg_count = 1 + (my_random() % 5);
288 for (j = 0; j < arg_count; j++) {
289 char buf[20];
290 size_t min_len = 1;
291 size_t arg_len = min_len +
292 (my_random() % (ARRAY_SIZE(buf) - min_len));
294 for (k = 0; k < arg_len; k++)
295 buf[k] = special[my_random() %
296 ARRAY_SIZE(special)];
297 buf[arg_len] = '\0';
299 strvec_push(&args, buf);
303 if (i < skip)
304 continue;
306 strvec_pushv(&cp.args, args.v);
307 strbuf_reset(&out);
308 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
309 return error("Failed to spawn child process");
311 for (j = 0, k = 0; j < arg_count; j++) {
312 const char *arg = args.v[j + arg_offset];
314 if (strcmp(arg, out.buf + k))
315 ret = error("incorrectly quoted arg: '%s', "
316 "echoed back as '%s'",
317 arg, out.buf + k);
318 k += strlen(out.buf + k) + 1;
321 if (k != out.len)
322 ret = error("got %d bytes, but consumed only %d",
323 (int)out.len, (int)k);
325 if (ret) {
326 fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
327 for (j = 0; j < arg_count; j++)
328 fprintf(stderr, "arg #%d: '%s'\n",
329 (int)j, args.v[j + arg_offset]);
331 strbuf_release(&out);
332 strvec_clear(&args);
334 return ret;
337 if (i && (i % 100) == 0)
338 fprintf(stderr, "Trials completed: %d\n", (int)i);
341 strbuf_release(&out);
342 strvec_clear(&args);
344 return 0;
347 static int quote_echo(int argc, const char **argv)
349 while (argc > 1) {
350 fwrite(argv[1], strlen(argv[1]), 1, stdout);
351 fputc('\0', stdout);
352 argv++;
353 argc--;
356 return 0;
359 static int inherit_handle(const char *argv0)
361 struct child_process cp = CHILD_PROCESS_INIT;
362 char path[PATH_MAX];
363 int tmp;
365 /* First, open an inheritable handle */
366 xsnprintf(path, sizeof(path), "out-XXXXXX");
367 tmp = xmkstemp(path);
369 strvec_pushl(&cp.args,
370 "test-tool", argv0, "inherited-handle-child", NULL);
371 cp.in = -1;
372 cp.no_stdout = cp.no_stderr = 1;
373 if (start_command(&cp) < 0)
374 die("Could not start child process");
376 /* Then close it, and try to delete it. */
377 close(tmp);
378 if (unlink(path))
379 die("Could not delete '%s'", path);
381 if (close(cp.in) < 0 || finish_command(&cp) < 0)
382 die("Child did not finish");
384 return 0;
387 static int inherit_handle_child(void)
389 struct strbuf buf = STRBUF_INIT;
391 if (strbuf_read(&buf, 0, 0) < 0)
392 die("Could not read stdin");
393 printf("Received %s\n", buf.buf);
394 strbuf_release(&buf);
396 return 0;
399 int cmd__run_command(int argc, const char **argv)
401 struct child_process proc = CHILD_PROCESS_INIT;
402 int jobs;
403 int ret;
404 struct run_process_parallel_opts opts = {
405 .data = &proc,
408 if (argc > 1 && !strcmp(argv[1], "testsuite"))
409 return testsuite(argc - 1, argv + 1);
410 if (!strcmp(argv[1], "inherited-handle"))
411 return inherit_handle(argv[0]);
412 if (!strcmp(argv[1], "inherited-handle-child"))
413 return inherit_handle_child();
415 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
416 return !!quote_stress_test(argc - 1, argv + 1);
418 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
419 return !!quote_echo(argc - 1, argv + 1);
421 if (argc < 3)
422 return 1;
423 while (!strcmp(argv[1], "env")) {
424 if (!argv[2])
425 die("env specifier without a value");
426 strvec_push(&proc.env, argv[2]);
427 argv += 2;
428 argc -= 2;
430 if (argc < 3) {
431 ret = 1;
432 goto cleanup;
434 strvec_pushv(&proc.args, (const char **)argv + 2);
436 if (!strcmp(argv[1], "start-command-ENOENT")) {
437 if (start_command(&proc) < 0 && errno == ENOENT) {
438 ret = 0;
439 goto cleanup;
441 fprintf(stderr, "FAIL %s\n", argv[1]);
442 return 1;
444 if (!strcmp(argv[1], "run-command")) {
445 ret = run_command(&proc);
446 goto cleanup;
449 if (!strcmp(argv[1], "--ungroup")) {
450 argv += 1;
451 argc -= 1;
452 opts.ungroup = 1;
455 jobs = atoi(argv[2]);
456 strvec_clear(&proc.args);
457 strvec_pushv(&proc.args, (const char **)argv + 3);
459 if (!strcmp(argv[1], "run-command-parallel")) {
460 opts.get_next_task = parallel_next;
461 } else if (!strcmp(argv[1], "run-command-abort")) {
462 opts.get_next_task = parallel_next;
463 opts.task_finished = task_finished;
464 } else if (!strcmp(argv[1], "run-command-no-jobs")) {
465 opts.get_next_task = no_job;
466 opts.task_finished = task_finished;
467 } else {
468 ret = 1;
469 fprintf(stderr, "check usage\n");
470 goto cleanup;
472 opts.processes = jobs;
473 run_processes_parallel(&opts);
474 ret = 0;
475 cleanup:
476 child_process_clear(&proc);
477 return ret;