10
11
#include "test-tool.h"
12
#include "git-compat-util.h"
13
+#include "cache.h"
14
#include "run-command.h"
15
#include "argv-array.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 <string.h>
22
#include <errno.h>
23
55
return 1;
56
}
57
58
+struct 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
+
66
+static 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
+
95
+static 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
+
109
+static 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
+
120
+static const char * const testsuite_usage[] = {
121
+ "test-run-command testsuite [<options>] [<pattern>...]",
122
+ NULL
123
+};
124
+
125
+static 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
+
203
int cmd__run_command(int argc, const char **argv)
204
{
205
struct child_process proc = CHILD_PROCESS_INIT;
206
int jobs;
207
208
+ if (argc > 1 && !strcmp(argv[1], "testsuite"))
209
+ exit(testsuite(argc - 1, argv + 1));
210
+
211
if (argc < 3)
212
return 1;
213
while (!strcmp(argv[1], "env")) {