]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-run-command.c
packfile: delete .idx files before .pack files
[thirdparty/git.git] / t / helper / test-run-command.c
1 /*
2 * test-run-command.c: test run command API.
3 *
4 * (C) 2009 Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
5 *
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.
9 */
10
11 #include "test-tool.h"
12 #include "git-compat-util.h"
13 #include "cache.h"
14 #include "run-command.h"
15 #include "strvec.h"
16 #include "strbuf.h"
17 #include "parse-options.h"
18 #include "string-list.h"
19 #include "thread-utils.h"
20 #include "wildmatch.h"
21 #include "gettext.h"
22
23 static int number_callbacks;
24 static int parallel_next(struct child_process *cp,
25 struct strbuf *err,
26 void *cb,
27 void **task_cb)
28 {
29 struct child_process *d = cb;
30 if (number_callbacks >= 4)
31 return 0;
32
33 strvec_pushv(&cp->args, d->args.v);
34 if (err)
35 strbuf_addstr(err, "preloaded output of a child\n");
36 else
37 fprintf(stderr, "preloaded output of a child\n");
38
39 number_callbacks++;
40 return 1;
41 }
42
43 static int no_job(struct child_process *cp,
44 struct strbuf *err,
45 void *cb,
46 void **task_cb)
47 {
48 if (err)
49 strbuf_addstr(err, "no further jobs available\n");
50 else
51 fprintf(stderr, "no further jobs available\n");
52 return 0;
53 }
54
55 static int task_finished(int result,
56 struct strbuf *err,
57 void *pp_cb,
58 void *pp_task_cb)
59 {
60 if (err)
61 strbuf_addstr(err, "asking for a quick stop\n");
62 else
63 fprintf(stderr, "asking for a quick stop\n");
64 return 1;
65 }
66
67 struct testsuite {
68 struct string_list tests, failed;
69 int next;
70 int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
71 };
72 #define TESTSUITE_INIT { \
73 .tests = STRING_LIST_INIT_DUP, \
74 .failed = STRING_LIST_INIT_DUP, \
75 }
76
77 static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
78 void **task_cb)
79 {
80 struct testsuite *suite = cb;
81 const char *test;
82 if (suite->next >= suite->tests.nr)
83 return 0;
84
85 test = suite->tests.items[suite->next++].string;
86 strvec_pushl(&cp->args, "sh", test, NULL);
87 if (suite->quiet)
88 strvec_push(&cp->args, "--quiet");
89 if (suite->immediate)
90 strvec_push(&cp->args, "-i");
91 if (suite->verbose)
92 strvec_push(&cp->args, "-v");
93 if (suite->verbose_log)
94 strvec_push(&cp->args, "-V");
95 if (suite->trace)
96 strvec_push(&cp->args, "-x");
97 if (suite->write_junit_xml)
98 strvec_push(&cp->args, "--write-junit-xml");
99
100 strbuf_addf(err, "Output of '%s':\n", test);
101 *task_cb = (void *)test;
102
103 return 1;
104 }
105
106 static int test_finished(int result, struct strbuf *err, void *cb,
107 void *task_cb)
108 {
109 struct testsuite *suite = cb;
110 const char *name = (const char *)task_cb;
111
112 if (result)
113 string_list_append(&suite->failed, name);
114
115 strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
116
117 return 0;
118 }
119
120 static int test_failed(struct strbuf *out, void *cb, void *task_cb)
121 {
122 struct testsuite *suite = cb;
123 const char *name = (const char *)task_cb;
124
125 string_list_append(&suite->failed, name);
126 strbuf_addf(out, "FAILED TO START: '%s'\n", name);
127
128 return 0;
129 }
130
131 static const char * const testsuite_usage[] = {
132 "test-run-command testsuite [<options>] [<pattern>...]",
133 NULL
134 };
135
136 static int testsuite(int argc, const char **argv)
137 {
138 struct testsuite suite = TESTSUITE_INIT;
139 int max_jobs = 1, i, ret;
140 DIR *dir;
141 struct dirent *d;
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"),
153 OPT_END()
154 };
155
156 argc = parse_options(argc, argv, NULL, options,
157 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
158
159 if (max_jobs <= 0)
160 max_jobs = online_cpus();
161
162 dir = opendir(".");
163 if (!dir)
164 die("Could not open the current directory");
165 while ((d = readdir(dir))) {
166 const char *p = d->d_name;
167
168 if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
169 !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
170 !ends_with(p, ".sh"))
171 continue;
172
173 /* No pattern: match all */
174 if (!argc) {
175 string_list_append(&suite.tests, p);
176 continue;
177 }
178
179 for (i = 0; i < argc; i++)
180 if (!wildmatch(argv[i], p, 0)) {
181 string_list_append(&suite.tests, p);
182 break;
183 }
184 }
185 closedir(dir);
186
187 if (!suite.tests.nr)
188 die("No tests match!");
189 if (max_jobs > suite.tests.nr)
190 max_jobs = suite.tests.nr;
191
192 fprintf(stderr, "Running %"PRIuMAX" tests (%d at a time)\n",
193 (uintmax_t)suite.tests.nr, max_jobs);
194
195 ret = run_processes_parallel(max_jobs, next_test, test_failed,
196 test_finished, &suite);
197
198 if (suite.failed.nr > 0) {
199 ret = 1;
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);
204 }
205
206 string_list_clear(&suite.tests, 0);
207 string_list_clear(&suite.failed, 0);
208
209 return !!ret;
210 }
211
212 static uint64_t my_random_next = 1234;
213
214 static uint64_t my_random(void)
215 {
216 uint64_t res = my_random_next;
217 my_random_next = my_random_next * 1103515245 + 12345;
218 return res;
219 }
220
221 static int quote_stress_test(int argc, const char **argv)
222 {
223 /*
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
227 * were passed in.
228 */
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"),
237 OPT_END()
238 };
239 const char * const usage[] = {
240 "test-tool run-command quote-stress-test <options>",
241 NULL
242 };
243
244 argc = parse_options(argc, argv, NULL, options, usage, 0);
245
246 setenv("MSYS_NO_PATHCONV", "1", 0);
247
248 for (i = 0; i < trials; i++) {
249 struct child_process cp = CHILD_PROCESS_INIT;
250 size_t arg_count, arg_offset;
251 int ret = 0;
252
253 strvec_clear(&args);
254 if (msys2)
255 strvec_pushl(&args, "sh", "-c",
256 "printf %s\\\\0 \"$@\"", "skip", NULL);
257 else
258 strvec_pushl(&args, "test-tool", "run-command",
259 "quote-echo", NULL);
260 arg_offset = args.nr;
261
262 if (argc > 0) {
263 trials = 1;
264 arg_count = argc;
265 for (j = 0; j < arg_count; j++)
266 strvec_push(&args, argv[j]);
267 } else {
268 arg_count = 1 + (my_random() % 5);
269 for (j = 0; j < arg_count; j++) {
270 char buf[20];
271 size_t min_len = 1;
272 size_t arg_len = min_len +
273 (my_random() % (ARRAY_SIZE(buf) - min_len));
274
275 for (k = 0; k < arg_len; k++)
276 buf[k] = special[my_random() %
277 ARRAY_SIZE(special)];
278 buf[arg_len] = '\0';
279
280 strvec_push(&args, buf);
281 }
282 }
283
284 if (i < skip)
285 continue;
286
287 strvec_pushv(&cp.args, args.v);
288 strbuf_reset(&out);
289 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
290 return error("Failed to spawn child process");
291
292 for (j = 0, k = 0; j < arg_count; j++) {
293 const char *arg = args.v[j + arg_offset];
294
295 if (strcmp(arg, out.buf + k))
296 ret = error("incorrectly quoted arg: '%s', "
297 "echoed back as '%s'",
298 arg, out.buf + k);
299 k += strlen(out.buf + k) + 1;
300 }
301
302 if (k != out.len)
303 ret = error("got %d bytes, but consumed only %d",
304 (int)out.len, (int)k);
305
306 if (ret) {
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]);
311
312 strbuf_release(&out);
313 strvec_clear(&args);
314
315 return ret;
316 }
317
318 if (i && (i % 100) == 0)
319 fprintf(stderr, "Trials completed: %d\n", (int)i);
320 }
321
322 strbuf_release(&out);
323 strvec_clear(&args);
324
325 return 0;
326 }
327
328 static int quote_echo(int argc, const char **argv)
329 {
330 while (argc > 1) {
331 fwrite(argv[1], strlen(argv[1]), 1, stdout);
332 fputc('\0', stdout);
333 argv++;
334 argc--;
335 }
336
337 return 0;
338 }
339
340 static int inherit_handle(const char *argv0)
341 {
342 struct child_process cp = CHILD_PROCESS_INIT;
343 char path[PATH_MAX];
344 int tmp;
345
346 /* First, open an inheritable handle */
347 xsnprintf(path, sizeof(path), "out-XXXXXX");
348 tmp = xmkstemp(path);
349
350 strvec_pushl(&cp.args,
351 "test-tool", argv0, "inherited-handle-child", NULL);
352 cp.in = -1;
353 cp.no_stdout = cp.no_stderr = 1;
354 if (start_command(&cp) < 0)
355 die("Could not start child process");
356
357 /* Then close it, and try to delete it. */
358 close(tmp);
359 if (unlink(path))
360 die("Could not delete '%s'", path);
361
362 if (close(cp.in) < 0 || finish_command(&cp) < 0)
363 die("Child did not finish");
364
365 return 0;
366 }
367
368 static int inherit_handle_child(void)
369 {
370 struct strbuf buf = STRBUF_INIT;
371
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);
376
377 return 0;
378 }
379
380 int cmd__run_command(int argc, const char **argv)
381 {
382 struct child_process proc = CHILD_PROCESS_INIT;
383 int jobs;
384
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());
391
392 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
393 return !!quote_stress_test(argc - 1, argv + 1);
394
395 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
396 return !!quote_echo(argc - 1, argv + 1);
397
398 if (argc < 3)
399 return 1;
400 while (!strcmp(argv[1], "env")) {
401 if (!argv[2])
402 die("env specifier without a value");
403 strvec_push(&proc.env, argv[2]);
404 argv += 2;
405 argc -= 2;
406 }
407 if (argc < 3)
408 return 1;
409 strvec_pushv(&proc.args, (const char **)argv + 2);
410
411 if (!strcmp(argv[1], "start-command-ENOENT")) {
412 if (start_command(&proc) < 0 && errno == ENOENT)
413 return 0;
414 fprintf(stderr, "FAIL %s\n", argv[1]);
415 return 1;
416 }
417 if (!strcmp(argv[1], "run-command"))
418 exit(run_command(&proc));
419
420 if (!strcmp(argv[1], "--ungroup")) {
421 argv += 1;
422 argc -= 1;
423 run_processes_parallel_ungroup = 1;
424 }
425
426 jobs = atoi(argv[2]);
427 strvec_clear(&proc.args);
428 strvec_pushv(&proc.args, (const char **)argv + 3);
429
430 if (!strcmp(argv[1], "run-command-parallel"))
431 exit(run_processes_parallel(jobs, parallel_next,
432 NULL, NULL, &proc));
433
434 if (!strcmp(argv[1], "run-command-abort"))
435 exit(run_processes_parallel(jobs, parallel_next,
436 NULL, task_finished, &proc));
437
438 if (!strcmp(argv[1], "run-command-no-jobs"))
439 exit(run_processes_parallel(jobs, no_job,
440 NULL, task_finished, &proc));
441
442 fprintf(stderr, "check usage\n");
443 return 1;
444 }