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 "run-command.h"
15 #include "parse-options.h"
16 #include "string-list.h"
17 #include "thread-utils.h"
18 #include "wildmatch.h"
20 static int number_callbacks
;
21 static int parallel_next(struct child_process
*cp
,
24 void **task_cb UNUSED
)
26 struct child_process
*d
= cb
;
27 if (number_callbacks
>= 4)
30 strvec_pushv(&cp
->args
, d
->args
.v
);
32 strbuf_addstr(err
, "preloaded output of a child\n");
34 fprintf(stderr
, "preloaded output of a child\n");
40 static int no_job(struct child_process
*cp UNUSED
,
43 void **task_cb UNUSED
)
46 strbuf_addstr(err
, "no further jobs available\n");
48 fprintf(stderr
, "no further jobs available\n");
52 static int task_finished(int result UNUSED
,
55 void *pp_task_cb UNUSED
)
58 strbuf_addstr(err
, "asking for a quick stop\n");
60 fprintf(stderr
, "asking for a quick stop\n");
65 struct string_list tests
, failed
;
67 int quiet
, immediate
, verbose
, verbose_log
, trace
, write_junit_xml
;
68 const char *shell_path
;
70 #define TESTSUITE_INIT { \
71 .tests = STRING_LIST_INIT_DUP, \
72 .failed = STRING_LIST_INIT_DUP, \
75 static int next_test(struct child_process
*cp
, struct strbuf
*err
, void *cb
,
78 struct testsuite
*suite
= cb
;
80 if (suite
->next
>= suite
->tests
.nr
)
83 test
= suite
->tests
.items
[suite
->next
++].string
;
84 if (suite
->shell_path
)
85 strvec_push(&cp
->args
, suite
->shell_path
);
86 strvec_push(&cp
->args
, test
);
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
,
161 struct strbuf progpath
= STRBUF_INIT
;
162 size_t path_prefix_len
;
164 argc
= parse_options(argc
, argv
, NULL
, options
,
165 testsuite_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
168 max_jobs
= online_cpus();
171 * If we run without a shell, execute the programs directly from CWD.
173 suite
.shell_path
= getenv("TEST_SHELL_PATH");
174 if (!suite
.shell_path
)
175 strbuf_addstr(&progpath
, "./");
176 path_prefix_len
= progpath
.len
;
180 die("Could not open the current directory");
181 while ((d
= readdir(dir
))) {
182 const char *p
= d
->d_name
;
184 if (!strcmp(p
, ".") || !strcmp(p
, ".."))
187 /* No pattern: match all */
189 strbuf_setlen(&progpath
, path_prefix_len
);
190 strbuf_addstr(&progpath
, p
);
191 string_list_append(&suite
.tests
, progpath
.buf
);
195 for (i
= 0; i
< argc
; i
++)
196 if (!wildmatch(argv
[i
], p
, 0)) {
197 strbuf_setlen(&progpath
, path_prefix_len
);
198 strbuf_addstr(&progpath
, p
);
199 string_list_append(&suite
.tests
, progpath
.buf
);
206 die("No tests match!");
207 if (max_jobs
> suite
.tests
.nr
)
208 max_jobs
= suite
.tests
.nr
;
210 fprintf(stderr
, "Running %"PRIuMAX
" tests (%d at a time)\n",
211 (uintmax_t)suite
.tests
.nr
, max_jobs
);
213 opts
.processes
= max_jobs
;
214 run_processes_parallel(&opts
);
216 if (suite
.failed
.nr
> 0) {
218 fprintf(stderr
, "%"PRIuMAX
" tests failed:\n\n",
219 (uintmax_t)suite
.failed
.nr
);
220 for (i
= 0; i
< suite
.failed
.nr
; i
++)
221 fprintf(stderr
, "\t%s\n", suite
.failed
.items
[i
].string
);
224 string_list_clear(&suite
.tests
, 0);
225 string_list_clear(&suite
.failed
, 0);
226 strbuf_release(&progpath
);
231 static uint64_t my_random_next
= 1234;
233 static uint64_t my_random(void)
235 uint64_t res
= my_random_next
;
236 my_random_next
= my_random_next
* 1103515245 + 12345;
240 static int quote_stress_test(int argc
, const char **argv
)
243 * We are running a quote-stress test.
244 * spawn a subprocess that runs quote-stress with a
245 * special option that echoes back the arguments that
248 char special
[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
249 int i
, j
, k
, trials
= 100, skip
= 0, msys2
= 0;
250 struct strbuf out
= STRBUF_INIT
;
251 struct strvec args
= STRVEC_INIT
;
252 struct option options
[] = {
253 OPT_INTEGER('n', "trials", &trials
, "number of trials"),
254 OPT_INTEGER('s', "skip", &skip
, "skip <n> trials"),
255 OPT_BOOL('m', "msys2", &msys2
, "test quoting for MSYS2's sh"),
258 const char * const usage
[] = {
259 "test-tool run-command quote-stress-test <options>",
263 argc
= parse_options(argc
, argv
, NULL
, options
, usage
, 0);
265 setenv("MSYS_NO_PATHCONV", "1", 0);
267 for (i
= 0; i
< trials
; i
++) {
268 struct child_process cp
= CHILD_PROCESS_INIT
;
269 size_t arg_count
, arg_offset
;
274 strvec_pushl(&args
, "sh", "-c",
275 "printf %s\\\\0 \"$@\"", "skip", NULL
);
277 strvec_pushl(&args
, "test-tool", "run-command",
279 arg_offset
= args
.nr
;
284 for (j
= 0; j
< arg_count
; j
++)
285 strvec_push(&args
, argv
[j
]);
287 arg_count
= 1 + (my_random() % 5);
288 for (j
= 0; j
< arg_count
; j
++) {
291 size_t arg_len
= min_len
+
292 (my_random() % (ARRAY_SIZE(buf
) - min_len
));
294 for (k
= 0; k
< arg_len
; k
++)
295 buf
[k
] = special
[my_random() %
296 ARRAY_SIZE(special
)];
299 strvec_push(&args
, buf
);
306 strvec_pushv(&cp
.args
, args
.v
);
308 if (pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0) < 0)
309 return error("Failed to spawn child process");
311 for (j
= 0, k
= 0; j
< arg_count
; j
++) {
312 const char *arg
= args
.v
[j
+ arg_offset
];
314 if (strcmp(arg
, out
.buf
+ k
))
315 ret
= error("incorrectly quoted arg: '%s', "
316 "echoed back as '%s'",
318 k
+= strlen(out
.buf
+ k
) + 1;
322 ret
= error("got %d bytes, but consumed only %d",
323 (int)out
.len
, (int)k
);
326 fprintf(stderr
, "Trial #%d failed. Arguments:\n", i
);
327 for (j
= 0; j
< arg_count
; j
++)
328 fprintf(stderr
, "arg #%d: '%s'\n",
329 (int)j
, args
.v
[j
+ arg_offset
]);
331 strbuf_release(&out
);
337 if (i
&& (i
% 100) == 0)
338 fprintf(stderr
, "Trials completed: %d\n", (int)i
);
341 strbuf_release(&out
);
347 static int quote_echo(int argc
, const char **argv
)
350 fwrite(argv
[1], strlen(argv
[1]), 1, stdout
);
359 static int inherit_handle(const char *argv0
)
361 struct child_process cp
= CHILD_PROCESS_INIT
;
365 /* First, open an inheritable handle */
366 xsnprintf(path
, sizeof(path
), "out-XXXXXX");
367 tmp
= xmkstemp(path
);
369 strvec_pushl(&cp
.args
,
370 "test-tool", argv0
, "inherited-handle-child", NULL
);
372 cp
.no_stdout
= cp
.no_stderr
= 1;
373 if (start_command(&cp
) < 0)
374 die("Could not start child process");
376 /* Then close it, and try to delete it. */
379 die("Could not delete '%s'", path
);
381 if (close(cp
.in
) < 0 || finish_command(&cp
) < 0)
382 die("Child did not finish");
387 static int inherit_handle_child(void)
389 struct strbuf buf
= STRBUF_INIT
;
391 if (strbuf_read(&buf
, 0, 0) < 0)
392 die("Could not read stdin");
393 printf("Received %s\n", buf
.buf
);
394 strbuf_release(&buf
);
399 int cmd__run_command(int argc
, const char **argv
)
401 struct child_process proc
= CHILD_PROCESS_INIT
;
404 struct run_process_parallel_opts opts
= {
408 if (argc
> 1 && !strcmp(argv
[1], "testsuite"))
409 return testsuite(argc
- 1, argv
+ 1);
410 if (!strcmp(argv
[1], "inherited-handle"))
411 return inherit_handle(argv
[0]);
412 if (!strcmp(argv
[1], "inherited-handle-child"))
413 return inherit_handle_child();
415 if (argc
>= 2 && !strcmp(argv
[1], "quote-stress-test"))
416 return !!quote_stress_test(argc
- 1, argv
+ 1);
418 if (argc
>= 2 && !strcmp(argv
[1], "quote-echo"))
419 return !!quote_echo(argc
- 1, argv
+ 1);
423 while (!strcmp(argv
[1], "env")) {
425 die("env specifier without a value");
426 strvec_push(&proc
.env
, argv
[2]);
434 strvec_pushv(&proc
.args
, (const char **)argv
+ 2);
436 if (!strcmp(argv
[1], "start-command-ENOENT")) {
437 if (start_command(&proc
) < 0 && errno
== ENOENT
) {
441 fprintf(stderr
, "FAIL %s\n", argv
[1]);
444 if (!strcmp(argv
[1], "run-command")) {
445 ret
= run_command(&proc
);
449 if (!strcmp(argv
[1], "--ungroup")) {
455 jobs
= atoi(argv
[2]);
456 strvec_clear(&proc
.args
);
457 strvec_pushv(&proc
.args
, (const char **)argv
+ 3);
459 if (!strcmp(argv
[1], "run-command-parallel")) {
460 opts
.get_next_task
= parallel_next
;
461 } else if (!strcmp(argv
[1], "run-command-abort")) {
462 opts
.get_next_task
= parallel_next
;
463 opts
.task_finished
= task_finished
;
464 } else if (!strcmp(argv
[1], "run-command-no-jobs")) {
465 opts
.get_next_task
= no_job
;
466 opts
.task_finished
= task_finished
;
469 fprintf(stderr
, "check usage\n");
472 opts
.processes
= jobs
;
473 run_processes_parallel(&opts
);
476 child_process_clear(&proc
);