]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-run-command.c
Merge branch 'en/fetch-negotiation-default-fix'
[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
GS
21#include "gettext.h"
22#include "parse-options.h"
2b541bf8 23
c553c72e
SB
24static int number_callbacks;
25static int parallel_next(struct child_process *cp,
26 struct strbuf *err,
27 void *cb,
28 void **task_cb)
29{
30 struct child_process *d = cb;
31 if (number_callbacks >= 4)
32 return 0;
33
87ee87dd 34 strvec_pushv(&cp->args, d->args.v);
02962d36 35 strbuf_addstr(err, "preloaded output of a child\n");
c553c72e
SB
36 number_callbacks++;
37 return 1;
38}
39
40static int no_job(struct child_process *cp,
41 struct strbuf *err,
42 void *cb,
43 void **task_cb)
44{
02962d36 45 strbuf_addstr(err, "no further jobs available\n");
c553c72e
SB
46 return 0;
47}
48
49static int task_finished(int result,
c553c72e
SB
50 struct strbuf *err,
51 void *pp_cb,
52 void *pp_task_cb)
53{
02962d36 54 strbuf_addstr(err, "asking for a quick stop\n");
c553c72e
SB
55 return 1;
56}
57
be5d88e1
JS
58struct testsuite {
59 struct string_list tests, failed;
60 int next;
61 int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
62};
f69a6e4f
ÆAB
63#define TESTSUITE_INIT { \
64 .tests = STRING_LIST_INIT_DUP, \
65 .failed = STRING_LIST_INIT_DUP, \
66}
be5d88e1
JS
67
68static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
69 void **task_cb)
70{
71 struct testsuite *suite = cb;
72 const char *test;
73 if (suite->next >= suite->tests.nr)
74 return 0;
75
76 test = suite->tests.items[suite->next++].string;
c972bf4c 77 strvec_pushl(&cp->args, "sh", test, NULL);
be5d88e1 78 if (suite->quiet)
c972bf4c 79 strvec_push(&cp->args, "--quiet");
be5d88e1 80 if (suite->immediate)
c972bf4c 81 strvec_push(&cp->args, "-i");
be5d88e1 82 if (suite->verbose)
c972bf4c 83 strvec_push(&cp->args, "-v");
be5d88e1 84 if (suite->verbose_log)
c972bf4c 85 strvec_push(&cp->args, "-V");
be5d88e1 86 if (suite->trace)
c972bf4c 87 strvec_push(&cp->args, "-x");
be5d88e1 88 if (suite->write_junit_xml)
c972bf4c 89 strvec_push(&cp->args, "--write-junit-xml");
be5d88e1
JS
90
91 strbuf_addf(err, "Output of '%s':\n", test);
92 *task_cb = (void *)test;
93
94 return 1;
95}
96
97static int test_finished(int result, struct strbuf *err, void *cb,
98 void *task_cb)
99{
100 struct testsuite *suite = cb;
101 const char *name = (const char *)task_cb;
102
103 if (result)
104 string_list_append(&suite->failed, name);
105
106 strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
107
108 return 0;
109}
110
111static int test_failed(struct strbuf *out, void *cb, void *task_cb)
112{
113 struct testsuite *suite = cb;
114 const char *name = (const char *)task_cb;
115
116 string_list_append(&suite->failed, name);
117 strbuf_addf(out, "FAILED TO START: '%s'\n", name);
118
119 return 0;
120}
121
122static const char * const testsuite_usage[] = {
123 "test-run-command testsuite [<options>] [<pattern>...]",
124 NULL
125};
126
127static int testsuite(int argc, const char **argv)
128{
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()
145 };
146
be5d88e1
JS
147 argc = parse_options(argc, argv, NULL, options,
148 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
149
150 if (max_jobs <= 0)
151 max_jobs = online_cpus();
152
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;
158
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;
163
164 /* No pattern: match all */
165 if (!argc) {
166 string_list_append(&suite.tests, p);
167 continue;
168 }
169
170 for (i = 0; i < argc; i++)
171 if (!wildmatch(argv[i], p, 0)) {
172 string_list_append(&suite.tests, p);
173 break;
174 }
175 }
176 closedir(dir);
177
178 if (!suite.tests.nr)
179 die("No tests match!");
180 if (max_jobs > suite.tests.nr)
181 max_jobs = suite.tests.nr;
182
183 fprintf(stderr, "Running %d tests (%d at a time)\n",
184 suite.tests.nr, max_jobs);
185
186 ret = run_processes_parallel(max_jobs, next_test, test_failed,
187 test_finished, &suite);
188
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);
194 }
195
196 string_list_clear(&suite.tests, 0);
197 string_list_clear(&suite.failed, 0);
198
199 return !!ret;
200}
201
ad155925
GS
202static uint64_t my_random_next = 1234;
203
204static uint64_t my_random(void)
205{
206 uint64_t res = my_random_next;
207 my_random_next = my_random_next * 1103515245 + 12345;
208 return res;
209}
210
211static int quote_stress_test(int argc, const char **argv)
212{
213 /*
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.
218 */
219 char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
379e51d1 220 int i, j, k, trials = 100, skip = 0, msys2 = 0;
ad155925 221 struct strbuf out = STRBUF_INIT;
c972bf4c 222 struct strvec args = STRVEC_INIT;
ad155925
GS
223 struct option options[] = {
224 OPT_INTEGER('n', "trials", &trials, "Number of trials"),
7530a628 225 OPT_INTEGER('s', "skip", &skip, "Skip <n> trials"),
379e51d1 226 OPT_BOOL('m', "msys2", &msys2, "Test quoting for MSYS2's sh"),
ad155925
GS
227 OPT_END()
228 };
229 const char * const usage[] = {
14af7ed5 230 "test-tool run-command quote-stress-test <options>",
ad155925
GS
231 NULL
232 };
233
234 argc = parse_options(argc, argv, NULL, options, usage, 0);
235
379e51d1
JS
236 setenv("MSYS_NO_PATHCONV", "1", 0);
237
ad155925
GS
238 for (i = 0; i < trials; i++) {
239 struct child_process cp = CHILD_PROCESS_INIT;
55953c77 240 size_t arg_count, arg_offset;
ad155925
GS
241 int ret = 0;
242
c972bf4c 243 strvec_clear(&args);
379e51d1 244 if (msys2)
c972bf4c 245 strvec_pushl(&args, "sh", "-c",
f6d8942b 246 "printf %s\\\\0 \"$@\"", "skip", NULL);
379e51d1 247 else
c972bf4c 248 strvec_pushl(&args, "test-tool", "run-command",
f6d8942b 249 "quote-echo", NULL);
d70a9eb6 250 arg_offset = args.nr;
55953c77
JS
251
252 if (argc > 0) {
253 trials = 1;
254 arg_count = argc;
255 for (j = 0; j < arg_count; j++)
c972bf4c 256 strvec_push(&args, argv[j]);
55953c77
JS
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));
264
265 for (k = 0; k < arg_len; k++)
266 buf[k] = special[my_random() %
267 ARRAY_SIZE(special)];
268 buf[arg_len] = '\0';
269
c972bf4c 270 strvec_push(&args, buf);
55953c77 271 }
ad155925
GS
272 }
273
7530a628
JS
274 if (i < skip)
275 continue;
276
87ee87dd 277 strvec_pushv(&cp.args, args.v);
ad155925
GS
278 strbuf_reset(&out);
279 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
280 return error("Failed to spawn child process");
281
282 for (j = 0, k = 0; j < arg_count; j++) {
d70a9eb6 283 const char *arg = args.v[j + arg_offset];
ad155925
GS
284
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;
290 }
291
292 if (k != out.len)
293 ret = error("got %d bytes, but consumed only %d",
294 (int)out.len, (int)k);
295
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",
d70a9eb6 300 (int)j, args.v[j + arg_offset]);
ad155925
GS
301
302 strbuf_release(&out);
c972bf4c 303 strvec_clear(&args);
ad155925
GS
304
305 return ret;
306 }
307
308 if (i && (i % 100) == 0)
309 fprintf(stderr, "Trials completed: %d\n", (int)i);
310 }
311
312 strbuf_release(&out);
c972bf4c 313 strvec_clear(&args);
ad155925
GS
314
315 return 0;
316}
317
318static int quote_echo(int argc, const char **argv)
319{
320 while (argc > 1) {
321 fwrite(argv[1], strlen(argv[1]), 1, stdout);
322 fputc('\0', stdout);
323 argv++;
324 argc--;
325 }
326
327 return 0;
328}
329
eea4a7f4
JS
330static int inherit_handle(const char *argv0)
331{
332 struct child_process cp = CHILD_PROCESS_INIT;
333 char path[PATH_MAX];
334 int tmp;
335
336 /* First, open an inheritable handle */
337 xsnprintf(path, sizeof(path), "out-XXXXXX");
338 tmp = xmkstemp(path);
339
c972bf4c 340 strvec_pushl(&cp.args,
f6d8942b 341 "test-tool", argv0, "inherited-handle-child", NULL);
eea4a7f4
JS
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");
346
347 /* Then close it, and try to delete it. */
348 close(tmp);
349 if (unlink(path))
350 die("Could not delete '%s'", path);
351
352 if (close(cp.in) < 0 || finish_command(&cp) < 0)
353 die("Child did not finish");
354
355 return 0;
356}
357
358static int inherit_handle_child(void)
359{
360 struct strbuf buf = STRBUF_INIT;
361
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);
366
367 return 0;
368}
369
ae6a51f5 370int cmd__run_command(int argc, const char **argv)
2b541bf8 371{
d3180279 372 struct child_process proc = CHILD_PROCESS_INIT;
c553c72e 373 int jobs;
2b541bf8 374
be5d88e1
JS
375 if (argc > 1 && !strcmp(argv[1], "testsuite"))
376 exit(testsuite(argc - 1, argv + 1));
eea4a7f4
JS
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());
be5d88e1 381
ad155925
GS
382 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
383 return !!quote_stress_test(argc - 1, argv + 1);
384
385 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
386 return !!quote_echo(argc - 1, argv + 1);
387
c61a975d
NTND
388 if (argc < 3)
389 return 1;
390 while (!strcmp(argv[1], "env")) {
391 if (!argv[2])
392 die("env specifier without a value");
c972bf4c 393 strvec_push(&proc.env_array, argv[2]);
c61a975d
NTND
394 argv += 2;
395 argc -= 2;
396 }
2b541bf8
JS
397 if (argc < 3)
398 return 1;
87ee87dd 399 strvec_pushv(&proc.args, (const char **)argv + 2);
2b541bf8
JS
400
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;
406 }
c0f19bf3
JN
407 if (!strcmp(argv[1], "run-command"))
408 exit(run_command(&proc));
2b541bf8 409
c553c72e 410 jobs = atoi(argv[2]);
87ee87dd
ÆAB
411 strvec_clear(&proc.args);
412 strvec_pushv(&proc.args, (const char **)argv + 3);
c553c72e
SB
413
414 if (!strcmp(argv[1], "run-command-parallel"))
415 exit(run_processes_parallel(jobs, parallel_next,
416 NULL, NULL, &proc));
417
418 if (!strcmp(argv[1], "run-command-abort"))
419 exit(run_processes_parallel(jobs, parallel_next,
420 NULL, task_finished, &proc));
421
422 if (!strcmp(argv[1], "run-command-no-jobs"))
423 exit(run_processes_parallel(jobs, no_job,
424 NULL, task_finished, &proc));
425
2b541bf8
JS
426 fprintf(stderr, "check usage\n");
427 return 1;
428}