]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-run-command.c
unicode: update the width tables to Unicode 15.1
[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 = 0;
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 struct run_process_parallel_opts opts = {
156 .get_next_task = next_test,
157 .start_failure = test_failed,
158 .task_finished = test_finished,
159 .data = &suite,
160 };
161
162 argc = parse_options(argc, argv, NULL, options,
163 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
164
165 if (max_jobs <= 0)
166 max_jobs = online_cpus();
167
168 dir = opendir(".");
169 if (!dir)
170 die("Could not open the current directory");
171 while ((d = readdir(dir))) {
172 const char *p = d->d_name;
173
174 if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
175 !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
176 !ends_with(p, ".sh"))
177 continue;
178
179 /* No pattern: match all */
180 if (!argc) {
181 string_list_append(&suite.tests, p);
182 continue;
183 }
184
185 for (i = 0; i < argc; i++)
186 if (!wildmatch(argv[i], p, 0)) {
187 string_list_append(&suite.tests, p);
188 break;
189 }
190 }
191 closedir(dir);
192
193 if (!suite.tests.nr)
194 die("No tests match!");
195 if (max_jobs > suite.tests.nr)
196 max_jobs = suite.tests.nr;
197
198 fprintf(stderr, "Running %"PRIuMAX" tests (%d at a time)\n",
199 (uintmax_t)suite.tests.nr, max_jobs);
200
201 opts.processes = max_jobs;
202 run_processes_parallel(&opts);
203
204 if (suite.failed.nr > 0) {
205 ret = 1;
206 fprintf(stderr, "%"PRIuMAX" tests failed:\n\n",
207 (uintmax_t)suite.failed.nr);
208 for (i = 0; i < suite.failed.nr; i++)
209 fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
210 }
211
212 string_list_clear(&suite.tests, 0);
213 string_list_clear(&suite.failed, 0);
214
215 return ret;
216 }
217
218 static uint64_t my_random_next = 1234;
219
220 static uint64_t my_random(void)
221 {
222 uint64_t res = my_random_next;
223 my_random_next = my_random_next * 1103515245 + 12345;
224 return res;
225 }
226
227 static int quote_stress_test(int argc, const char **argv)
228 {
229 /*
230 * We are running a quote-stress test.
231 * spawn a subprocess that runs quote-stress with a
232 * special option that echoes back the arguments that
233 * were passed in.
234 */
235 char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
236 int i, j, k, trials = 100, skip = 0, msys2 = 0;
237 struct strbuf out = STRBUF_INIT;
238 struct strvec args = STRVEC_INIT;
239 struct option options[] = {
240 OPT_INTEGER('n', "trials", &trials, "number of trials"),
241 OPT_INTEGER('s', "skip", &skip, "skip <n> trials"),
242 OPT_BOOL('m', "msys2", &msys2, "test quoting for MSYS2's sh"),
243 OPT_END()
244 };
245 const char * const usage[] = {
246 "test-tool run-command quote-stress-test <options>",
247 NULL
248 };
249
250 argc = parse_options(argc, argv, NULL, options, usage, 0);
251
252 setenv("MSYS_NO_PATHCONV", "1", 0);
253
254 for (i = 0; i < trials; i++) {
255 struct child_process cp = CHILD_PROCESS_INIT;
256 size_t arg_count, arg_offset;
257 int ret = 0;
258
259 strvec_clear(&args);
260 if (msys2)
261 strvec_pushl(&args, "sh", "-c",
262 "printf %s\\\\0 \"$@\"", "skip", NULL);
263 else
264 strvec_pushl(&args, "test-tool", "run-command",
265 "quote-echo", NULL);
266 arg_offset = args.nr;
267
268 if (argc > 0) {
269 trials = 1;
270 arg_count = argc;
271 for (j = 0; j < arg_count; j++)
272 strvec_push(&args, argv[j]);
273 } else {
274 arg_count = 1 + (my_random() % 5);
275 for (j = 0; j < arg_count; j++) {
276 char buf[20];
277 size_t min_len = 1;
278 size_t arg_len = min_len +
279 (my_random() % (ARRAY_SIZE(buf) - min_len));
280
281 for (k = 0; k < arg_len; k++)
282 buf[k] = special[my_random() %
283 ARRAY_SIZE(special)];
284 buf[arg_len] = '\0';
285
286 strvec_push(&args, buf);
287 }
288 }
289
290 if (i < skip)
291 continue;
292
293 strvec_pushv(&cp.args, args.v);
294 strbuf_reset(&out);
295 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
296 return error("Failed to spawn child process");
297
298 for (j = 0, k = 0; j < arg_count; j++) {
299 const char *arg = args.v[j + arg_offset];
300
301 if (strcmp(arg, out.buf + k))
302 ret = error("incorrectly quoted arg: '%s', "
303 "echoed back as '%s'",
304 arg, out.buf + k);
305 k += strlen(out.buf + k) + 1;
306 }
307
308 if (k != out.len)
309 ret = error("got %d bytes, but consumed only %d",
310 (int)out.len, (int)k);
311
312 if (ret) {
313 fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
314 for (j = 0; j < arg_count; j++)
315 fprintf(stderr, "arg #%d: '%s'\n",
316 (int)j, args.v[j + arg_offset]);
317
318 strbuf_release(&out);
319 strvec_clear(&args);
320
321 return ret;
322 }
323
324 if (i && (i % 100) == 0)
325 fprintf(stderr, "Trials completed: %d\n", (int)i);
326 }
327
328 strbuf_release(&out);
329 strvec_clear(&args);
330
331 return 0;
332 }
333
334 static int quote_echo(int argc, const char **argv)
335 {
336 while (argc > 1) {
337 fwrite(argv[1], strlen(argv[1]), 1, stdout);
338 fputc('\0', stdout);
339 argv++;
340 argc--;
341 }
342
343 return 0;
344 }
345
346 static int inherit_handle(const char *argv0)
347 {
348 struct child_process cp = CHILD_PROCESS_INIT;
349 char path[PATH_MAX];
350 int tmp;
351
352 /* First, open an inheritable handle */
353 xsnprintf(path, sizeof(path), "out-XXXXXX");
354 tmp = xmkstemp(path);
355
356 strvec_pushl(&cp.args,
357 "test-tool", argv0, "inherited-handle-child", NULL);
358 cp.in = -1;
359 cp.no_stdout = cp.no_stderr = 1;
360 if (start_command(&cp) < 0)
361 die("Could not start child process");
362
363 /* Then close it, and try to delete it. */
364 close(tmp);
365 if (unlink(path))
366 die("Could not delete '%s'", path);
367
368 if (close(cp.in) < 0 || finish_command(&cp) < 0)
369 die("Child did not finish");
370
371 return 0;
372 }
373
374 static int inherit_handle_child(void)
375 {
376 struct strbuf buf = STRBUF_INIT;
377
378 if (strbuf_read(&buf, 0, 0) < 0)
379 die("Could not read stdin");
380 printf("Received %s\n", buf.buf);
381 strbuf_release(&buf);
382
383 return 0;
384 }
385
386 int cmd__run_command(int argc, const char **argv)
387 {
388 struct child_process proc = CHILD_PROCESS_INIT;
389 int jobs;
390 int ret;
391 struct run_process_parallel_opts opts = {
392 .data = &proc,
393 };
394
395 if (argc > 1 && !strcmp(argv[1], "testsuite"))
396 return testsuite(argc - 1, argv + 1);
397 if (!strcmp(argv[1], "inherited-handle"))
398 return inherit_handle(argv[0]);
399 if (!strcmp(argv[1], "inherited-handle-child"))
400 return inherit_handle_child();
401
402 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
403 return !!quote_stress_test(argc - 1, argv + 1);
404
405 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
406 return !!quote_echo(argc - 1, argv + 1);
407
408 if (argc < 3)
409 return 1;
410 while (!strcmp(argv[1], "env")) {
411 if (!argv[2])
412 die("env specifier without a value");
413 strvec_push(&proc.env, argv[2]);
414 argv += 2;
415 argc -= 2;
416 }
417 if (argc < 3) {
418 ret = 1;
419 goto cleanup;
420 }
421 strvec_pushv(&proc.args, (const char **)argv + 2);
422
423 if (!strcmp(argv[1], "start-command-ENOENT")) {
424 if (start_command(&proc) < 0 && errno == ENOENT) {
425 ret = 0;
426 goto cleanup;
427 }
428 fprintf(stderr, "FAIL %s\n", argv[1]);
429 return 1;
430 }
431 if (!strcmp(argv[1], "run-command")) {
432 ret = run_command(&proc);
433 goto cleanup;
434 }
435
436 if (!strcmp(argv[1], "--ungroup")) {
437 argv += 1;
438 argc -= 1;
439 opts.ungroup = 1;
440 }
441
442 jobs = atoi(argv[2]);
443 strvec_clear(&proc.args);
444 strvec_pushv(&proc.args, (const char **)argv + 3);
445
446 if (!strcmp(argv[1], "run-command-parallel")) {
447 opts.get_next_task = parallel_next;
448 } else if (!strcmp(argv[1], "run-command-abort")) {
449 opts.get_next_task = parallel_next;
450 opts.task_finished = task_finished;
451 } else if (!strcmp(argv[1], "run-command-no-jobs")) {
452 opts.get_next_task = no_job;
453 opts.task_finished = task_finished;
454 } else {
455 ret = 1;
456 fprintf(stderr, "check usage\n");
457 goto cleanup;
458 }
459 opts.processes = jobs;
460 run_processes_parallel(&opts);
461 ret = 0;
462 cleanup:
463 child_process_clear(&proc);
464 return ret;
465 }