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
;
69 #define TESTSUITE_INIT { \
70 .tests = STRING_LIST_INIT_DUP, \
71 .failed = STRING_LIST_INIT_DUP, \
74 static int next_test(struct child_process
*cp
, struct strbuf
*err
, void *cb
,
77 struct testsuite
*suite
= cb
;
79 if (suite
->next
>= suite
->tests
.nr
)
82 test
= suite
->tests
.items
[suite
->next
++].string
;
83 strvec_pushl(&cp
->args
, "sh", test
, NULL
);
85 strvec_push(&cp
->args
, "--quiet");
87 strvec_push(&cp
->args
, "-i");
89 strvec_push(&cp
->args
, "-v");
90 if (suite
->verbose_log
)
91 strvec_push(&cp
->args
, "-V");
93 strvec_push(&cp
->args
, "-x");
94 if (suite
->write_junit_xml
)
95 strvec_push(&cp
->args
, "--write-junit-xml");
97 strbuf_addf(err
, "Output of '%s':\n", test
);
98 *task_cb
= (void *)test
;
103 static int test_finished(int result
, struct strbuf
*err
, void *cb
,
106 struct testsuite
*suite
= cb
;
107 const char *name
= (const char *)task_cb
;
110 string_list_append(&suite
->failed
, name
);
112 strbuf_addf(err
, "%s: '%s'\n", result
? "FAIL" : "SUCCESS", name
);
117 static int test_failed(struct strbuf
*out
, void *cb
, void *task_cb
)
119 struct testsuite
*suite
= cb
;
120 const char *name
= (const char *)task_cb
;
122 string_list_append(&suite
->failed
, name
);
123 strbuf_addf(out
, "FAILED TO START: '%s'\n", name
);
128 static const char * const testsuite_usage
[] = {
129 "test-run-command testsuite [<options>] [<pattern>...]",
133 static int testsuite(int argc
, const char **argv
)
135 struct testsuite suite
= TESTSUITE_INIT
;
136 int max_jobs
= 1, i
, ret
= 0;
139 struct option options
[] = {
140 OPT_BOOL('i', "immediate", &suite
.immediate
,
141 "stop at first failed test case(s)"),
142 OPT_INTEGER('j', "jobs", &max_jobs
, "run <N> jobs in parallel"),
143 OPT_BOOL('q', "quiet", &suite
.quiet
, "be terse"),
144 OPT_BOOL('v', "verbose", &suite
.verbose
, "be verbose"),
145 OPT_BOOL('V', "verbose-log", &suite
.verbose_log
,
146 "be verbose, redirected to a file"),
147 OPT_BOOL('x', "trace", &suite
.trace
, "trace shell commands"),
148 OPT_BOOL(0, "write-junit-xml", &suite
.write_junit_xml
,
149 "write JUnit-style XML files"),
152 struct run_process_parallel_opts opts
= {
153 .get_next_task
= next_test
,
154 .start_failure
= test_failed
,
155 .task_finished
= test_finished
,
159 argc
= parse_options(argc
, argv
, NULL
, options
,
160 testsuite_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
163 max_jobs
= online_cpus();
167 die("Could not open the current directory");
168 while ((d
= readdir(dir
))) {
169 const char *p
= d
->d_name
;
171 if (*p
!= 't' || !isdigit(p
[1]) || !isdigit(p
[2]) ||
172 !isdigit(p
[3]) || !isdigit(p
[4]) || p
[5] != '-' ||
173 !ends_with(p
, ".sh"))
176 /* No pattern: match all */
178 string_list_append(&suite
.tests
, p
);
182 for (i
= 0; i
< argc
; i
++)
183 if (!wildmatch(argv
[i
], p
, 0)) {
184 string_list_append(&suite
.tests
, p
);
191 die("No tests match!");
192 if (max_jobs
> suite
.tests
.nr
)
193 max_jobs
= suite
.tests
.nr
;
195 fprintf(stderr
, "Running %"PRIuMAX
" tests (%d at a time)\n",
196 (uintmax_t)suite
.tests
.nr
, max_jobs
);
198 opts
.processes
= max_jobs
;
199 run_processes_parallel(&opts
);
201 if (suite
.failed
.nr
> 0) {
203 fprintf(stderr
, "%"PRIuMAX
" tests failed:\n\n",
204 (uintmax_t)suite
.failed
.nr
);
205 for (i
= 0; i
< suite
.failed
.nr
; i
++)
206 fprintf(stderr
, "\t%s\n", suite
.failed
.items
[i
].string
);
209 string_list_clear(&suite
.tests
, 0);
210 string_list_clear(&suite
.failed
, 0);
215 static uint64_t my_random_next
= 1234;
217 static uint64_t my_random(void)
219 uint64_t res
= my_random_next
;
220 my_random_next
= my_random_next
* 1103515245 + 12345;
224 static int quote_stress_test(int argc
, const char **argv
)
227 * We are running a quote-stress test.
228 * spawn a subprocess that runs quote-stress with a
229 * special option that echoes back the arguments that
232 char special
[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
233 int i
, j
, k
, trials
= 100, skip
= 0, msys2
= 0;
234 struct strbuf out
= STRBUF_INIT
;
235 struct strvec args
= STRVEC_INIT
;
236 struct option options
[] = {
237 OPT_INTEGER('n', "trials", &trials
, "number of trials"),
238 OPT_INTEGER('s', "skip", &skip
, "skip <n> trials"),
239 OPT_BOOL('m', "msys2", &msys2
, "test quoting for MSYS2's sh"),
242 const char * const usage
[] = {
243 "test-tool run-command quote-stress-test <options>",
247 argc
= parse_options(argc
, argv
, NULL
, options
, usage
, 0);
249 setenv("MSYS_NO_PATHCONV", "1", 0);
251 for (i
= 0; i
< trials
; i
++) {
252 struct child_process cp
= CHILD_PROCESS_INIT
;
253 size_t arg_count
, arg_offset
;
258 strvec_pushl(&args
, "sh", "-c",
259 "printf %s\\\\0 \"$@\"", "skip", NULL
);
261 strvec_pushl(&args
, "test-tool", "run-command",
263 arg_offset
= args
.nr
;
268 for (j
= 0; j
< arg_count
; j
++)
269 strvec_push(&args
, argv
[j
]);
271 arg_count
= 1 + (my_random() % 5);
272 for (j
= 0; j
< arg_count
; j
++) {
275 size_t arg_len
= min_len
+
276 (my_random() % (ARRAY_SIZE(buf
) - min_len
));
278 for (k
= 0; k
< arg_len
; k
++)
279 buf
[k
] = special
[my_random() %
280 ARRAY_SIZE(special
)];
283 strvec_push(&args
, buf
);
290 strvec_pushv(&cp
.args
, args
.v
);
292 if (pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0) < 0)
293 return error("Failed to spawn child process");
295 for (j
= 0, k
= 0; j
< arg_count
; j
++) {
296 const char *arg
= args
.v
[j
+ arg_offset
];
298 if (strcmp(arg
, out
.buf
+ k
))
299 ret
= error("incorrectly quoted arg: '%s', "
300 "echoed back as '%s'",
302 k
+= strlen(out
.buf
+ k
) + 1;
306 ret
= error("got %d bytes, but consumed only %d",
307 (int)out
.len
, (int)k
);
310 fprintf(stderr
, "Trial #%d failed. Arguments:\n", i
);
311 for (j
= 0; j
< arg_count
; j
++)
312 fprintf(stderr
, "arg #%d: '%s'\n",
313 (int)j
, args
.v
[j
+ arg_offset
]);
315 strbuf_release(&out
);
321 if (i
&& (i
% 100) == 0)
322 fprintf(stderr
, "Trials completed: %d\n", (int)i
);
325 strbuf_release(&out
);
331 static int quote_echo(int argc
, const char **argv
)
334 fwrite(argv
[1], strlen(argv
[1]), 1, stdout
);
343 static int inherit_handle(const char *argv0
)
345 struct child_process cp
= CHILD_PROCESS_INIT
;
349 /* First, open an inheritable handle */
350 xsnprintf(path
, sizeof(path
), "out-XXXXXX");
351 tmp
= xmkstemp(path
);
353 strvec_pushl(&cp
.args
,
354 "test-tool", argv0
, "inherited-handle-child", NULL
);
356 cp
.no_stdout
= cp
.no_stderr
= 1;
357 if (start_command(&cp
) < 0)
358 die("Could not start child process");
360 /* Then close it, and try to delete it. */
363 die("Could not delete '%s'", path
);
365 if (close(cp
.in
) < 0 || finish_command(&cp
) < 0)
366 die("Child did not finish");
371 static int inherit_handle_child(void)
373 struct strbuf buf
= STRBUF_INIT
;
375 if (strbuf_read(&buf
, 0, 0) < 0)
376 die("Could not read stdin");
377 printf("Received %s\n", buf
.buf
);
378 strbuf_release(&buf
);
383 int cmd__run_command(int argc
, const char **argv
)
385 struct child_process proc
= CHILD_PROCESS_INIT
;
388 struct run_process_parallel_opts opts
= {
392 if (argc
> 1 && !strcmp(argv
[1], "testsuite"))
393 return testsuite(argc
- 1, argv
+ 1);
394 if (!strcmp(argv
[1], "inherited-handle"))
395 return inherit_handle(argv
[0]);
396 if (!strcmp(argv
[1], "inherited-handle-child"))
397 return inherit_handle_child();
399 if (argc
>= 2 && !strcmp(argv
[1], "quote-stress-test"))
400 return !!quote_stress_test(argc
- 1, argv
+ 1);
402 if (argc
>= 2 && !strcmp(argv
[1], "quote-echo"))
403 return !!quote_echo(argc
- 1, argv
+ 1);
407 while (!strcmp(argv
[1], "env")) {
409 die("env specifier without a value");
410 strvec_push(&proc
.env
, argv
[2]);
418 strvec_pushv(&proc
.args
, (const char **)argv
+ 2);
420 if (!strcmp(argv
[1], "start-command-ENOENT")) {
421 if (start_command(&proc
) < 0 && errno
== ENOENT
) {
425 fprintf(stderr
, "FAIL %s\n", argv
[1]);
428 if (!strcmp(argv
[1], "run-command")) {
429 ret
= run_command(&proc
);
433 if (!strcmp(argv
[1], "--ungroup")) {
439 jobs
= atoi(argv
[2]);
440 strvec_clear(&proc
.args
);
441 strvec_pushv(&proc
.args
, (const char **)argv
+ 3);
443 if (!strcmp(argv
[1], "run-command-parallel")) {
444 opts
.get_next_task
= parallel_next
;
445 } else if (!strcmp(argv
[1], "run-command-abort")) {
446 opts
.get_next_task
= parallel_next
;
447 opts
.task_finished
= task_finished
;
448 } else if (!strcmp(argv
[1], "run-command-no-jobs")) {
449 opts
.get_next_task
= no_job
;
450 opts
.task_finished
= task_finished
;
453 fprintf(stderr
, "check usage\n");
456 opts
.processes
= jobs
;
457 run_processes_parallel(&opts
);
460 child_process_clear(&proc
);