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 "git-compat-util.h"
12 #include "run-command.h"
13 #include "argv-array.h"
16 #include "parse-options.h"
18 static int number_callbacks;
19 static int parallel_next(struct child_process *cp,
24 struct child_process *d = cb;
25 if (number_callbacks >= 4)
28 argv_array_pushv(&cp->args, d->argv);
29 strbuf_addstr(err, "preloaded output of a child\n");
34 static int no_job(struct child_process *cp,
39 strbuf_addstr(err, "no further jobs available\n");
43 static int task_finished(int result,
48 strbuf_addstr(err, "asking for a quick stop\n");
52 static uint64_t my_random_next = 1234;
54 static uint64_t my_random(void)
56 uint64_t res = my_random_next;
57 my_random_next = my_random_next * 1103515245 + 12345;
61 static int quote_stress_test(int argc, const char **argv)
64 * We are running a quote-stress test.
65 * spawn a subprocess that runs quote-stress with a
66 * special option that echoes back the arguments that
69 char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
70 int i, j, k, trials = 100;
71 struct strbuf out = STRBUF_INIT;
72 struct argv_array args = ARGV_ARRAY_INIT;
73 struct option options[] = {
74 OPT_INTEGER('n', "trials", &trials, "Number of trials"),
77 const char * const usage[] = {
78 "test-run-command quote-stress-test <options>",
82 argc = parse_options(argc, argv, NULL, options, usage, 0);
84 for (i = 0; i < trials; i++) {
85 struct child_process cp = CHILD_PROCESS_INIT;
86 size_t arg_count, arg_offset;
89 argv_array_clear(&args);
90 argv_array_pushl(&args, "test-run-command",
92 arg_offset = args.argc;
97 for (j = 0; j < arg_count; j++)
98 argv_array_push(&args, argv[j]);
100 arg_count = 1 + (my_random() % 5);
101 for (j = 0; j < arg_count; j++) {
104 size_t arg_len = min_len +
105 (my_random() % (ARRAY_SIZE(buf) - min_len));
107 for (k = 0; k < arg_len; k++)
108 buf[k] = special[my_random() %
109 ARRAY_SIZE(special)];
112 argv_array_push(&args, buf);
118 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
119 return error("Failed to spawn child process");
121 for (j = 0, k = 0; j < arg_count; j++) {
122 const char *arg = args.argv[j + arg_offset];
124 if (strcmp(arg, out.buf + k))
125 ret = error("incorrectly quoted arg: '%s', "
126 "echoed back as '%s'",
128 k += strlen(out.buf + k) + 1;
132 ret = error("got %d bytes, but consumed only %d",
133 (int)out.len, (int)k);
136 fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
137 for (j = 0; j < arg_count; j++)
138 fprintf(stderr, "arg #%d: '%s'\n",
139 (int)j, args.argv[j + arg_offset]);
141 strbuf_release(&out);
142 argv_array_clear(&args);
147 if (i && (i % 100) == 0)
148 fprintf(stderr, "Trials completed: %d\n", (int)i);
151 strbuf_release(&out);
152 argv_array_clear(&args);
157 static int quote_echo(int argc, const char **argv)
160 fwrite(argv[1], strlen(argv[1]), 1, stdout);
169 int cmd_main(int argc, const char **argv)
171 struct child_process proc = CHILD_PROCESS_INIT;
174 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
175 return !!quote_stress_test(argc - 1, argv + 1);
177 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
178 return !!quote_echo(argc - 1, argv + 1);
182 proc.argv = (const char **)argv + 2;
184 if (!strcmp(argv[1], "start-command-ENOENT")) {
185 if (start_command(&proc) < 0 && errno == ENOENT)
187 fprintf(stderr, "FAIL %s\n", argv[1]);
190 if (!strcmp(argv[1], "run-command"))
191 exit(run_command(&proc));
193 jobs = atoi(argv[2]);
194 proc.argv = (const char **)argv + 3;
196 if (!strcmp(argv[1], "run-command-parallel"))
197 exit(run_processes_parallel(jobs, parallel_next,
200 if (!strcmp(argv[1], "run-command-abort"))
201 exit(run_processes_parallel(jobs, parallel_next,
202 NULL, task_finished, &proc));
204 if (!strcmp(argv[1], "run-command-no-jobs"))
205 exit(run_processes_parallel(jobs, no_job,
206 NULL, task_finished, &proc));
208 fprintf(stderr, "check usage\n");