]>
Commit | Line | Data |
---|---|---|
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 |
23 | static int number_callbacks; |
24 | static 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); |
02962d36 | 34 | strbuf_addstr(err, "preloaded output of a child\n"); |
c553c72e SB |
35 | number_callbacks++; |
36 | return 1; | |
37 | } | |
38 | ||
39 | static int no_job(struct child_process *cp, | |
40 | struct strbuf *err, | |
41 | void *cb, | |
42 | void **task_cb) | |
43 | { | |
02962d36 | 44 | strbuf_addstr(err, "no further jobs available\n"); |
c553c72e SB |
45 | return 0; |
46 | } | |
47 | ||
48 | static int task_finished(int result, | |
c553c72e SB |
49 | struct strbuf *err, |
50 | void *pp_cb, | |
51 | void *pp_task_cb) | |
52 | { | |
02962d36 | 53 | strbuf_addstr(err, "asking for a quick stop\n"); |
c553c72e SB |
54 | return 1; |
55 | } | |
56 | ||
be5d88e1 JS |
57 | struct testsuite { |
58 | struct string_list tests, failed; | |
59 | int next; | |
60 | int quiet, immediate, verbose, verbose_log, trace, write_junit_xml; | |
61 | }; | |
f69a6e4f ÆAB |
62 | #define TESTSUITE_INIT { \ |
63 | .tests = STRING_LIST_INIT_DUP, \ | |
64 | .failed = STRING_LIST_INIT_DUP, \ | |
65 | } | |
be5d88e1 JS |
66 | |
67 | static int next_test(struct child_process *cp, struct strbuf *err, void *cb, | |
68 | void **task_cb) | |
69 | { | |
70 | struct testsuite *suite = cb; | |
71 | const char *test; | |
72 | if (suite->next >= suite->tests.nr) | |
73 | return 0; | |
74 | ||
75 | test = suite->tests.items[suite->next++].string; | |
c972bf4c | 76 | strvec_pushl(&cp->args, "sh", test, NULL); |
be5d88e1 | 77 | if (suite->quiet) |
c972bf4c | 78 | strvec_push(&cp->args, "--quiet"); |
be5d88e1 | 79 | if (suite->immediate) |
c972bf4c | 80 | strvec_push(&cp->args, "-i"); |
be5d88e1 | 81 | if (suite->verbose) |
c972bf4c | 82 | strvec_push(&cp->args, "-v"); |
be5d88e1 | 83 | if (suite->verbose_log) |
c972bf4c | 84 | strvec_push(&cp->args, "-V"); |
be5d88e1 | 85 | if (suite->trace) |
c972bf4c | 86 | strvec_push(&cp->args, "-x"); |
be5d88e1 | 87 | if (suite->write_junit_xml) |
c972bf4c | 88 | strvec_push(&cp->args, "--write-junit-xml"); |
be5d88e1 JS |
89 | |
90 | strbuf_addf(err, "Output of '%s':\n", test); | |
91 | *task_cb = (void *)test; | |
92 | ||
93 | return 1; | |
94 | } | |
95 | ||
96 | static int test_finished(int result, struct strbuf *err, void *cb, | |
97 | void *task_cb) | |
98 | { | |
99 | struct testsuite *suite = cb; | |
100 | const char *name = (const char *)task_cb; | |
101 | ||
102 | if (result) | |
103 | string_list_append(&suite->failed, name); | |
104 | ||
105 | strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name); | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
110 | static int test_failed(struct strbuf *out, void *cb, void *task_cb) | |
111 | { | |
112 | struct testsuite *suite = cb; | |
113 | const char *name = (const char *)task_cb; | |
114 | ||
115 | string_list_append(&suite->failed, name); | |
116 | strbuf_addf(out, "FAILED TO START: '%s'\n", name); | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
121 | static const char * const testsuite_usage[] = { | |
122 | "test-run-command testsuite [<options>] [<pattern>...]", | |
123 | NULL | |
124 | }; | |
125 | ||
126 | static int testsuite(int argc, const char **argv) | |
127 | { | |
128 | struct testsuite suite = TESTSUITE_INIT; | |
129 | int max_jobs = 1, i, ret; | |
130 | DIR *dir; | |
131 | struct dirent *d; | |
132 | struct option options[] = { | |
133 | OPT_BOOL('i', "immediate", &suite.immediate, | |
134 | "stop at first failed test case(s)"), | |
135 | OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"), | |
136 | OPT_BOOL('q', "quiet", &suite.quiet, "be terse"), | |
137 | OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"), | |
138 | OPT_BOOL('V', "verbose-log", &suite.verbose_log, | |
139 | "be verbose, redirected to a file"), | |
140 | OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"), | |
141 | OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml, | |
142 | "write JUnit-style XML files"), | |
143 | OPT_END() | |
144 | }; | |
145 | ||
be5d88e1 JS |
146 | argc = parse_options(argc, argv, NULL, options, |
147 | testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION); | |
148 | ||
149 | if (max_jobs <= 0) | |
150 | max_jobs = online_cpus(); | |
151 | ||
152 | dir = opendir("."); | |
153 | if (!dir) | |
154 | die("Could not open the current directory"); | |
155 | while ((d = readdir(dir))) { | |
156 | const char *p = d->d_name; | |
157 | ||
158 | if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) || | |
159 | !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' || | |
160 | !ends_with(p, ".sh")) | |
161 | continue; | |
162 | ||
163 | /* No pattern: match all */ | |
164 | if (!argc) { | |
165 | string_list_append(&suite.tests, p); | |
166 | continue; | |
167 | } | |
168 | ||
169 | for (i = 0; i < argc; i++) | |
170 | if (!wildmatch(argv[i], p, 0)) { | |
171 | string_list_append(&suite.tests, p); | |
172 | break; | |
173 | } | |
174 | } | |
175 | closedir(dir); | |
176 | ||
177 | if (!suite.tests.nr) | |
178 | die("No tests match!"); | |
179 | if (max_jobs > suite.tests.nr) | |
180 | max_jobs = suite.tests.nr; | |
181 | ||
99d60545 ÆAB |
182 | fprintf(stderr, "Running %"PRIuMAX" tests (%d at a time)\n", |
183 | (uintmax_t)suite.tests.nr, max_jobs); | |
be5d88e1 JS |
184 | |
185 | ret = run_processes_parallel(max_jobs, next_test, test_failed, | |
186 | test_finished, &suite); | |
187 | ||
188 | if (suite.failed.nr > 0) { | |
189 | ret = 1; | |
99d60545 ÆAB |
190 | fprintf(stderr, "%"PRIuMAX" tests failed:\n\n", |
191 | (uintmax_t)suite.failed.nr); | |
be5d88e1 JS |
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 |
202 | static uint64_t my_random_next = 1234; |
203 | ||
204 | static 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 | ||
211 | static 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 | 223 | struct option options[] = { |
9e1f22c8 AC |
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"), | |
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 | ||
318 | static 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 |
330 | static 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 | ||
358 | static 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 | 370 | int 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 | } |