Merge branch 'jc/t4204-do-not-write-git-on-upstream-of-pipe'
[git/debian.git] / t / helper / test-run-command.c
blob913775a14b758e5b4dd89cbce9ead4ff104abbc1
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 "git-compat-util.h"
13 #include "cache.h"
14 #include "run-command.h"
15 #include "strvec.h"
16 #include "strbuf.h"
17 #include "parse-options.h"
18 #include "string-list.h"
19 #include "thread-utils.h"
20 #include "wildmatch.h"
21 #include "gettext.h"
22 #include "parse-options.h"
24 static int number_callbacks;
25 static int parallel_next(struct child_process *cp,
26 struct strbuf *err,
27 void *cb,
28 void **task_cb)
30 struct child_process *d = cb;
31 if (number_callbacks >= 4)
32 return 0;
34 strvec_pushv(&cp->args, d->args.v);
35 strbuf_addstr(err, "preloaded output of a child\n");
36 number_callbacks++;
37 return 1;
40 static int no_job(struct child_process *cp,
41 struct strbuf *err,
42 void *cb,
43 void **task_cb)
45 strbuf_addstr(err, "no further jobs available\n");
46 return 0;
49 static int task_finished(int result,
50 struct strbuf *err,
51 void *pp_cb,
52 void *pp_task_cb)
54 strbuf_addstr(err, "asking for a quick stop\n");
55 return 1;
58 struct testsuite {
59 struct string_list tests, failed;
60 int next;
61 int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
63 #define TESTSUITE_INIT { \
64 .tests = STRING_LIST_INIT_DUP, \
65 .failed = STRING_LIST_INIT_DUP, \
68 static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
69 void **task_cb)
71 struct testsuite *suite = cb;
72 const char *test;
73 if (suite->next >= suite->tests.nr)
74 return 0;
76 test = suite->tests.items[suite->next++].string;
77 strvec_pushl(&cp->args, "sh", test, NULL);
78 if (suite->quiet)
79 strvec_push(&cp->args, "--quiet");
80 if (suite->immediate)
81 strvec_push(&cp->args, "-i");
82 if (suite->verbose)
83 strvec_push(&cp->args, "-v");
84 if (suite->verbose_log)
85 strvec_push(&cp->args, "-V");
86 if (suite->trace)
87 strvec_push(&cp->args, "-x");
88 if (suite->write_junit_xml)
89 strvec_push(&cp->args, "--write-junit-xml");
91 strbuf_addf(err, "Output of '%s':\n", test);
92 *task_cb = (void *)test;
94 return 1;
97 static int test_finished(int result, struct strbuf *err, void *cb,
98 void *task_cb)
100 struct testsuite *suite = cb;
101 const char *name = (const char *)task_cb;
103 if (result)
104 string_list_append(&suite->failed, name);
106 strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
108 return 0;
111 static int test_failed(struct strbuf *out, void *cb, void *task_cb)
113 struct testsuite *suite = cb;
114 const char *name = (const char *)task_cb;
116 string_list_append(&suite->failed, name);
117 strbuf_addf(out, "FAILED TO START: '%s'\n", name);
119 return 0;
122 static const char * const testsuite_usage[] = {
123 "test-run-command testsuite [<options>] [<pattern>...]",
124 NULL
127 static int testsuite(int argc, const char **argv)
129 struct testsuite suite = TESTSUITE_INIT;
130 int max_jobs = 1, i, ret;
131 DIR *dir;
132 struct dirent *d;
133 struct option options[] = {
134 OPT_BOOL('i', "immediate", &suite.immediate,
135 "stop at first failed test case(s)"),
136 OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
137 OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
138 OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
139 OPT_BOOL('V', "verbose-log", &suite.verbose_log,
140 "be verbose, redirected to a file"),
141 OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
142 OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
143 "write JUnit-style XML files"),
144 OPT_END()
147 argc = parse_options(argc, argv, NULL, options,
148 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
150 if (max_jobs <= 0)
151 max_jobs = online_cpus();
153 dir = opendir(".");
154 if (!dir)
155 die("Could not open the current directory");
156 while ((d = readdir(dir))) {
157 const char *p = d->d_name;
159 if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
160 !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
161 !ends_with(p, ".sh"))
162 continue;
164 /* No pattern: match all */
165 if (!argc) {
166 string_list_append(&suite.tests, p);
167 continue;
170 for (i = 0; i < argc; i++)
171 if (!wildmatch(argv[i], p, 0)) {
172 string_list_append(&suite.tests, p);
173 break;
176 closedir(dir);
178 if (!suite.tests.nr)
179 die("No tests match!");
180 if (max_jobs > suite.tests.nr)
181 max_jobs = suite.tests.nr;
183 fprintf(stderr, "Running %d tests (%d at a time)\n",
184 suite.tests.nr, max_jobs);
186 ret = run_processes_parallel(max_jobs, next_test, test_failed,
187 test_finished, &suite);
189 if (suite.failed.nr > 0) {
190 ret = 1;
191 fprintf(stderr, "%d tests failed:\n\n", suite.failed.nr);
192 for (i = 0; i < suite.failed.nr; i++)
193 fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
196 string_list_clear(&suite.tests, 0);
197 string_list_clear(&suite.failed, 0);
199 return !!ret;
202 static uint64_t my_random_next = 1234;
204 static uint64_t my_random(void)
206 uint64_t res = my_random_next;
207 my_random_next = my_random_next * 1103515245 + 12345;
208 return res;
211 static int quote_stress_test(int argc, const char **argv)
214 * We are running a quote-stress test.
215 * spawn a subprocess that runs quote-stress with a
216 * special option that echoes back the arguments that
217 * were passed in.
219 char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
220 int i, j, k, trials = 100, skip = 0, msys2 = 0;
221 struct strbuf out = STRBUF_INIT;
222 struct strvec args = STRVEC_INIT;
223 struct option options[] = {
224 OPT_INTEGER('n', "trials", &trials, "Number of trials"),
225 OPT_INTEGER('s', "skip", &skip, "Skip <n> trials"),
226 OPT_BOOL('m', "msys2", &msys2, "Test quoting for MSYS2's sh"),
227 OPT_END()
229 const char * const usage[] = {
230 "test-tool run-command quote-stress-test <options>",
231 NULL
234 argc = parse_options(argc, argv, NULL, options, usage, 0);
236 setenv("MSYS_NO_PATHCONV", "1", 0);
238 for (i = 0; i < trials; i++) {
239 struct child_process cp = CHILD_PROCESS_INIT;
240 size_t arg_count, arg_offset;
241 int ret = 0;
243 strvec_clear(&args);
244 if (msys2)
245 strvec_pushl(&args, "sh", "-c",
246 "printf %s\\\\0 \"$@\"", "skip", NULL);
247 else
248 strvec_pushl(&args, "test-tool", "run-command",
249 "quote-echo", NULL);
250 arg_offset = args.nr;
252 if (argc > 0) {
253 trials = 1;
254 arg_count = argc;
255 for (j = 0; j < arg_count; j++)
256 strvec_push(&args, argv[j]);
257 } else {
258 arg_count = 1 + (my_random() % 5);
259 for (j = 0; j < arg_count; j++) {
260 char buf[20];
261 size_t min_len = 1;
262 size_t arg_len = min_len +
263 (my_random() % (ARRAY_SIZE(buf) - min_len));
265 for (k = 0; k < arg_len; k++)
266 buf[k] = special[my_random() %
267 ARRAY_SIZE(special)];
268 buf[arg_len] = '\0';
270 strvec_push(&args, buf);
274 if (i < skip)
275 continue;
277 strvec_pushv(&cp.args, args.v);
278 strbuf_reset(&out);
279 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
280 return error("Failed to spawn child process");
282 for (j = 0, k = 0; j < arg_count; j++) {
283 const char *arg = args.v[j + arg_offset];
285 if (strcmp(arg, out.buf + k))
286 ret = error("incorrectly quoted arg: '%s', "
287 "echoed back as '%s'",
288 arg, out.buf + k);
289 k += strlen(out.buf + k) + 1;
292 if (k != out.len)
293 ret = error("got %d bytes, but consumed only %d",
294 (int)out.len, (int)k);
296 if (ret) {
297 fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
298 for (j = 0; j < arg_count; j++)
299 fprintf(stderr, "arg #%d: '%s'\n",
300 (int)j, args.v[j + arg_offset]);
302 strbuf_release(&out);
303 strvec_clear(&args);
305 return ret;
308 if (i && (i % 100) == 0)
309 fprintf(stderr, "Trials completed: %d\n", (int)i);
312 strbuf_release(&out);
313 strvec_clear(&args);
315 return 0;
318 static int quote_echo(int argc, const char **argv)
320 while (argc > 1) {
321 fwrite(argv[1], strlen(argv[1]), 1, stdout);
322 fputc('\0', stdout);
323 argv++;
324 argc--;
327 return 0;
330 static int inherit_handle(const char *argv0)
332 struct child_process cp = CHILD_PROCESS_INIT;
333 char path[PATH_MAX];
334 int tmp;
336 /* First, open an inheritable handle */
337 xsnprintf(path, sizeof(path), "out-XXXXXX");
338 tmp = xmkstemp(path);
340 strvec_pushl(&cp.args,
341 "test-tool", argv0, "inherited-handle-child", NULL);
342 cp.in = -1;
343 cp.no_stdout = cp.no_stderr = 1;
344 if (start_command(&cp) < 0)
345 die("Could not start child process");
347 /* Then close it, and try to delete it. */
348 close(tmp);
349 if (unlink(path))
350 die("Could not delete '%s'", path);
352 if (close(cp.in) < 0 || finish_command(&cp) < 0)
353 die("Child did not finish");
355 return 0;
358 static int inherit_handle_child(void)
360 struct strbuf buf = STRBUF_INIT;
362 if (strbuf_read(&buf, 0, 0) < 0)
363 die("Could not read stdin");
364 printf("Received %s\n", buf.buf);
365 strbuf_release(&buf);
367 return 0;
370 int cmd__run_command(int argc, const char **argv)
372 struct child_process proc = CHILD_PROCESS_INIT;
373 int jobs;
375 if (argc > 1 && !strcmp(argv[1], "testsuite"))
376 exit(testsuite(argc - 1, argv + 1));
377 if (!strcmp(argv[1], "inherited-handle"))
378 exit(inherit_handle(argv[0]));
379 if (!strcmp(argv[1], "inherited-handle-child"))
380 exit(inherit_handle_child());
382 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
383 return !!quote_stress_test(argc - 1, argv + 1);
385 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
386 return !!quote_echo(argc - 1, argv + 1);
388 if (argc < 3)
389 return 1;
390 while (!strcmp(argv[1], "env")) {
391 if (!argv[2])
392 die("env specifier without a value");
393 strvec_push(&proc.env_array, argv[2]);
394 argv += 2;
395 argc -= 2;
397 if (argc < 3)
398 return 1;
399 strvec_pushv(&proc.args, (const char **)argv + 2);
401 if (!strcmp(argv[1], "start-command-ENOENT")) {
402 if (start_command(&proc) < 0 && errno == ENOENT)
403 return 0;
404 fprintf(stderr, "FAIL %s\n", argv[1]);
405 return 1;
407 if (!strcmp(argv[1], "run-command"))
408 exit(run_command(&proc));
410 jobs = atoi(argv[2]);
411 strvec_clear(&proc.args);
412 strvec_pushv(&proc.args, (const char **)argv + 3);
414 if (!strcmp(argv[1], "run-command-parallel"))
415 exit(run_processes_parallel(jobs, parallel_next,
416 NULL, NULL, &proc));
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");
427 return 1;