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
;
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"),
156 argc
= parse_options(argc
, argv
, NULL
, options
,
157 testsuite_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
160 max_jobs
= online_cpus();
164 die("Could not open the current directory");
165 while ((d
= readdir(dir
))) {
166 const char *p
= d
->d_name
;
168 if (*p
!= 't' || !isdigit(p
[1]) || !isdigit(p
[2]) ||
169 !isdigit(p
[3]) || !isdigit(p
[4]) || p
[5] != '-' ||
170 !ends_with(p
, ".sh"))
173 /* No pattern: match all */
175 string_list_append(&suite
.tests
, p
);
179 for (i
= 0; i
< argc
; i
++)
180 if (!wildmatch(argv
[i
], p
, 0)) {
181 string_list_append(&suite
.tests
, p
);
188 die("No tests match!");
189 if (max_jobs
> suite
.tests
.nr
)
190 max_jobs
= suite
.tests
.nr
;
192 fprintf(stderr
, "Running %"PRIuMAX
" tests (%d at a time)\n",
193 (uintmax_t)suite
.tests
.nr
, max_jobs
);
195 ret
= run_processes_parallel(max_jobs
, next_test
, test_failed
,
196 test_finished
, &suite
);
198 if (suite
.failed
.nr
> 0) {
200 fprintf(stderr
, "%"PRIuMAX
" tests failed:\n\n",
201 (uintmax_t)suite
.failed
.nr
);
202 for (i
= 0; i
< suite
.failed
.nr
; i
++)
203 fprintf(stderr
, "\t%s\n", suite
.failed
.items
[i
].string
);
206 string_list_clear(&suite
.tests
, 0);
207 string_list_clear(&suite
.failed
, 0);
212 static uint64_t my_random_next
= 1234;
214 static uint64_t my_random(void)
216 uint64_t res
= my_random_next
;
217 my_random_next
= my_random_next
* 1103515245 + 12345;
221 static int quote_stress_test(int argc
, const char **argv
)
224 * We are running a quote-stress test.
225 * spawn a subprocess that runs quote-stress with a
226 * special option that echoes back the arguments that
229 char special
[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
230 int i
, j
, k
, trials
= 100, skip
= 0, msys2
= 0;
231 struct strbuf out
= STRBUF_INIT
;
232 struct strvec args
= STRVEC_INIT
;
233 struct option options
[] = {
234 OPT_INTEGER('n', "trials", &trials
, "number of trials"),
235 OPT_INTEGER('s', "skip", &skip
, "skip <n> trials"),
236 OPT_BOOL('m', "msys2", &msys2
, "test quoting for MSYS2's sh"),
239 const char * const usage
[] = {
240 "test-tool run-command quote-stress-test <options>",
244 argc
= parse_options(argc
, argv
, NULL
, options
, usage
, 0);
246 setenv("MSYS_NO_PATHCONV", "1", 0);
248 for (i
= 0; i
< trials
; i
++) {
249 struct child_process cp
= CHILD_PROCESS_INIT
;
250 size_t arg_count
, arg_offset
;
255 strvec_pushl(&args
, "sh", "-c",
256 "printf %s\\\\0 \"$@\"", "skip", NULL
);
258 strvec_pushl(&args
, "test-tool", "run-command",
260 arg_offset
= args
.nr
;
265 for (j
= 0; j
< arg_count
; j
++)
266 strvec_push(&args
, argv
[j
]);
268 arg_count
= 1 + (my_random() % 5);
269 for (j
= 0; j
< arg_count
; j
++) {
272 size_t arg_len
= min_len
+
273 (my_random() % (ARRAY_SIZE(buf
) - min_len
));
275 for (k
= 0; k
< arg_len
; k
++)
276 buf
[k
] = special
[my_random() %
277 ARRAY_SIZE(special
)];
280 strvec_push(&args
, buf
);
287 strvec_pushv(&cp
.args
, args
.v
);
289 if (pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0) < 0)
290 return error("Failed to spawn child process");
292 for (j
= 0, k
= 0; j
< arg_count
; j
++) {
293 const char *arg
= args
.v
[j
+ arg_offset
];
295 if (strcmp(arg
, out
.buf
+ k
))
296 ret
= error("incorrectly quoted arg: '%s', "
297 "echoed back as '%s'",
299 k
+= strlen(out
.buf
+ k
) + 1;
303 ret
= error("got %d bytes, but consumed only %d",
304 (int)out
.len
, (int)k
);
307 fprintf(stderr
, "Trial #%d failed. Arguments:\n", i
);
308 for (j
= 0; j
< arg_count
; j
++)
309 fprintf(stderr
, "arg #%d: '%s'\n",
310 (int)j
, args
.v
[j
+ arg_offset
]);
312 strbuf_release(&out
);
318 if (i
&& (i
% 100) == 0)
319 fprintf(stderr
, "Trials completed: %d\n", (int)i
);
322 strbuf_release(&out
);
328 static int quote_echo(int argc
, const char **argv
)
331 fwrite(argv
[1], strlen(argv
[1]), 1, stdout
);
340 static int inherit_handle(const char *argv0
)
342 struct child_process cp
= CHILD_PROCESS_INIT
;
346 /* First, open an inheritable handle */
347 xsnprintf(path
, sizeof(path
), "out-XXXXXX");
348 tmp
= xmkstemp(path
);
350 strvec_pushl(&cp
.args
,
351 "test-tool", argv0
, "inherited-handle-child", NULL
);
353 cp
.no_stdout
= cp
.no_stderr
= 1;
354 if (start_command(&cp
) < 0)
355 die("Could not start child process");
357 /* Then close it, and try to delete it. */
360 die("Could not delete '%s'", path
);
362 if (close(cp
.in
) < 0 || finish_command(&cp
) < 0)
363 die("Child did not finish");
368 static int inherit_handle_child(void)
370 struct strbuf buf
= STRBUF_INIT
;
372 if (strbuf_read(&buf
, 0, 0) < 0)
373 die("Could not read stdin");
374 printf("Received %s\n", buf
.buf
);
375 strbuf_release(&buf
);
380 int cmd__run_command(int argc
, const char **argv
)
382 struct child_process proc
= CHILD_PROCESS_INIT
;
385 if (argc
> 1 && !strcmp(argv
[1], "testsuite"))
386 exit(testsuite(argc
- 1, argv
+ 1));
387 if (!strcmp(argv
[1], "inherited-handle"))
388 exit(inherit_handle(argv
[0]));
389 if (!strcmp(argv
[1], "inherited-handle-child"))
390 exit(inherit_handle_child());
392 if (argc
>= 2 && !strcmp(argv
[1], "quote-stress-test"))
393 return !!quote_stress_test(argc
- 1, argv
+ 1);
395 if (argc
>= 2 && !strcmp(argv
[1], "quote-echo"))
396 return !!quote_echo(argc
- 1, argv
+ 1);
400 while (!strcmp(argv
[1], "env")) {
402 die("env specifier without a value");
403 strvec_push(&proc
.env
, argv
[2]);
409 strvec_pushv(&proc
.args
, (const char **)argv
+ 2);
411 if (!strcmp(argv
[1], "start-command-ENOENT")) {
412 if (start_command(&proc
) < 0 && errno
== ENOENT
)
414 fprintf(stderr
, "FAIL %s\n", argv
[1]);
417 if (!strcmp(argv
[1], "run-command"))
418 exit(run_command(&proc
));
420 if (!strcmp(argv
[1], "--ungroup")) {
423 run_processes_parallel_ungroup
= 1;
426 jobs
= atoi(argv
[2]);
427 strvec_clear(&proc
.args
);
428 strvec_pushv(&proc
.args
, (const char **)argv
+ 3);
430 if (!strcmp(argv
[1], "run-command-parallel"))
431 exit(run_processes_parallel(jobs
, parallel_next
,
434 if (!strcmp(argv
[1], "run-command-abort"))
435 exit(run_processes_parallel(jobs
, parallel_next
,
436 NULL
, task_finished
, &proc
));
438 if (!strcmp(argv
[1], "run-command-no-jobs"))
439 exit(run_processes_parallel(jobs
, no_job
,
440 NULL
, task_finished
, &proc
));
442 fprintf(stderr
, "check usage\n");