]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-run-command.c
run-command API: have run_process_parallel() take an "opts" struct
[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 "git-compat-util.h"
be5d88e1 13#include "cache.h"
2b541bf8 14#include "run-command.h"
dbbcd44f 15#include "strvec.h"
c553c72e 16#include "strbuf.h"
be5d88e1
JS
17#include "parse-options.h"
18#include "string-list.h"
19#include "thread-utils.h"
20#include "wildmatch.h"
ad155925 21#include "gettext.h"
2b541bf8 22
c553c72e
SB
23static int number_callbacks;
24static int parallel_next(struct child_process *cp,
25 struct strbuf *err,
26 void *cb,
27 void **task_cb)
28{
29 struct child_process *d = cb;
30 if (number_callbacks >= 4)
31 return 0;
32
87ee87dd 33 strvec_pushv(&cp->args, d->args.v);
fd3aaf53
ÆAB
34 if (err)
35 strbuf_addstr(err, "preloaded output of a child\n");
36 else
37 fprintf(stderr, "preloaded output of a child\n");
38
c553c72e
SB
39 number_callbacks++;
40 return 1;
41}
42
43static int no_job(struct child_process *cp,
44 struct strbuf *err,
45 void *cb,
46 void **task_cb)
47{
fd3aaf53
ÆAB
48 if (err)
49 strbuf_addstr(err, "no further jobs available\n");
50 else
51 fprintf(stderr, "no further jobs available\n");
c553c72e
SB
52 return 0;
53}
54
55static int task_finished(int result,
c553c72e
SB
56 struct strbuf *err,
57 void *pp_cb,
58 void *pp_task_cb)
59{
fd3aaf53
ÆAB
60 if (err)
61 strbuf_addstr(err, "asking for a quick stop\n");
62 else
63 fprintf(stderr, "asking for a quick stop\n");
c553c72e
SB
64 return 1;
65}
66
be5d88e1
JS
67struct testsuite {
68 struct string_list tests, failed;
69 int next;
70 int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
71};
f69a6e4f
ÆAB
72#define TESTSUITE_INIT { \
73 .tests = STRING_LIST_INIT_DUP, \
74 .failed = STRING_LIST_INIT_DUP, \
75}
be5d88e1
JS
76
77static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
78 void **task_cb)
79{
80 struct testsuite *suite = cb;
81 const char *test;
82 if (suite->next >= suite->tests.nr)
83 return 0;
84
85 test = suite->tests.items[suite->next++].string;
c972bf4c 86 strvec_pushl(&cp->args, "sh", test, NULL);
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 };
be5d88e1 161
be5d88e1
JS
162 argc = parse_options(argc, argv, NULL, options,
163 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
164
165 if (max_jobs <= 0)
166 max_jobs = online_cpus();
167
168 dir = opendir(".");
169 if (!dir)
170 die("Could not open the current directory");
171 while ((d = readdir(dir))) {
172 const char *p = d->d_name;
173
174 if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
175 !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
176 !ends_with(p, ".sh"))
177 continue;
178
179 /* No pattern: match all */
180 if (!argc) {
181 string_list_append(&suite.tests, p);
182 continue;
183 }
184
185 for (i = 0; i < argc; i++)
186 if (!wildmatch(argv[i], p, 0)) {
187 string_list_append(&suite.tests, p);
188 break;
189 }
190 }
191 closedir(dir);
192
193 if (!suite.tests.nr)
194 die("No tests match!");
195 if (max_jobs > suite.tests.nr)
196 max_jobs = suite.tests.nr;
197
99d60545
ÆAB
198 fprintf(stderr, "Running %"PRIuMAX" tests (%d at a time)\n",
199 (uintmax_t)suite.tests.nr, max_jobs);
be5d88e1 200
6e5ba0ba
ÆAB
201 opts.processes = max_jobs;
202 run_processes_parallel(&opts);
be5d88e1
JS
203
204 if (suite.failed.nr > 0) {
205 ret = 1;
99d60545
ÆAB
206 fprintf(stderr, "%"PRIuMAX" tests failed:\n\n",
207 (uintmax_t)suite.failed.nr);
be5d88e1
JS
208 for (i = 0; i < suite.failed.nr; i++)
209 fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
210 }
211
212 string_list_clear(&suite.tests, 0);
213 string_list_clear(&suite.failed, 0);
214
6e5ba0ba 215 return ret;
be5d88e1
JS
216}
217
ad155925
GS
218static uint64_t my_random_next = 1234;
219
220static uint64_t my_random(void)
221{
222 uint64_t res = my_random_next;
223 my_random_next = my_random_next * 1103515245 + 12345;
224 return res;
225}
226
227static int quote_stress_test(int argc, const char **argv)
228{
229 /*
230 * We are running a quote-stress test.
231 * spawn a subprocess that runs quote-stress with a
232 * special option that echoes back the arguments that
233 * were passed in.
234 */
235 char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
379e51d1 236 int i, j, k, trials = 100, skip = 0, msys2 = 0;
ad155925 237 struct strbuf out = STRBUF_INIT;
c972bf4c 238 struct strvec args = STRVEC_INIT;
ad155925 239 struct option options[] = {
9e1f22c8
AC
240 OPT_INTEGER('n', "trials", &trials, "number of trials"),
241 OPT_INTEGER('s', "skip", &skip, "skip <n> trials"),
242 OPT_BOOL('m', "msys2", &msys2, "test quoting for MSYS2's sh"),
ad155925
GS
243 OPT_END()
244 };
245 const char * const usage[] = {
14af7ed5 246 "test-tool run-command quote-stress-test <options>",
ad155925
GS
247 NULL
248 };
249
250 argc = parse_options(argc, argv, NULL, options, usage, 0);
251
379e51d1
JS
252 setenv("MSYS_NO_PATHCONV", "1", 0);
253
ad155925
GS
254 for (i = 0; i < trials; i++) {
255 struct child_process cp = CHILD_PROCESS_INIT;
55953c77 256 size_t arg_count, arg_offset;
ad155925
GS
257 int ret = 0;
258
c972bf4c 259 strvec_clear(&args);
379e51d1 260 if (msys2)
c972bf4c 261 strvec_pushl(&args, "sh", "-c",
f6d8942b 262 "printf %s\\\\0 \"$@\"", "skip", NULL);
379e51d1 263 else
c972bf4c 264 strvec_pushl(&args, "test-tool", "run-command",
f6d8942b 265 "quote-echo", NULL);
d70a9eb6 266 arg_offset = args.nr;
55953c77
JS
267
268 if (argc > 0) {
269 trials = 1;
270 arg_count = argc;
271 for (j = 0; j < arg_count; j++)
c972bf4c 272 strvec_push(&args, argv[j]);
55953c77
JS
273 } else {
274 arg_count = 1 + (my_random() % 5);
275 for (j = 0; j < arg_count; j++) {
276 char buf[20];
277 size_t min_len = 1;
278 size_t arg_len = min_len +
279 (my_random() % (ARRAY_SIZE(buf) - min_len));
280
281 for (k = 0; k < arg_len; k++)
282 buf[k] = special[my_random() %
283 ARRAY_SIZE(special)];
284 buf[arg_len] = '\0';
285
c972bf4c 286 strvec_push(&args, buf);
55953c77 287 }
ad155925
GS
288 }
289
7530a628
JS
290 if (i < skip)
291 continue;
292
87ee87dd 293 strvec_pushv(&cp.args, args.v);
ad155925
GS
294 strbuf_reset(&out);
295 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
296 return error("Failed to spawn child process");
297
298 for (j = 0, k = 0; j < arg_count; j++) {
d70a9eb6 299 const char *arg = args.v[j + arg_offset];
ad155925
GS
300
301 if (strcmp(arg, out.buf + k))
302 ret = error("incorrectly quoted arg: '%s', "
303 "echoed back as '%s'",
304 arg, out.buf + k);
305 k += strlen(out.buf + k) + 1;
306 }
307
308 if (k != out.len)
309 ret = error("got %d bytes, but consumed only %d",
310 (int)out.len, (int)k);
311
312 if (ret) {
313 fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
314 for (j = 0; j < arg_count; j++)
315 fprintf(stderr, "arg #%d: '%s'\n",
d70a9eb6 316 (int)j, args.v[j + arg_offset]);
ad155925
GS
317
318 strbuf_release(&out);
c972bf4c 319 strvec_clear(&args);
ad155925
GS
320
321 return ret;
322 }
323
324 if (i && (i % 100) == 0)
325 fprintf(stderr, "Trials completed: %d\n", (int)i);
326 }
327
328 strbuf_release(&out);
c972bf4c 329 strvec_clear(&args);
ad155925
GS
330
331 return 0;
332}
333
334static int quote_echo(int argc, const char **argv)
335{
336 while (argc > 1) {
337 fwrite(argv[1], strlen(argv[1]), 1, stdout);
338 fputc('\0', stdout);
339 argv++;
340 argc--;
341 }
342
343 return 0;
344}
345
eea4a7f4
JS
346static int inherit_handle(const char *argv0)
347{
348 struct child_process cp = CHILD_PROCESS_INIT;
349 char path[PATH_MAX];
350 int tmp;
351
352 /* First, open an inheritable handle */
353 xsnprintf(path, sizeof(path), "out-XXXXXX");
354 tmp = xmkstemp(path);
355
c972bf4c 356 strvec_pushl(&cp.args,
f6d8942b 357 "test-tool", argv0, "inherited-handle-child", NULL);
eea4a7f4
JS
358 cp.in = -1;
359 cp.no_stdout = cp.no_stderr = 1;
360 if (start_command(&cp) < 0)
361 die("Could not start child process");
362
363 /* Then close it, and try to delete it. */
364 close(tmp);
365 if (unlink(path))
366 die("Could not delete '%s'", path);
367
368 if (close(cp.in) < 0 || finish_command(&cp) < 0)
369 die("Child did not finish");
370
371 return 0;
372}
373
374static int inherit_handle_child(void)
375{
376 struct strbuf buf = STRBUF_INIT;
377
378 if (strbuf_read(&buf, 0, 0) < 0)
379 die("Could not read stdin");
380 printf("Received %s\n", buf.buf);
381 strbuf_release(&buf);
382
383 return 0;
384}
385
ae6a51f5 386int cmd__run_command(int argc, const char **argv)
2b541bf8 387{
d3180279 388 struct child_process proc = CHILD_PROCESS_INIT;
c553c72e 389 int jobs;
910e2b37 390 int ret;
6e5ba0ba
ÆAB
391 struct run_process_parallel_opts opts = {
392 .data = &proc,
393 };
2b541bf8 394
be5d88e1 395 if (argc > 1 && !strcmp(argv[1], "testsuite"))
910e2b37 396 return testsuite(argc - 1, argv + 1);
eea4a7f4 397 if (!strcmp(argv[1], "inherited-handle"))
910e2b37 398 return inherit_handle(argv[0]);
eea4a7f4 399 if (!strcmp(argv[1], "inherited-handle-child"))
910e2b37 400 return inherit_handle_child();
be5d88e1 401
ad155925
GS
402 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
403 return !!quote_stress_test(argc - 1, argv + 1);
404
405 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
406 return !!quote_echo(argc - 1, argv + 1);
407
c61a975d
NTND
408 if (argc < 3)
409 return 1;
410 while (!strcmp(argv[1], "env")) {
411 if (!argv[2])
412 die("env specifier without a value");
29fda24d 413 strvec_push(&proc.env, argv[2]);
c61a975d
NTND
414 argv += 2;
415 argc -= 2;
416 }
910e2b37
ÆAB
417 if (argc < 3) {
418 ret = 1;
419 goto cleanup;
420 }
87ee87dd 421 strvec_pushv(&proc.args, (const char **)argv + 2);
2b541bf8
JS
422
423 if (!strcmp(argv[1], "start-command-ENOENT")) {
910e2b37
ÆAB
424 if (start_command(&proc) < 0 && errno == ENOENT) {
425 ret = 0;
426 goto cleanup;
427 }
2b541bf8
JS
428 fprintf(stderr, "FAIL %s\n", argv[1]);
429 return 1;
430 }
910e2b37
ÆAB
431 if (!strcmp(argv[1], "run-command")) {
432 ret = run_command(&proc);
433 goto cleanup;
434 }
2b541bf8 435
fd3aaf53
ÆAB
436 if (!strcmp(argv[1], "--ungroup")) {
437 argv += 1;
438 argc -= 1;
6e5ba0ba 439 opts.ungroup = 1;
fd3aaf53
ÆAB
440 }
441
c553c72e 442 jobs = atoi(argv[2]);
87ee87dd
ÆAB
443 strvec_clear(&proc.args);
444 strvec_pushv(&proc.args, (const char **)argv + 3);
c553c72e 445
a083f94c 446 if (!strcmp(argv[1], "run-command-parallel")) {
6e5ba0ba 447 opts.get_next_task = parallel_next;
a083f94c 448 } else if (!strcmp(argv[1], "run-command-abort")) {
6e5ba0ba
ÆAB
449 opts.get_next_task = parallel_next;
450 opts.task_finished = task_finished;
a083f94c 451 } else if (!strcmp(argv[1], "run-command-no-jobs")) {
6e5ba0ba
ÆAB
452 opts.get_next_task = no_job;
453 opts.task_finished = task_finished;
a083f94c 454 } else {
910e2b37 455 ret = 1;
a083f94c 456 fprintf(stderr, "check usage\n");
910e2b37 457 goto cleanup;
a083f94c 458 }
6e5ba0ba
ÆAB
459 opts.processes = jobs;
460 run_processes_parallel(&opts);
910e2b37
ÆAB
461 ret = 0;
462cleanup:
463 child_process_clear(&proc);
464 return ret;
2b541bf8 465}