Git 2.18.4
[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 "run-command.h"
14 #include "argv-array.h"
15 #include "strbuf.h"
16 #include "gettext.h"
17 #include "parse-options.h"
18
19 static int number_callbacks;
20 static int parallel_next(struct child_process *cp,
21                          struct strbuf *err,
22                          void *cb,
23                          void **task_cb)
24 {
25         struct child_process *d = cb;
26         if (number_callbacks >= 4)
27                 return 0;
28
29         argv_array_pushv(&cp->args, d->argv);
30         strbuf_addstr(err, "preloaded output of a child\n");
31         number_callbacks++;
32         return 1;
33 }
34
35 static int no_job(struct child_process *cp,
36                   struct strbuf *err,
37                   void *cb,
38                   void **task_cb)
39 {
40         strbuf_addstr(err, "no further jobs available\n");
41         return 0;
42 }
43
44 static int task_finished(int result,
45                          struct strbuf *err,
46                          void *pp_cb,
47                          void *pp_task_cb)
48 {
49         strbuf_addstr(err, "asking for a quick stop\n");
50         return 1;
51 }
52
53 static uint64_t my_random_next = 1234;
54
55 static uint64_t my_random(void)
56 {
57         uint64_t res = my_random_next;
58         my_random_next = my_random_next * 1103515245 + 12345;
59         return res;
60 }
61
62 static int quote_stress_test(int argc, const char **argv)
63 {
64         /*
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
68          * were passed in.
69          */
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"),
78                 OPT_END()
79         };
80         const char * const usage[] = {
81                 "test-tool run-command quote-stress-test <options>",
82                 NULL
83         };
84
85         argc = parse_options(argc, argv, NULL, options, usage, 0);
86
87         setenv("MSYS_NO_PATHCONV", "1", 0);
88
89         for (i = 0; i < trials; i++) {
90                 struct child_process cp = CHILD_PROCESS_INIT;
91                 size_t arg_count, arg_offset;
92                 int ret = 0;
93
94                 argv_array_clear(&args);
95                 if (msys2)
96                         argv_array_pushl(&args, "sh", "-c",
97                                          "printf %s\\\\0 \"$@\"", "skip", NULL);
98                 else
99                         argv_array_pushl(&args, "test-tool", "run-command",
100                                          "quote-echo", NULL);
101                 arg_offset = args.argc;
102
103                 if (argc > 0) {
104                         trials = 1;
105                         arg_count = argc;
106                         for (j = 0; j < arg_count; j++)
107                                 argv_array_push(&args, argv[j]);
108                 } else {
109                         arg_count = 1 + (my_random() % 5);
110                         for (j = 0; j < arg_count; j++) {
111                                 char buf[20];
112                                 size_t min_len = 1;
113                                 size_t arg_len = min_len +
114                                         (my_random() % (ARRAY_SIZE(buf) - min_len));
115
116                                 for (k = 0; k < arg_len; k++)
117                                         buf[k] = special[my_random() %
118                                                 ARRAY_SIZE(special)];
119                                 buf[arg_len] = '\0';
120
121                                 argv_array_push(&args, buf);
122                         }
123                 }
124
125                 if (i < skip)
126                         continue;
127
128                 cp.argv = args.argv;
129                 strbuf_reset(&out);
130                 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
131                         return error("Failed to spawn child process");
132
133                 for (j = 0, k = 0; j < arg_count; j++) {
134                         const char *arg = args.argv[j + arg_offset];
135
136                         if (strcmp(arg, out.buf + k))
137                                 ret = error("incorrectly quoted arg: '%s', "
138                                             "echoed back as '%s'",
139                                              arg, out.buf + k);
140                         k += strlen(out.buf + k) + 1;
141                 }
142
143                 if (k != out.len)
144                         ret = error("got %d bytes, but consumed only %d",
145                                      (int)out.len, (int)k);
146
147                 if (ret) {
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]);
152
153                         strbuf_release(&out);
154                         argv_array_clear(&args);
155
156                         return ret;
157                 }
158
159                 if (i && (i % 100) == 0)
160                         fprintf(stderr, "Trials completed: %d\n", (int)i);
161         }
162
163         strbuf_release(&out);
164         argv_array_clear(&args);
165
166         return 0;
167 }
168
169 static int quote_echo(int argc, const char **argv)
170 {
171         while (argc > 1) {
172                 fwrite(argv[1], strlen(argv[1]), 1, stdout);
173                 fputc('\0', stdout);
174                 argv++;
175                 argc--;
176         }
177
178         return 0;
179 }
180
181 int cmd__run_command(int argc, const char **argv)
182 {
183         struct child_process proc = CHILD_PROCESS_INIT;
184         int jobs;
185
186         if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
187                 return !!quote_stress_test(argc - 1, argv + 1);
188
189         if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
190                 return !!quote_echo(argc - 1, argv + 1);
191
192         if (argc < 3)
193                 return 1;
194         while (!strcmp(argv[1], "env")) {
195                 if (!argv[2])
196                         die("env specifier without a value");
197                 argv_array_push(&proc.env_array, argv[2]);
198                 argv += 2;
199                 argc -= 2;
200         }
201         if (argc < 3)
202                 return 1;
203         proc.argv = (const char **)argv + 2;
204
205         if (!strcmp(argv[1], "start-command-ENOENT")) {
206                 if (start_command(&proc) < 0 && errno == ENOENT)
207                         return 0;
208                 fprintf(stderr, "FAIL %s\n", argv[1]);
209                 return 1;
210         }
211         if (!strcmp(argv[1], "run-command"))
212                 exit(run_command(&proc));
213
214         jobs = atoi(argv[2]);
215         proc.argv = (const char **)argv + 3;
216
217         if (!strcmp(argv[1], "run-command-parallel"))
218                 exit(run_processes_parallel(jobs, parallel_next,
219                                             NULL, NULL, &proc));
220
221         if (!strcmp(argv[1], "run-command-abort"))
222                 exit(run_processes_parallel(jobs, parallel_next,
223                                             NULL, task_finished, &proc));
224
225         if (!strcmp(argv[1], "run-command-no-jobs"))
226                 exit(run_processes_parallel(jobs, no_job,
227                                             NULL, task_finished, &proc));
228
229         fprintf(stderr, "check usage\n");
230         return 1;
231 }