]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-run-command.c
Merge branch 'en/merge-recursive-directory-rename-fixes'
[thirdparty/git.git] / t / helper / test-run-command.c
CommitLineData
2b541bf8
JS
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
ae6a51f5 11#include "test-tool.h"
2b541bf8 12#include "git-compat-util.h"
be5d88e1 13#include "cache.h"
2b541bf8 14#include "run-command.h"
c553c72e
SB
15#include "argv-array.h"
16#include "strbuf.h"
be5d88e1
JS
17#include "parse-options.h"
18#include "string-list.h"
19#include "thread-utils.h"
20#include "wildmatch.h"
2b541bf8
JS
21#include <string.h>
22#include <errno.h>
23
c553c72e
SB
24static int number_callbacks;
25static 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 argv_array_pushv(&cp->args, d->argv);
02962d36 35 strbuf_addstr(err, "preloaded output of a child\n");
c553c72e
SB
36 number_callbacks++;
37 return 1;
38}
39
40static int no_job(struct child_process *cp,
41 struct strbuf *err,
42 void *cb,
43 void **task_cb)
44{
02962d36 45 strbuf_addstr(err, "no further jobs available\n");
c553c72e
SB
46 return 0;
47}
48
49static int task_finished(int result,
c553c72e
SB
50 struct strbuf *err,
51 void *pp_cb,
52 void *pp_task_cb)
53{
02962d36 54 strbuf_addstr(err, "asking for a quick stop\n");
c553c72e
SB
55 return 1;
56}
57
be5d88e1
JS
58struct testsuite {
59 struct string_list tests, failed;
60 int next;
61 int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
62};
63#define TESTSUITE_INIT \
64 { STRING_LIST_INIT_DUP, STRING_LIST_INIT_DUP, -1, 0, 0, 0, 0, 0, 0 }
65
66static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
67 void **task_cb)
68{
69 struct testsuite *suite = cb;
70 const char *test;
71 if (suite->next >= suite->tests.nr)
72 return 0;
73
74 test = suite->tests.items[suite->next++].string;
75 argv_array_pushl(&cp->args, "sh", test, NULL);
76 if (suite->quiet)
77 argv_array_push(&cp->args, "--quiet");
78 if (suite->immediate)
79 argv_array_push(&cp->args, "-i");
80 if (suite->verbose)
81 argv_array_push(&cp->args, "-v");
82 if (suite->verbose_log)
83 argv_array_push(&cp->args, "-V");
84 if (suite->trace)
85 argv_array_push(&cp->args, "-x");
86 if (suite->write_junit_xml)
87 argv_array_push(&cp->args, "--write-junit-xml");
88
89 strbuf_addf(err, "Output of '%s':\n", test);
90 *task_cb = (void *)test;
91
92 return 1;
93}
94
95static int test_finished(int result, struct strbuf *err, void *cb,
96 void *task_cb)
97{
98 struct testsuite *suite = cb;
99 const char *name = (const char *)task_cb;
100
101 if (result)
102 string_list_append(&suite->failed, name);
103
104 strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
105
106 return 0;
107}
108
109static int test_failed(struct strbuf *out, void *cb, void *task_cb)
110{
111 struct testsuite *suite = cb;
112 const char *name = (const char *)task_cb;
113
114 string_list_append(&suite->failed, name);
115 strbuf_addf(out, "FAILED TO START: '%s'\n", name);
116
117 return 0;
118}
119
120static const char * const testsuite_usage[] = {
121 "test-run-command testsuite [<options>] [<pattern>...]",
122 NULL
123};
124
125static int testsuite(int argc, const char **argv)
126{
127 struct testsuite suite = TESTSUITE_INIT;
128 int max_jobs = 1, i, ret;
129 DIR *dir;
130 struct dirent *d;
131 struct option options[] = {
132 OPT_BOOL('i', "immediate", &suite.immediate,
133 "stop at first failed test case(s)"),
134 OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
135 OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
136 OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
137 OPT_BOOL('V', "verbose-log", &suite.verbose_log,
138 "be verbose, redirected to a file"),
139 OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
140 OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
141 "write JUnit-style XML files"),
142 OPT_END()
143 };
144
145 memset(&suite, 0, sizeof(suite));
146 suite.tests.strdup_strings = suite.failed.strdup_strings = 1;
147
148 argc = parse_options(argc, argv, NULL, options,
149 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
150
151 if (max_jobs <= 0)
152 max_jobs = online_cpus();
153
154 dir = opendir(".");
155 if (!dir)
156 die("Could not open the current directory");
157 while ((d = readdir(dir))) {
158 const char *p = d->d_name;
159
160 if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
161 !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
162 !ends_with(p, ".sh"))
163 continue;
164
165 /* No pattern: match all */
166 if (!argc) {
167 string_list_append(&suite.tests, p);
168 continue;
169 }
170
171 for (i = 0; i < argc; i++)
172 if (!wildmatch(argv[i], p, 0)) {
173 string_list_append(&suite.tests, p);
174 break;
175 }
176 }
177 closedir(dir);
178
179 if (!suite.tests.nr)
180 die("No tests match!");
181 if (max_jobs > suite.tests.nr)
182 max_jobs = suite.tests.nr;
183
184 fprintf(stderr, "Running %d tests (%d at a time)\n",
185 suite.tests.nr, max_jobs);
186
187 ret = run_processes_parallel(max_jobs, next_test, test_failed,
188 test_finished, &suite);
189
190 if (suite.failed.nr > 0) {
191 ret = 1;
192 fprintf(stderr, "%d tests failed:\n\n", suite.failed.nr);
193 for (i = 0; i < suite.failed.nr; i++)
194 fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
195 }
196
197 string_list_clear(&suite.tests, 0);
198 string_list_clear(&suite.failed, 0);
199
200 return !!ret;
201}
202
ae6a51f5 203int cmd__run_command(int argc, const char **argv)
2b541bf8 204{
d3180279 205 struct child_process proc = CHILD_PROCESS_INIT;
c553c72e 206 int jobs;
2b541bf8 207
be5d88e1
JS
208 if (argc > 1 && !strcmp(argv[1], "testsuite"))
209 exit(testsuite(argc - 1, argv + 1));
210
c61a975d
NTND
211 if (argc < 3)
212 return 1;
213 while (!strcmp(argv[1], "env")) {
214 if (!argv[2])
215 die("env specifier without a value");
216 argv_array_push(&proc.env_array, argv[2]);
217 argv += 2;
218 argc -= 2;
219 }
2b541bf8
JS
220 if (argc < 3)
221 return 1;
c553c72e 222 proc.argv = (const char **)argv + 2;
2b541bf8
JS
223
224 if (!strcmp(argv[1], "start-command-ENOENT")) {
225 if (start_command(&proc) < 0 && errno == ENOENT)
226 return 0;
227 fprintf(stderr, "FAIL %s\n", argv[1]);
228 return 1;
229 }
c0f19bf3
JN
230 if (!strcmp(argv[1], "run-command"))
231 exit(run_command(&proc));
2b541bf8 232
c553c72e
SB
233 jobs = atoi(argv[2]);
234 proc.argv = (const char **)argv + 3;
235
236 if (!strcmp(argv[1], "run-command-parallel"))
237 exit(run_processes_parallel(jobs, parallel_next,
238 NULL, NULL, &proc));
239
240 if (!strcmp(argv[1], "run-command-abort"))
241 exit(run_processes_parallel(jobs, parallel_next,
242 NULL, task_finished, &proc));
243
244 if (!strcmp(argv[1], "run-command-no-jobs"))
245 exit(run_processes_parallel(jobs, no_job,
246 NULL, task_finished, &proc));
247
2b541bf8
JS
248 fprintf(stderr, "check usage\n");
249 return 1;
250}