]>
git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-run-command.c
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"
13 #include "run-command.h"
14 #include "argv-array.h"
17 #include "parse-options.h"
19 static int number_callbacks
;
20 static int parallel_next(struct child_process
*cp
,
25 struct child_process
*d
= cb
;
26 if (number_callbacks
>= 4)
29 argv_array_pushv(&cp
->args
, d
->argv
);
30 strbuf_addstr(err
, "preloaded output of a child\n");
35 static int no_job(struct child_process
*cp
,
40 strbuf_addstr(err
, "no further jobs available\n");
44 static int task_finished(int result
,
49 strbuf_addstr(err
, "asking for a quick stop\n");
53 static uint64_t my_random_next
= 1234;
55 static uint64_t my_random(void)
57 uint64_t res
= my_random_next
;
58 my_random_next
= my_random_next
* 1103515245 + 12345;
62 static int quote_stress_test(int argc
, const char **argv
)
65 * We are running a quote-stress test.
66 * spawn a subprocess that runs quote-stress with a
67 * special option that echoes back the arguments that
70 char special
[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
71 int i
, j
, k
, trials
= 100, skip
= 0, msys2
= 0;
72 struct strbuf out
= STRBUF_INIT
;
73 struct argv_array args
= ARGV_ARRAY_INIT
;
74 struct option options
[] = {
75 OPT_INTEGER('n', "trials", &trials
, "Number of trials"),
76 OPT_INTEGER('s', "skip", &skip
, "Skip <n> trials"),
77 OPT_BOOL('m', "msys2", &msys2
, "Test quoting for MSYS2's sh"),
80 const char * const usage
[] = {
81 "test-tool run-command quote-stress-test <options>",
85 argc
= parse_options(argc
, argv
, NULL
, options
, usage
, 0);
87 setenv("MSYS_NO_PATHCONV", "1", 0);
89 for (i
= 0; i
< trials
; i
++) {
90 struct child_process cp
= CHILD_PROCESS_INIT
;
91 size_t arg_count
, arg_offset
;
94 argv_array_clear(&args
);
96 argv_array_pushl(&args
, "sh", "-c",
97 "printf %s\\\\0 \"$@\"", "skip", NULL
);
99 argv_array_pushl(&args
, "test-tool", "run-command",
101 arg_offset
= args
.argc
;
106 for (j
= 0; j
< arg_count
; j
++)
107 argv_array_push(&args
, argv
[j
]);
109 arg_count
= 1 + (my_random() % 5);
110 for (j
= 0; j
< arg_count
; j
++) {
113 size_t arg_len
= min_len
+
114 (my_random() % (ARRAY_SIZE(buf
) - min_len
));
116 for (k
= 0; k
< arg_len
; k
++)
117 buf
[k
] = special
[my_random() %
118 ARRAY_SIZE(special
)];
121 argv_array_push(&args
, buf
);
130 if (pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0) < 0)
131 return error("Failed to spawn child process");
133 for (j
= 0, k
= 0; j
< arg_count
; j
++) {
134 const char *arg
= args
.argv
[j
+ arg_offset
];
136 if (strcmp(arg
, out
.buf
+ k
))
137 ret
= error("incorrectly quoted arg: '%s', "
138 "echoed back as '%s'",
140 k
+= strlen(out
.buf
+ k
) + 1;
144 ret
= error("got %d bytes, but consumed only %d",
145 (int)out
.len
, (int)k
);
148 fprintf(stderr
, "Trial #%d failed. Arguments:\n", i
);
149 for (j
= 0; j
< arg_count
; j
++)
150 fprintf(stderr
, "arg #%d: '%s'\n",
151 (int)j
, args
.argv
[j
+ arg_offset
]);
153 strbuf_release(&out
);
154 argv_array_clear(&args
);
159 if (i
&& (i
% 100) == 0)
160 fprintf(stderr
, "Trials completed: %d\n", (int)i
);
163 strbuf_release(&out
);
164 argv_array_clear(&args
);
169 static int quote_echo(int argc
, const char **argv
)
172 fwrite(argv
[1], strlen(argv
[1]), 1, stdout
);
181 int cmd__run_command(int argc
, const char **argv
)
183 struct child_process proc
= CHILD_PROCESS_INIT
;
186 if (argc
>= 2 && !strcmp(argv
[1], "quote-stress-test"))
187 return !!quote_stress_test(argc
- 1, argv
+ 1);
189 if (argc
>= 2 && !strcmp(argv
[1], "quote-echo"))
190 return !!quote_echo(argc
- 1, argv
+ 1);
194 while (!strcmp(argv
[1], "env")) {
196 die("env specifier without a value");
197 argv_array_push(&proc
.env_array
, argv
[2]);
203 proc
.argv
= (const char **)argv
+ 2;
205 if (!strcmp(argv
[1], "start-command-ENOENT")) {
206 if (start_command(&proc
) < 0 && errno
== ENOENT
)
208 fprintf(stderr
, "FAIL %s\n", argv
[1]);
211 if (!strcmp(argv
[1], "run-command"))
212 exit(run_command(&proc
));
214 jobs
= atoi(argv
[2]);
215 proc
.argv
= (const char **)argv
+ 3;
217 if (!strcmp(argv
[1], "run-command-parallel"))
218 exit(run_processes_parallel(jobs
, parallel_next
,
221 if (!strcmp(argv
[1], "run-command-abort"))
222 exit(run_processes_parallel(jobs
, parallel_next
,
223 NULL
, task_finished
, &proc
));
225 if (!strcmp(argv
[1], "run-command-no-jobs"))
226 exit(run_processes_parallel(jobs
, no_job
,
227 NULL
, task_finished
, &proc
));
229 fprintf(stderr
, "check usage\n");