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.
11 #include "test-tool.h"
12 #include "git-compat-util.h"
14 #include "run-command.h"
17 #include "parse-options.h"
18 #include "string-list.h"
19 #include "thread-utils.h"
20 #include "wildmatch.h"
23 static int number_callbacks
;
24 static int parallel_next(struct child_process
*cp
,
29 struct child_process
*d
= cb
;
30 if (number_callbacks
>= 4)
33 strvec_pushv(&cp
->args
, d
->args
.v
);
35 strbuf_addstr(err
, "preloaded output of a child\n");
37 fprintf(stderr
, "preloaded output of a child\n");
43 static int no_job(struct child_process
*cp
,
49 strbuf_addstr(err
, "no further jobs available\n");
51 fprintf(stderr
, "no further jobs available\n");
55 static int task_finished(int result
,
61 strbuf_addstr(err
, "asking for a quick stop\n");
63 fprintf(stderr
, "asking for a quick stop\n");
68 struct string_list tests
, failed
;
70 int quiet
, immediate
, verbose
, verbose_log
, trace
, write_junit_xml
;
72 #define TESTSUITE_INIT { \
73 .tests = STRING_LIST_INIT_DUP, \
74 .failed = STRING_LIST_INIT_DUP, \
77 static int next_test(struct child_process
*cp
, struct strbuf
*err
, void *cb
,
80 struct testsuite
*suite
= cb
;
82 if (suite
->next
>= suite
->tests
.nr
)
85 test
= suite
->tests
.items
[suite
->next
++].string
;
86 strvec_pushl(&cp
->args
, "sh", test
, NULL
);
88 strvec_push(&cp
->args
, "--quiet");
90 strvec_push(&cp
->args
, "-i");
92 strvec_push(&cp
->args
, "-v");
93 if (suite
->verbose_log
)
94 strvec_push(&cp
->args
, "-V");
96 strvec_push(&cp
->args
, "-x");
97 if (suite
->write_junit_xml
)
98 strvec_push(&cp
->args
, "--write-junit-xml");
100 strbuf_addf(err
, "Output of '%s':\n", test
);
101 *task_cb
= (void *)test
;
106 static int test_finished(int result
, struct strbuf
*err
, void *cb
,
109 struct testsuite
*suite
= cb
;
110 const char *name
= (const char *)task_cb
;
113 string_list_append(&suite
->failed
, name
);
115 strbuf_addf(err
, "%s: '%s'\n", result
? "FAIL" : "SUCCESS", name
);
120 static int test_failed(struct strbuf
*out
, void *cb
, void *task_cb
)
122 struct testsuite
*suite
= cb
;
123 const char *name
= (const char *)task_cb
;
125 string_list_append(&suite
->failed
, name
);
126 strbuf_addf(out
, "FAILED TO START: '%s'\n", name
);
131 static const char * const testsuite_usage
[] = {
132 "test-run-command testsuite [<options>] [<pattern>...]",
136 static int testsuite(int argc
, const char **argv
)
138 struct testsuite suite
= TESTSUITE_INIT
;
139 int max_jobs
= 1, i
, ret
= 0;
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"),
155 struct run_process_parallel_opts opts
= {
156 .get_next_task
= next_test
,
157 .start_failure
= test_failed
,
158 .task_finished
= test_finished
,
162 argc
= parse_options(argc
, argv
, NULL
, options
,
163 testsuite_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
166 max_jobs
= online_cpus();
170 die("Could not open the current directory");
171 while ((d
= readdir(dir
))) {
172 const char *p
= d
->d_name
;
174 if (*p
!= 't' || !isdigit(p
[1]) || !isdigit(p
[2]) ||
175 !isdigit(p
[3]) || !isdigit(p
[4]) || p
[5] != '-' ||
176 !ends_with(p
, ".sh"))
179 /* No pattern: match all */
181 string_list_append(&suite
.tests
, p
);
185 for (i
= 0; i
< argc
; i
++)
186 if (!wildmatch(argv
[i
], p
, 0)) {
187 string_list_append(&suite
.tests
, p
);
194 die("No tests match!");
195 if (max_jobs
> suite
.tests
.nr
)
196 max_jobs
= suite
.tests
.nr
;
198 fprintf(stderr
, "Running %"PRIuMAX
" tests (%d at a time)\n",
199 (uintmax_t)suite
.tests
.nr
, max_jobs
);
201 opts
.processes
= max_jobs
;
202 run_processes_parallel(&opts
);
204 if (suite
.failed
.nr
> 0) {
206 fprintf(stderr
, "%"PRIuMAX
" tests failed:\n\n",
207 (uintmax_t)suite
.failed
.nr
);
208 for (i
= 0; i
< suite
.failed
.nr
; i
++)
209 fprintf(stderr
, "\t%s\n", suite
.failed
.items
[i
].string
);
212 string_list_clear(&suite
.tests
, 0);
213 string_list_clear(&suite
.failed
, 0);
218 static uint64_t my_random_next
= 1234;
220 static uint64_t my_random(void)
222 uint64_t res
= my_random_next
;
223 my_random_next
= my_random_next
* 1103515245 + 12345;
227 static int quote_stress_test(int argc
, const char **argv
)
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
235 char special
[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
236 int i
, j
, k
, trials
= 100, skip
= 0, msys2
= 0;
237 struct strbuf out
= STRBUF_INIT
;
238 struct strvec args
= STRVEC_INIT
;
239 struct option options
[] = {
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"),
245 const char * const usage
[] = {
246 "test-tool run-command quote-stress-test <options>",
250 argc
= parse_options(argc
, argv
, NULL
, options
, usage
, 0);
252 setenv("MSYS_NO_PATHCONV", "1", 0);
254 for (i
= 0; i
< trials
; i
++) {
255 struct child_process cp
= CHILD_PROCESS_INIT
;
256 size_t arg_count
, arg_offset
;
261 strvec_pushl(&args
, "sh", "-c",
262 "printf %s\\\\0 \"$@\"", "skip", NULL
);
264 strvec_pushl(&args
, "test-tool", "run-command",
266 arg_offset
= args
.nr
;
271 for (j
= 0; j
< arg_count
; j
++)
272 strvec_push(&args
, argv
[j
]);
274 arg_count
= 1 + (my_random() % 5);
275 for (j
= 0; j
< arg_count
; j
++) {
278 size_t arg_len
= min_len
+
279 (my_random() % (ARRAY_SIZE(buf
) - min_len
));
281 for (k
= 0; k
< arg_len
; k
++)
282 buf
[k
] = special
[my_random() %
283 ARRAY_SIZE(special
)];
286 strvec_push(&args
, buf
);
293 strvec_pushv(&cp
.args
, args
.v
);
295 if (pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0) < 0)
296 return error("Failed to spawn child process");
298 for (j
= 0, k
= 0; j
< arg_count
; j
++) {
299 const char *arg
= args
.v
[j
+ arg_offset
];
301 if (strcmp(arg
, out
.buf
+ k
))
302 ret
= error("incorrectly quoted arg: '%s', "
303 "echoed back as '%s'",
305 k
+= strlen(out
.buf
+ k
) + 1;
309 ret
= error("got %d bytes, but consumed only %d",
310 (int)out
.len
, (int)k
);
313 fprintf(stderr
, "Trial #%d failed. Arguments:\n", i
);
314 for (j
= 0; j
< arg_count
; j
++)
315 fprintf(stderr
, "arg #%d: '%s'\n",
316 (int)j
, args
.v
[j
+ arg_offset
]);
318 strbuf_release(&out
);
324 if (i
&& (i
% 100) == 0)
325 fprintf(stderr
, "Trials completed: %d\n", (int)i
);
328 strbuf_release(&out
);
334 static int quote_echo(int argc
, const char **argv
)
337 fwrite(argv
[1], strlen(argv
[1]), 1, stdout
);
346 static int inherit_handle(const char *argv0
)
348 struct child_process cp
= CHILD_PROCESS_INIT
;
352 /* First, open an inheritable handle */
353 xsnprintf(path
, sizeof(path
), "out-XXXXXX");
354 tmp
= xmkstemp(path
);
356 strvec_pushl(&cp
.args
,
357 "test-tool", argv0
, "inherited-handle-child", NULL
);
359 cp
.no_stdout
= cp
.no_stderr
= 1;
360 if (start_command(&cp
) < 0)
361 die("Could not start child process");
363 /* Then close it, and try to delete it. */
366 die("Could not delete '%s'", path
);
368 if (close(cp
.in
) < 0 || finish_command(&cp
) < 0)
369 die("Child did not finish");
374 static int inherit_handle_child(void)
376 struct strbuf buf
= STRBUF_INIT
;
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
);
386 int cmd__run_command(int argc
, const char **argv
)
388 struct child_process proc
= CHILD_PROCESS_INIT
;
391 struct run_process_parallel_opts opts
= {
395 if (argc
> 1 && !strcmp(argv
[1], "testsuite"))
396 return testsuite(argc
- 1, argv
+ 1);
397 if (!strcmp(argv
[1], "inherited-handle"))
398 return inherit_handle(argv
[0]);
399 if (!strcmp(argv
[1], "inherited-handle-child"))
400 return inherit_handle_child();
402 if (argc
>= 2 && !strcmp(argv
[1], "quote-stress-test"))
403 return !!quote_stress_test(argc
- 1, argv
+ 1);
405 if (argc
>= 2 && !strcmp(argv
[1], "quote-echo"))
406 return !!quote_echo(argc
- 1, argv
+ 1);
410 while (!strcmp(argv
[1], "env")) {
412 die("env specifier without a value");
413 strvec_push(&proc
.env
, argv
[2]);
421 strvec_pushv(&proc
.args
, (const char **)argv
+ 2);
423 if (!strcmp(argv
[1], "start-command-ENOENT")) {
424 if (start_command(&proc
) < 0 && errno
== ENOENT
) {
428 fprintf(stderr
, "FAIL %s\n", argv
[1]);
431 if (!strcmp(argv
[1], "run-command")) {
432 ret
= run_command(&proc
);
436 if (!strcmp(argv
[1], "--ungroup")) {
442 jobs
= atoi(argv
[2]);
443 strvec_clear(&proc
.args
);
444 strvec_pushv(&proc
.args
, (const char **)argv
+ 3);
446 if (!strcmp(argv
[1], "run-command-parallel")) {
447 opts
.get_next_task
= parallel_next
;
448 } else if (!strcmp(argv
[1], "run-command-abort")) {
449 opts
.get_next_task
= parallel_next
;
450 opts
.task_finished
= task_finished
;
451 } else if (!strcmp(argv
[1], "run-command-no-jobs")) {
452 opts
.get_next_task
= no_job
;
453 opts
.task_finished
= task_finished
;
456 fprintf(stderr
, "check usage\n");
459 opts
.processes
= jobs
;
460 run_processes_parallel(&opts
);
463 child_process_clear(&proc
);