]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-run-command.c
The sixth batch
[thirdparty/git.git] / t / helper / test-run-command.c
CommitLineData
2b541bf8
JS
1/*
2 * test-run-command.c: test run command API.
3 *
4 * (C) 2009 Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
5 *
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 */
10
ae6a51f5 11#include "test-tool.h"
2b541bf8 12#include "run-command.h"
dbbcd44f 13#include "strvec.h"
c553c72e 14#include "strbuf.h"
be5d88e1
JS
15#include "parse-options.h"
16#include "string-list.h"
17#include "thread-utils.h"
18#include "wildmatch.h"
2b541bf8 19
c553c72e
SB
20static int number_callbacks;
21static int parallel_next(struct child_process *cp,
22 struct strbuf *err,
23 void *cb,
a5c76b36 24 void **task_cb UNUSED)
c553c72e
SB
25{
26 struct child_process *d = cb;
27 if (number_callbacks >= 4)
28 return 0;
29
87ee87dd 30 strvec_pushv(&cp->args, d->args.v);
fd3aaf53
ÆAB
31 if (err)
32 strbuf_addstr(err, "preloaded output of a child\n");
33 else
34 fprintf(stderr, "preloaded output of a child\n");
35
c553c72e
SB
36 number_callbacks++;
37 return 1;
38}
39
a5c76b36 40static int no_job(struct child_process *cp UNUSED,
c553c72e 41 struct strbuf *err,
a5c76b36
JK
42 void *cb UNUSED,
43 void **task_cb UNUSED)
c553c72e 44{
fd3aaf53
ÆAB
45 if (err)
46 strbuf_addstr(err, "no further jobs available\n");
47 else
48 fprintf(stderr, "no further jobs available\n");
c553c72e
SB
49 return 0;
50}
51
a5c76b36 52static int task_finished(int result UNUSED,
c553c72e 53 struct strbuf *err,
a5c76b36
JK
54 void *pp_cb UNUSED,
55 void *pp_task_cb UNUSED)
c553c72e 56{
fd3aaf53
ÆAB
57 if (err)
58 strbuf_addstr(err, "asking for a quick stop\n");
59 else
60 fprintf(stderr, "asking for a quick stop\n");
c553c72e
SB
61 return 1;
62}
63
be5d88e1
JS
64struct testsuite {
65 struct string_list tests, failed;
66 int next;
67 int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
22f0df7a 68 const char *shell_path;
be5d88e1 69};
f69a6e4f
ÆAB
70#define TESTSUITE_INIT { \
71 .tests = STRING_LIST_INIT_DUP, \
72 .failed = STRING_LIST_INIT_DUP, \
73}
be5d88e1
JS
74
75static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
76 void **task_cb)
77{
78 struct testsuite *suite = cb;
79 const char *test;
80 if (suite->next >= suite->tests.nr)
81 return 0;
82
83 test = suite->tests.items[suite->next++].string;
22f0df7a
JS
84 if (suite->shell_path)
85 strvec_push(&cp->args, suite->shell_path);
86 strvec_push(&cp->args, test);
be5d88e1 87 if (suite->quiet)
c972bf4c 88 strvec_push(&cp->args, "--quiet");
be5d88e1 89 if (suite->immediate)
c972bf4c 90 strvec_push(&cp->args, "-i");
be5d88e1 91 if (suite->verbose)
c972bf4c 92 strvec_push(&cp->args, "-v");
be5d88e1 93 if (suite->verbose_log)
c972bf4c 94 strvec_push(&cp->args, "-V");
be5d88e1 95 if (suite->trace)
c972bf4c 96 strvec_push(&cp->args, "-x");
be5d88e1 97 if (suite->write_junit_xml)
c972bf4c 98 strvec_push(&cp->args, "--write-junit-xml");
be5d88e1
JS
99
100 strbuf_addf(err, "Output of '%s':\n", test);
101 *task_cb = (void *)test;
102
103 return 1;
104}
105
106static int test_finished(int result, struct strbuf *err, void *cb,
107 void *task_cb)
108{
109 struct testsuite *suite = cb;
110 const char *name = (const char *)task_cb;
111
112 if (result)
113 string_list_append(&suite->failed, name);
114
115 strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
116
117 return 0;
118}
119
120static int test_failed(struct strbuf *out, void *cb, void *task_cb)
121{
122 struct testsuite *suite = cb;
123 const char *name = (const char *)task_cb;
124
125 string_list_append(&suite->failed, name);
126 strbuf_addf(out, "FAILED TO START: '%s'\n", name);
127
128 return 0;
129}
130
131static const char * const testsuite_usage[] = {
132 "test-run-command testsuite [<options>] [<pattern>...]",
133 NULL
134};
135
136static int testsuite(int argc, const char **argv)
137{
138 struct testsuite suite = TESTSUITE_INIT;
6e5ba0ba 139 int max_jobs = 1, i, ret = 0;
be5d88e1
JS
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()
154 };
6e5ba0ba
ÆAB
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,
160 };
a2b55e25
JS
161 struct strbuf progpath = STRBUF_INIT;
162 size_t path_prefix_len;
be5d88e1 163
be5d88e1
JS
164 argc = parse_options(argc, argv, NULL, options,
165 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
166
167 if (max_jobs <= 0)
168 max_jobs = online_cpus();
169
a2b55e25
JS
170 /*
171 * If we run without a shell, execute the programs directly from CWD.
172 */
22f0df7a
JS
173 suite.shell_path = getenv("TEST_SHELL_PATH");
174 if (!suite.shell_path)
a2b55e25
JS
175 strbuf_addstr(&progpath, "./");
176 path_prefix_len = progpath.len;
22f0df7a 177
be5d88e1
JS
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;
183
d28c5a52 184 if (!strcmp(p, ".") || !strcmp(p, ".."))
be5d88e1
JS
185 continue;
186
187 /* No pattern: match all */
188 if (!argc) {
a2b55e25
JS
189 strbuf_setlen(&progpath, path_prefix_len);
190 strbuf_addstr(&progpath, p);
191 string_list_append(&suite.tests, progpath.buf);
be5d88e1
JS
192 continue;
193 }
194
195 for (i = 0; i < argc; i++)
196 if (!wildmatch(argv[i], p, 0)) {
a2b55e25
JS
197 strbuf_setlen(&progpath, path_prefix_len);
198 strbuf_addstr(&progpath, p);
199 string_list_append(&suite.tests, progpath.buf);
be5d88e1
JS
200 break;
201 }
202 }
203 closedir(dir);
204
205 if (!suite.tests.nr)
206 die("No tests match!");
207 if (max_jobs > suite.tests.nr)
208 max_jobs = suite.tests.nr;
209
99d60545
ÆAB
210 fprintf(stderr, "Running %"PRIuMAX" tests (%d at a time)\n",
211 (uintmax_t)suite.tests.nr, max_jobs);
be5d88e1 212
6e5ba0ba
ÆAB
213 opts.processes = max_jobs;
214 run_processes_parallel(&opts);
be5d88e1
JS
215
216 if (suite.failed.nr > 0) {
217 ret = 1;
99d60545
ÆAB
218 fprintf(stderr, "%"PRIuMAX" tests failed:\n\n",
219 (uintmax_t)suite.failed.nr);
be5d88e1
JS
220 for (i = 0; i < suite.failed.nr; i++)
221 fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
222 }
223
224 string_list_clear(&suite.tests, 0);
225 string_list_clear(&suite.failed, 0);
a2b55e25 226 strbuf_release(&progpath);
be5d88e1 227
6e5ba0ba 228 return ret;
be5d88e1
JS
229}
230
ad155925
GS
231static uint64_t my_random_next = 1234;
232
233static uint64_t my_random(void)
234{
235 uint64_t res = my_random_next;
236 my_random_next = my_random_next * 1103515245 + 12345;
237 return res;
238}
239
240static int quote_stress_test(int argc, const char **argv)
241{
242 /*
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.
247 */
248 char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
379e51d1 249 int i, j, k, trials = 100, skip = 0, msys2 = 0;
ad155925 250 struct strbuf out = STRBUF_INIT;
c972bf4c 251 struct strvec args = STRVEC_INIT;
ad155925 252 struct option options[] = {
9e1f22c8
AC
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"),
ad155925
GS
256 OPT_END()
257 };
258 const char * const usage[] = {
14af7ed5 259 "test-tool run-command quote-stress-test <options>",
ad155925
GS
260 NULL
261 };
262
263 argc = parse_options(argc, argv, NULL, options, usage, 0);
264
379e51d1
JS
265 setenv("MSYS_NO_PATHCONV", "1", 0);
266
ad155925
GS
267 for (i = 0; i < trials; i++) {
268 struct child_process cp = CHILD_PROCESS_INIT;
55953c77 269 size_t arg_count, arg_offset;
ad155925
GS
270 int ret = 0;
271
c972bf4c 272 strvec_clear(&args);
379e51d1 273 if (msys2)
c972bf4c 274 strvec_pushl(&args, "sh", "-c",
f6d8942b 275 "printf %s\\\\0 \"$@\"", "skip", NULL);
379e51d1 276 else
c972bf4c 277 strvec_pushl(&args, "test-tool", "run-command",
f6d8942b 278 "quote-echo", NULL);
d70a9eb6 279 arg_offset = args.nr;
55953c77
JS
280
281 if (argc > 0) {
282 trials = 1;
283 arg_count = argc;
284 for (j = 0; j < arg_count; j++)
c972bf4c 285 strvec_push(&args, argv[j]);
55953c77
JS
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));
293
294 for (k = 0; k < arg_len; k++)
295 buf[k] = special[my_random() %
296 ARRAY_SIZE(special)];
297 buf[arg_len] = '\0';
298
c972bf4c 299 strvec_push(&args, buf);
55953c77 300 }
ad155925
GS
301 }
302
7530a628
JS
303 if (i < skip)
304 continue;
305
87ee87dd 306 strvec_pushv(&cp.args, args.v);
ad155925
GS
307 strbuf_reset(&out);
308 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
309 return error("Failed to spawn child process");
310
311 for (j = 0, k = 0; j < arg_count; j++) {
d70a9eb6 312 const char *arg = args.v[j + arg_offset];
ad155925
GS
313
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;
319 }
320
321 if (k != out.len)
322 ret = error("got %d bytes, but consumed only %d",
323 (int)out.len, (int)k);
324
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",
d70a9eb6 329 (int)j, args.v[j + arg_offset]);
ad155925
GS
330
331 strbuf_release(&out);
c972bf4c 332 strvec_clear(&args);
ad155925
GS
333
334 return ret;
335 }
336
337 if (i && (i % 100) == 0)
338 fprintf(stderr, "Trials completed: %d\n", (int)i);
339 }
340
341 strbuf_release(&out);
c972bf4c 342 strvec_clear(&args);
ad155925
GS
343
344 return 0;
345}
346
347static int quote_echo(int argc, const char **argv)
348{
349 while (argc > 1) {
350 fwrite(argv[1], strlen(argv[1]), 1, stdout);
351 fputc('\0', stdout);
352 argv++;
353 argc--;
354 }
355
356 return 0;
357}
358
eea4a7f4
JS
359static int inherit_handle(const char *argv0)
360{
361 struct child_process cp = CHILD_PROCESS_INIT;
362 char path[PATH_MAX];
363 int tmp;
364
365 /* First, open an inheritable handle */
366 xsnprintf(path, sizeof(path), "out-XXXXXX");
367 tmp = xmkstemp(path);
368
c972bf4c 369 strvec_pushl(&cp.args,
f6d8942b 370 "test-tool", argv0, "inherited-handle-child", NULL);
eea4a7f4
JS
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");
375
376 /* Then close it, and try to delete it. */
377 close(tmp);
378 if (unlink(path))
379 die("Could not delete '%s'", path);
380
381 if (close(cp.in) < 0 || finish_command(&cp) < 0)
382 die("Child did not finish");
383
384 return 0;
385}
386
387static int inherit_handle_child(void)
388{
389 struct strbuf buf = STRBUF_INIT;
390
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);
395
396 return 0;
397}
398
ae6a51f5 399int cmd__run_command(int argc, const char **argv)
2b541bf8 400{
d3180279 401 struct child_process proc = CHILD_PROCESS_INIT;
c553c72e 402 int jobs;
910e2b37 403 int ret;
6e5ba0ba
ÆAB
404 struct run_process_parallel_opts opts = {
405 .data = &proc,
406 };
2b541bf8 407
be5d88e1 408 if (argc > 1 && !strcmp(argv[1], "testsuite"))
910e2b37 409 return testsuite(argc - 1, argv + 1);
eea4a7f4 410 if (!strcmp(argv[1], "inherited-handle"))
910e2b37 411 return inherit_handle(argv[0]);
eea4a7f4 412 if (!strcmp(argv[1], "inherited-handle-child"))
910e2b37 413 return inherit_handle_child();
be5d88e1 414
ad155925
GS
415 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
416 return !!quote_stress_test(argc - 1, argv + 1);
417
418 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
419 return !!quote_echo(argc - 1, argv + 1);
420
c61a975d
NTND
421 if (argc < 3)
422 return 1;
423 while (!strcmp(argv[1], "env")) {
424 if (!argv[2])
425 die("env specifier without a value");
29fda24d 426 strvec_push(&proc.env, argv[2]);
c61a975d
NTND
427 argv += 2;
428 argc -= 2;
429 }
910e2b37
ÆAB
430 if (argc < 3) {
431 ret = 1;
432 goto cleanup;
433 }
87ee87dd 434 strvec_pushv(&proc.args, (const char **)argv + 2);
2b541bf8
JS
435
436 if (!strcmp(argv[1], "start-command-ENOENT")) {
910e2b37
ÆAB
437 if (start_command(&proc) < 0 && errno == ENOENT) {
438 ret = 0;
439 goto cleanup;
440 }
2b541bf8
JS
441 fprintf(stderr, "FAIL %s\n", argv[1]);
442 return 1;
443 }
910e2b37
ÆAB
444 if (!strcmp(argv[1], "run-command")) {
445 ret = run_command(&proc);
446 goto cleanup;
447 }
2b541bf8 448
fd3aaf53
ÆAB
449 if (!strcmp(argv[1], "--ungroup")) {
450 argv += 1;
451 argc -= 1;
6e5ba0ba 452 opts.ungroup = 1;
fd3aaf53
ÆAB
453 }
454
c553c72e 455 jobs = atoi(argv[2]);
87ee87dd
ÆAB
456 strvec_clear(&proc.args);
457 strvec_pushv(&proc.args, (const char **)argv + 3);
c553c72e 458
a083f94c 459 if (!strcmp(argv[1], "run-command-parallel")) {
6e5ba0ba 460 opts.get_next_task = parallel_next;
a083f94c 461 } else if (!strcmp(argv[1], "run-command-abort")) {
6e5ba0ba
ÆAB
462 opts.get_next_task = parallel_next;
463 opts.task_finished = task_finished;
a083f94c 464 } else if (!strcmp(argv[1], "run-command-no-jobs")) {
6e5ba0ba
ÆAB
465 opts.get_next_task = no_job;
466 opts.task_finished = task_finished;
a083f94c 467 } else {
910e2b37 468 ret = 1;
a083f94c 469 fprintf(stderr, "check usage\n");
910e2b37 470 goto cleanup;
a083f94c 471 }
6e5ba0ba
ÆAB
472 opts.processes = jobs;
473 run_processes_parallel(&opts);
910e2b37
ÆAB
474 ret = 0;
475cleanup:
476 child_process_clear(&proc);
477 return ret;
2b541bf8 478}