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 #define DISABLE_SIGN_COMPARE_WARNINGS
13 #include "test-tool.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"
22 static int number_callbacks
;
23 static int parallel_next(struct child_process
*cp
,
26 void **task_cb UNUSED
)
28 struct child_process
*d
= cb
;
29 if (number_callbacks
>= 4)
32 strvec_pushv(&cp
->args
, d
->args
.v
);
34 strbuf_addstr(err
, "preloaded output of a child\n");
36 fprintf(stderr
, "preloaded output of a child\n");
42 static int no_job(struct child_process
*cp UNUSED
,
45 void **task_cb UNUSED
)
48 strbuf_addstr(err
, "no further jobs available\n");
50 fprintf(stderr
, "no further jobs available\n");
54 static int task_finished(int result UNUSED
,
57 void *pp_task_cb UNUSED
)
60 strbuf_addstr(err
, "asking for a quick stop\n");
62 fprintf(stderr
, "asking for a quick stop\n");
67 struct string_list tests
, failed
;
69 int quiet
, immediate
, verbose
, verbose_log
, trace
, write_junit_xml
;
70 const char *shell_path
;
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 if (suite
->shell_path
)
87 strvec_push(&cp
->args
, suite
->shell_path
);
88 strvec_push(&cp
->args
, test
);
90 strvec_push(&cp
->args
, "--quiet");
92 strvec_push(&cp
->args
, "-i");
94 strvec_push(&cp
->args
, "-v");
95 if (suite
->verbose_log
)
96 strvec_push(&cp
->args
, "-V");
98 strvec_push(&cp
->args
, "-x");
99 if (suite
->write_junit_xml
)
100 strvec_push(&cp
->args
, "--write-junit-xml");
102 strbuf_addf(err
, "Output of '%s':\n", test
);
103 *task_cb
= (void *)test
;
108 static int test_finished(int result
, struct strbuf
*err
, void *cb
,
111 struct testsuite
*suite
= cb
;
112 const char *name
= (const char *)task_cb
;
115 string_list_append(&suite
->failed
, name
);
117 strbuf_addf(err
, "%s: '%s'\n", result
? "FAIL" : "SUCCESS", name
);
122 static int test_failed(struct strbuf
*out
, void *cb
, void *task_cb
)
124 struct testsuite
*suite
= cb
;
125 const char *name
= (const char *)task_cb
;
127 string_list_append(&suite
->failed
, name
);
128 strbuf_addf(out
, "FAILED TO START: '%s'\n", name
);
133 static const char * const testsuite_usage
[] = {
134 "test-run-command testsuite [<options>] [<pattern>...]",
138 static int testsuite(int argc
, const char **argv
)
140 struct testsuite suite
= TESTSUITE_INIT
;
141 int max_jobs
= 1, i
, ret
= 0;
144 struct option options
[] = {
145 OPT_BOOL('i', "immediate", &suite
.immediate
,
146 "stop at first failed test case(s)"),
147 OPT_INTEGER('j', "jobs", &max_jobs
, "run <N> jobs in parallel"),
148 OPT_BOOL('q', "quiet", &suite
.quiet
, "be terse"),
149 OPT_BOOL('v', "verbose", &suite
.verbose
, "be verbose"),
150 OPT_BOOL('V', "verbose-log", &suite
.verbose_log
,
151 "be verbose, redirected to a file"),
152 OPT_BOOL('x', "trace", &suite
.trace
, "trace shell commands"),
153 OPT_BOOL(0, "write-junit-xml", &suite
.write_junit_xml
,
154 "write JUnit-style XML files"),
157 struct run_process_parallel_opts opts
= {
158 .get_next_task
= next_test
,
159 .start_failure
= test_failed
,
160 .task_finished
= test_finished
,
163 struct strbuf progpath
= STRBUF_INIT
;
164 size_t path_prefix_len
;
166 argc
= parse_options(argc
, argv
, NULL
, options
,
167 testsuite_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
170 max_jobs
= online_cpus();
173 * If we run without a shell, execute the programs directly from CWD.
175 suite
.shell_path
= getenv("TEST_SHELL_PATH");
176 if (!suite
.shell_path
)
177 strbuf_addstr(&progpath
, "./");
178 path_prefix_len
= progpath
.len
;
182 die("Could not open the current directory");
183 while ((d
= readdir(dir
))) {
184 const char *p
= d
->d_name
;
186 if (!strcmp(p
, ".") || !strcmp(p
, ".."))
189 /* No pattern: match all */
191 strbuf_setlen(&progpath
, path_prefix_len
);
192 strbuf_addstr(&progpath
, p
);
193 string_list_append(&suite
.tests
, progpath
.buf
);
197 for (i
= 0; i
< argc
; i
++)
198 if (!wildmatch(argv
[i
], p
, 0)) {
199 strbuf_setlen(&progpath
, path_prefix_len
);
200 strbuf_addstr(&progpath
, p
);
201 string_list_append(&suite
.tests
, progpath
.buf
);
208 die("No tests match!");
209 if (max_jobs
> suite
.tests
.nr
)
210 max_jobs
= suite
.tests
.nr
;
212 fprintf(stderr
, "Running %"PRIuMAX
" tests (%d at a time)\n",
213 (uintmax_t)suite
.tests
.nr
, max_jobs
);
215 opts
.processes
= max_jobs
;
216 run_processes_parallel(&opts
);
218 if (suite
.failed
.nr
> 0) {
220 fprintf(stderr
, "%"PRIuMAX
" tests failed:\n\n",
221 (uintmax_t)suite
.failed
.nr
);
222 for (i
= 0; i
< suite
.failed
.nr
; i
++)
223 fprintf(stderr
, "\t%s\n", suite
.failed
.items
[i
].string
);
226 string_list_clear(&suite
.tests
, 0);
227 string_list_clear(&suite
.failed
, 0);
228 strbuf_release(&progpath
);
233 static uint64_t my_random_next
= 1234;
235 static uint64_t my_random(void)
237 uint64_t res
= my_random_next
;
238 my_random_next
= my_random_next
* 1103515245 + 12345;
242 static int quote_stress_test(int argc
, const char **argv
)
245 * We are running a quote-stress test.
246 * spawn a subprocess that runs quote-stress with a
247 * special option that echoes back the arguments that
250 char special
[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
251 int i
, j
, k
, trials
= 100, skip
= 0, msys2
= 0;
252 struct strbuf out
= STRBUF_INIT
;
253 struct strvec args
= STRVEC_INIT
;
254 struct option options
[] = {
255 OPT_INTEGER('n', "trials", &trials
, "number of trials"),
256 OPT_INTEGER('s', "skip", &skip
, "skip <n> trials"),
257 OPT_BOOL('m', "msys2", &msys2
, "test quoting for MSYS2's sh"),
260 const char * const usage
[] = {
261 "test-tool run-command quote-stress-test <options>",
265 argc
= parse_options(argc
, argv
, NULL
, options
, usage
, 0);
267 setenv("MSYS_NO_PATHCONV", "1", 0);
269 for (i
= 0; i
< trials
; i
++) {
270 struct child_process cp
= CHILD_PROCESS_INIT
;
271 size_t arg_count
, arg_offset
;
276 strvec_pushl(&args
, "sh", "-c",
277 "printf %s\\\\0 \"$@\"", "skip", NULL
);
279 strvec_pushl(&args
, "test-tool", "run-command",
281 arg_offset
= args
.nr
;
286 for (j
= 0; j
< arg_count
; j
++)
287 strvec_push(&args
, argv
[j
]);
289 arg_count
= 1 + (my_random() % 5);
290 for (j
= 0; j
< arg_count
; j
++) {
293 size_t arg_len
= min_len
+
294 (my_random() % (ARRAY_SIZE(buf
) - min_len
));
296 for (k
= 0; k
< arg_len
; k
++)
297 buf
[k
] = special
[my_random() %
298 ARRAY_SIZE(special
)];
301 strvec_push(&args
, buf
);
308 strvec_pushv(&cp
.args
, args
.v
);
310 if (pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0) < 0)
311 return error("Failed to spawn child process");
313 for (j
= 0, k
= 0; j
< arg_count
; j
++) {
314 const char *arg
= args
.v
[j
+ arg_offset
];
316 if (strcmp(arg
, out
.buf
+ k
))
317 ret
= error("incorrectly quoted arg: '%s', "
318 "echoed back as '%s'",
320 k
+= strlen(out
.buf
+ k
) + 1;
324 ret
= error("got %d bytes, but consumed only %d",
325 (int)out
.len
, (int)k
);
328 fprintf(stderr
, "Trial #%d failed. Arguments:\n", i
);
329 for (j
= 0; j
< arg_count
; j
++)
330 fprintf(stderr
, "arg #%d: '%s'\n",
331 (int)j
, args
.v
[j
+ arg_offset
]);
333 strbuf_release(&out
);
339 if (i
&& (i
% 100) == 0)
340 fprintf(stderr
, "Trials completed: %d\n", (int)i
);
343 strbuf_release(&out
);
349 static int quote_echo(int argc
, const char **argv
)
352 fwrite(argv
[1], strlen(argv
[1]), 1, stdout
);
361 static int inherit_handle(const char *argv0
)
363 struct child_process cp
= CHILD_PROCESS_INIT
;
367 /* First, open an inheritable handle */
368 xsnprintf(path
, sizeof(path
), "out-XXXXXX");
369 tmp
= xmkstemp(path
);
371 strvec_pushl(&cp
.args
,
372 "test-tool", argv0
, "inherited-handle-child", NULL
);
374 cp
.no_stdout
= cp
.no_stderr
= 1;
375 if (start_command(&cp
) < 0)
376 die("Could not start child process");
378 /* Then close it, and try to delete it. */
381 die("Could not delete '%s'", path
);
383 if (close(cp
.in
) < 0 || finish_command(&cp
) < 0)
384 die("Child did not finish");
389 static int inherit_handle_child(void)
391 struct strbuf buf
= STRBUF_INIT
;
393 if (strbuf_read(&buf
, 0, 0) < 0)
394 die("Could not read stdin");
395 printf("Received %s\n", buf
.buf
);
396 strbuf_release(&buf
);
401 int cmd__run_command(int argc
, const char **argv
)
403 struct child_process proc
= CHILD_PROCESS_INIT
;
406 struct run_process_parallel_opts opts
= {
410 if (argc
> 1 && !strcmp(argv
[1], "testsuite"))
411 return testsuite(argc
- 1, argv
+ 1);
412 if (!strcmp(argv
[1], "inherited-handle"))
413 return inherit_handle(argv
[0]);
414 if (!strcmp(argv
[1], "inherited-handle-child"))
415 return inherit_handle_child();
417 if (argc
>= 2 && !strcmp(argv
[1], "quote-stress-test"))
418 return !!quote_stress_test(argc
- 1, argv
+ 1);
420 if (argc
>= 2 && !strcmp(argv
[1], "quote-echo"))
421 return !!quote_echo(argc
- 1, argv
+ 1);
425 while (!strcmp(argv
[1], "env")) {
427 die("env specifier without a value");
428 strvec_push(&proc
.env
, argv
[2]);
436 strvec_pushv(&proc
.args
, (const char **)argv
+ 2);
438 if (!strcmp(argv
[1], "start-command-ENOENT")) {
439 if (start_command(&proc
) < 0 && errno
== ENOENT
) {
443 fprintf(stderr
, "FAIL %s\n", argv
[1]);
446 if (!strcmp(argv
[1], "run-command")) {
447 ret
= run_command(&proc
);
451 if (!strcmp(argv
[1], "--ungroup")) {
457 jobs
= atoi(argv
[2]);
458 strvec_clear(&proc
.args
);
459 strvec_pushv(&proc
.args
, (const char **)argv
+ 3);
461 if (!strcmp(argv
[1], "run-command-parallel")) {
462 opts
.get_next_task
= parallel_next
;
463 } else if (!strcmp(argv
[1], "run-command-abort")) {
464 opts
.get_next_task
= parallel_next
;
465 opts
.task_finished
= task_finished
;
466 } else if (!strcmp(argv
[1], "run-command-no-jobs")) {
467 opts
.get_next_task
= no_job
;
468 opts
.task_finished
= task_finished
;
471 fprintf(stderr
, "check usage\n");
474 opts
.processes
= jobs
;
475 run_processes_parallel(&opts
);
478 child_process_clear(&proc
);