Merge branch 'ab/config-based-hooks-base' into seen
[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 #include "parse-options.h"
23
24 static int number_callbacks;
25 static int parallel_next(struct child_process *cp,
26                          struct strbuf *err,
27                          void *cb,
28                          void **task_cb)
29 {
30         struct child_process *d = cb;
31         if (number_callbacks >= 4)
32                 return 0;
33
34         strvec_pushv(&cp->args, d->argv);
35         cp->in = d->in;
36         cp->no_stdin = d->no_stdin;
37         strbuf_addstr(err, "preloaded output of a child\n");
38         number_callbacks++;
39
40         *task_cb = xmalloc(sizeof(int));
41         *(int*)(*task_cb) = 2;
42         return 1;
43 }
44
45 static int no_job(struct child_process *cp,
46                   struct strbuf *err,
47                   void *cb,
48                   void **task_cb)
49 {
50         strbuf_addstr(err, "no further jobs available\n");
51         return 0;
52 }
53
54 static void test_consume_sideband(struct strbuf *output, void *cb)
55 {
56         FILE *sideband;
57
58         sideband = fopen("./sideband", "a");
59
60         strbuf_write(output, sideband);
61         fclose(sideband);
62 }
63
64 static int task_finished(int result,
65                          struct strbuf *err,
66                          void *pp_cb,
67                          void *pp_task_cb)
68 {
69         strbuf_addstr(err, "asking for a quick stop\n");
70         return 1;
71 }
72
73 static int test_stdin(struct strbuf *pipe, void *cb, void *task_cb)
74 {
75         int *lines_remaining = task_cb;
76
77         if (*lines_remaining)
78                 strbuf_addf(pipe, "sample stdin %d\n", --(*lines_remaining));
79
80         return !(*lines_remaining);
81 }
82
83
84 struct testsuite {
85         struct string_list tests, failed;
86         int next;
87         int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
88 };
89 #define TESTSUITE_INIT \
90         { STRING_LIST_INIT_DUP, STRING_LIST_INIT_DUP, -1, 0, 0, 0, 0, 0, 0 }
91
92 static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
93                      void **task_cb)
94 {
95         struct testsuite *suite = cb;
96         const char *test;
97         if (suite->next >= suite->tests.nr)
98                 return 0;
99
100         test = suite->tests.items[suite->next++].string;
101         strvec_pushl(&cp->args, "sh", test, NULL);
102         if (suite->quiet)
103                 strvec_push(&cp->args, "--quiet");
104         if (suite->immediate)
105                 strvec_push(&cp->args, "-i");
106         if (suite->verbose)
107                 strvec_push(&cp->args, "-v");
108         if (suite->verbose_log)
109                 strvec_push(&cp->args, "-V");
110         if (suite->trace)
111                 strvec_push(&cp->args, "-x");
112         if (suite->write_junit_xml)
113                 strvec_push(&cp->args, "--write-junit-xml");
114
115         strbuf_addf(err, "Output of '%s':\n", test);
116         *task_cb = (void *)test;
117
118         return 1;
119 }
120
121 static int test_finished(int result, struct strbuf *err, void *cb,
122                          void *task_cb)
123 {
124         struct testsuite *suite = cb;
125         const char *name = (const char *)task_cb;
126
127         if (result)
128                 string_list_append(&suite->failed, name);
129
130         strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
131
132         return 0;
133 }
134
135 static int test_failed(struct strbuf *out, void *cb, void *task_cb)
136 {
137         struct testsuite *suite = cb;
138         const char *name = (const char *)task_cb;
139
140         string_list_append(&suite->failed, name);
141         strbuf_addf(out, "FAILED TO START: '%s'\n", name);
142
143         return 0;
144 }
145
146 static const char * const testsuite_usage[] = {
147         "test-run-command testsuite [<options>] [<pattern>...]",
148         NULL
149 };
150
151 static int testsuite(int argc, const char **argv)
152 {
153         struct testsuite suite = TESTSUITE_INIT;
154         int max_jobs = 1, i, ret;
155         DIR *dir;
156         struct dirent *d;
157         struct option options[] = {
158                 OPT_BOOL('i', "immediate", &suite.immediate,
159                          "stop at first failed test case(s)"),
160                 OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
161                 OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
162                 OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
163                 OPT_BOOL('V', "verbose-log", &suite.verbose_log,
164                          "be verbose, redirected to a file"),
165                 OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
166                 OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
167                          "write JUnit-style XML files"),
168                 OPT_END()
169         };
170
171         memset(&suite, 0, sizeof(suite));
172         suite.tests.strdup_strings = suite.failed.strdup_strings = 1;
173
174         argc = parse_options(argc, argv, NULL, options,
175                         testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
176
177         if (max_jobs <= 0)
178                 max_jobs = online_cpus();
179
180         dir = opendir(".");
181         if (!dir)
182                 die("Could not open the current directory");
183         while ((d = readdir(dir))) {
184                 const char *p = d->d_name;
185
186                 if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
187                     !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
188                     !ends_with(p, ".sh"))
189                         continue;
190
191                 /* No pattern: match all */
192                 if (!argc) {
193                         string_list_append(&suite.tests, p);
194                         continue;
195                 }
196
197                 for (i = 0; i < argc; i++)
198                         if (!wildmatch(argv[i], p, 0)) {
199                                 string_list_append(&suite.tests, p);
200                                 break;
201                         }
202         }
203         closedir(dir);
204
205         if (!suite.tests.nr)
206                 die("No tests match!");
207         if (max_jobs > suite.tests.nr)
208                 max_jobs = suite.tests.nr;
209
210         fprintf(stderr, "Running %d tests (%d at a time)\n",
211                 suite.tests.nr, max_jobs);
212
213         ret = run_processes_parallel(max_jobs, next_test, test_failed,
214                                      test_stdin, NULL, test_finished, &suite);
215
216         if (suite.failed.nr > 0) {
217                 ret = 1;
218                 fprintf(stderr, "%d tests failed:\n\n", suite.failed.nr);
219                 for (i = 0; i < suite.failed.nr; i++)
220                         fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
221         }
222
223         string_list_clear(&suite.tests, 0);
224         string_list_clear(&suite.failed, 0);
225
226         return !!ret;
227 }
228
229 static uint64_t my_random_next = 1234;
230
231 static uint64_t my_random(void)
232 {
233         uint64_t res = my_random_next;
234         my_random_next = my_random_next * 1103515245 + 12345;
235         return res;
236 }
237
238 static int quote_stress_test(int argc, const char **argv)
239 {
240         /*
241          * We are running a quote-stress test.
242          * spawn a subprocess that runs quote-stress with a
243          * special option that echoes back the arguments that
244          * were passed in.
245          */
246         char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
247         int i, j, k, trials = 100, skip = 0, msys2 = 0;
248         struct strbuf out = STRBUF_INIT;
249         struct strvec args = STRVEC_INIT;
250         struct option options[] = {
251                 OPT_INTEGER('n', "trials", &trials, "Number of trials"),
252                 OPT_INTEGER('s', "skip", &skip, "Skip <n> trials"),
253                 OPT_BOOL('m', "msys2", &msys2, "Test quoting for MSYS2's sh"),
254                 OPT_END()
255         };
256         const char * const usage[] = {
257                 "test-tool run-command quote-stress-test <options>",
258                 NULL
259         };
260
261         argc = parse_options(argc, argv, NULL, options, usage, 0);
262
263         setenv("MSYS_NO_PATHCONV", "1", 0);
264
265         for (i = 0; i < trials; i++) {
266                 struct child_process cp = CHILD_PROCESS_INIT;
267                 size_t arg_count, arg_offset;
268                 int ret = 0;
269
270                 strvec_clear(&args);
271                 if (msys2)
272                         strvec_pushl(&args, "sh", "-c",
273                                      "printf %s\\\\0 \"$@\"", "skip", NULL);
274                 else
275                         strvec_pushl(&args, "test-tool", "run-command",
276                                      "quote-echo", NULL);
277                 arg_offset = args.nr;
278
279                 if (argc > 0) {
280                         trials = 1;
281                         arg_count = argc;
282                         for (j = 0; j < arg_count; j++)
283                                 strvec_push(&args, argv[j]);
284                 } else {
285                         arg_count = 1 + (my_random() % 5);
286                         for (j = 0; j < arg_count; j++) {
287                                 char buf[20];
288                                 size_t min_len = 1;
289                                 size_t arg_len = min_len +
290                                         (my_random() % (ARRAY_SIZE(buf) - min_len));
291
292                                 for (k = 0; k < arg_len; k++)
293                                         buf[k] = special[my_random() %
294                                                 ARRAY_SIZE(special)];
295                                 buf[arg_len] = '\0';
296
297                                 strvec_push(&args, buf);
298                         }
299                 }
300
301                 if (i < skip)
302                         continue;
303
304                 cp.argv = args.v;
305                 strbuf_reset(&out);
306                 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
307                         return error("Failed to spawn child process");
308
309                 for (j = 0, k = 0; j < arg_count; j++) {
310                         const char *arg = args.v[j + arg_offset];
311
312                         if (strcmp(arg, out.buf + k))
313                                 ret = error("incorrectly quoted arg: '%s', "
314                                             "echoed back as '%s'",
315                                              arg, out.buf + k);
316                         k += strlen(out.buf + k) + 1;
317                 }
318
319                 if (k != out.len)
320                         ret = error("got %d bytes, but consumed only %d",
321                                      (int)out.len, (int)k);
322
323                 if (ret) {
324                         fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
325                         for (j = 0; j < arg_count; j++)
326                                 fprintf(stderr, "arg #%d: '%s'\n",
327                                         (int)j, args.v[j + arg_offset]);
328
329                         strbuf_release(&out);
330                         strvec_clear(&args);
331
332                         return ret;
333                 }
334
335                 if (i && (i % 100) == 0)
336                         fprintf(stderr, "Trials completed: %d\n", (int)i);
337         }
338
339         strbuf_release(&out);
340         strvec_clear(&args);
341
342         return 0;
343 }
344
345 static int quote_echo(int argc, const char **argv)
346 {
347         while (argc > 1) {
348                 fwrite(argv[1], strlen(argv[1]), 1, stdout);
349                 fputc('\0', stdout);
350                 argv++;
351                 argc--;
352         }
353
354         return 0;
355 }
356
357 static int inherit_handle(const char *argv0)
358 {
359         struct child_process cp = CHILD_PROCESS_INIT;
360         char path[PATH_MAX];
361         int tmp;
362
363         /* First, open an inheritable handle */
364         xsnprintf(path, sizeof(path), "out-XXXXXX");
365         tmp = xmkstemp(path);
366
367         strvec_pushl(&cp.args,
368                      "test-tool", argv0, "inherited-handle-child", NULL);
369         cp.in = -1;
370         cp.no_stdout = cp.no_stderr = 1;
371         if (start_command(&cp) < 0)
372                 die("Could not start child process");
373
374         /* Then close it, and try to delete it. */
375         close(tmp);
376         if (unlink(path))
377                 die("Could not delete '%s'", path);
378
379         if (close(cp.in) < 0 || finish_command(&cp) < 0)
380                 die("Child did not finish");
381
382         return 0;
383 }
384
385 static int inherit_handle_child(void)
386 {
387         struct strbuf buf = STRBUF_INIT;
388
389         if (strbuf_read(&buf, 0, 0) < 0)
390                 die("Could not read stdin");
391         printf("Received %s\n", buf.buf);
392         strbuf_release(&buf);
393
394         return 0;
395 }
396
397 int cmd__run_command(int argc, const char **argv)
398 {
399         struct child_process proc = CHILD_PROCESS_INIT;
400         int jobs;
401
402         if (argc > 1 && !strcmp(argv[1], "testsuite"))
403                 exit(testsuite(argc - 1, argv + 1));
404         if (!strcmp(argv[1], "inherited-handle"))
405                 exit(inherit_handle(argv[0]));
406         if (!strcmp(argv[1], "inherited-handle-child"))
407                 exit(inherit_handle_child());
408
409         if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
410                 return !!quote_stress_test(argc - 1, argv + 1);
411
412         if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
413                 return !!quote_echo(argc - 1, argv + 1);
414
415         if (argc < 3)
416                 return 1;
417         while (!strcmp(argv[1], "env")) {
418                 if (!argv[2])
419                         die("env specifier without a value");
420                 strvec_push(&proc.env_array, argv[2]);
421                 argv += 2;
422                 argc -= 2;
423         }
424         if (argc < 3)
425                 return 1;
426         proc.argv = (const char **)argv + 2;
427
428         if (!strcmp(argv[1], "start-command-ENOENT")) {
429                 if (start_command(&proc) < 0 && errno == ENOENT)
430                         return 0;
431                 fprintf(stderr, "FAIL %s\n", argv[1]);
432                 return 1;
433         }
434         if (!strcmp(argv[1], "run-command"))
435                 exit(run_command(&proc));
436
437         jobs = atoi(argv[2]);
438         proc.argv = (const char **)argv + 3;
439
440         if (!strcmp(argv[1], "run-command-parallel"))
441                 exit(run_processes_parallel(jobs, parallel_next,
442                                             NULL, NULL, NULL, NULL, &proc));
443
444         if (!strcmp(argv[1], "run-command-abort"))
445                 exit(run_processes_parallel(jobs, parallel_next,
446                                             NULL, NULL, NULL, task_finished, &proc));
447
448         if (!strcmp(argv[1], "run-command-no-jobs"))
449                 exit(run_processes_parallel(jobs, no_job,
450                                             NULL, NULL, NULL, task_finished, &proc));
451
452         if (!strcmp(argv[1], "run-command-stdin")) {
453                 proc.in = -1;
454                 proc.no_stdin = 0;
455                 exit (run_processes_parallel(jobs, parallel_next, NULL,
456                                              test_stdin, NULL, NULL, &proc));
457         }
458
459         if (!strcmp(argv[1], "run-command-sideband"))
460                 exit(run_processes_parallel(jobs, parallel_next, NULL, NULL,
461                                             test_consume_sideband, NULL,
462                                             &proc));
463
464         fprintf(stderr, "check usage\n");
465         return 1;
466 }