]> git.ozlabs.org Git - ccan/blob - tools/run_tests.c
Enhance file_analysis preprocessor a little more, use in idempotent test.
[ccan] / tools / run_tests.c
1 #include <err.h>
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <dirent.h>
5 #include <assert.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
8 #include <errno.h>
9 #include "ccan/tap/tap.h"
10 #include "ccan/talloc/talloc.h"
11 #include "ccan/str/str.h"
12 #include "ccan/array_size/array_size.h"
13 #include "tools.h"
14
15 static struct test *tests = NULL;
16 static struct obj *objs = NULL;
17 static int verbose;
18
19 struct test_type
20 {
21         const char *name;
22         void (*buildfn)(const char *dir, struct test_type *t, const char *name,
23                         const char *apiobj);
24         void (*runfn)(const char *name);
25 };
26
27 struct test
28 {
29         struct test *next;
30         struct test_type *type;
31         char *name;
32 };
33
34 struct obj
35 {
36         struct obj *next;
37         bool generate;
38         char *name;
39 };
40
41 static char *output_name(const char *name)
42 {
43         char *ret;
44
45         assert(strends(name, ".c"));
46
47         ret = talloc_strdup(name, name);
48         ret[strlen(ret) - 2] = '\0';
49         return ret;
50 }
51
52 static char *obj_list(void)
53 {
54         char *list = talloc_strdup(objs, "");
55         struct obj *i;
56
57         for (i = objs; i; i = i->next)
58                 list = talloc_asprintf_append(list, "%s ", i->name);
59
60         /* FIXME */
61         list = talloc_asprintf_append(list, "ccan/tap/tap.o");
62         return list;
63 }
64
65 static void compile_objs(void)
66 {
67         struct obj *i;
68
69         for (i = objs; i; i = i->next) {
70                 char *cmd = talloc_asprintf(i, "gcc " CFLAGS " -o %s.o -c %s%s",
71                                             output_name(i->name), i->name,
72                                             verbose ? "" : "> /dev/null 2>&1");
73                 ok(system(cmd) == 0, "%s", cmd);
74         }
75 }
76
77 static void cleanup_objs(void)
78 {
79         struct obj *i;
80
81         for (i = objs; i; i = i->next) {
82                 if (!i->generate)
83                         continue;
84                 unlink(talloc_asprintf(i, "%s.o", output_name(i->name)));
85         }
86 }
87
88 static void add_test(const char *testdir, const char *name, struct test_type *t)
89 {
90         struct test *test = talloc(testdir, struct test);
91
92         test->next = tests;
93         test->type = t;
94         test->name = talloc_asprintf(test, "%s/%s", testdir, name);
95         tests = test;
96 }
97
98 static void add_obj(const char *testdir, const char *name, bool generate)
99 {
100         struct obj *obj = talloc(testdir, struct obj);
101
102         obj->next = objs;
103         obj->name = talloc_asprintf(obj, "%s/%s", testdir, name);
104         obj->generate = generate;
105         objs = obj;
106 }
107
108 static int build(const char *dir, const char *name, const char *apiobj,
109                  int fail)
110 {
111         const char *cmd;
112         int ret;
113
114         cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s %s %s",
115                               fail ? "-DFAIL" : "",
116                               output_name(name), name, apiobj, obj_list(),
117                               verbose ? "" : "> /dev/null 2>&1");
118
119         if (verbose)
120                 fprintf(stderr, "Running %s\n", cmd);
121
122         ret = system(cmd);
123         if (ret == -1)
124                 diag("cmd '%s' failed to execute", cmd);
125
126         return ret;
127 }
128
129 static void compile_ok(const char *dir, struct test_type *t, const char *name,
130                        const char *apiobj)
131 {
132         ok(build(dir, name, "", 0) == 0, "%s %s", t->name, name);
133 }
134
135 /* api tests get the API obj linked in as well. */
136 static void compile_api_ok(const char *dir, struct test_type *t,
137                            const char *name, const char *apiobj)
138 {
139         ok(build(dir, name, apiobj, 0) == 0, "%s %s", t->name, name);
140 }
141
142 static void compile_fail(const char *dir, struct test_type *t, const char *name,
143                          const char *apiobj)
144 {
145         if (build(dir, name, "", 0) != 0)
146                 fail("non-FAIL build %s", name);
147         else
148                 ok(build(dir, name, "", 1) > 0, "%s %s", t->name, name);
149 }
150
151 static void no_run(const char *name)
152 {
153 }
154
155 static void run(const char *name)
156 {
157         if (system(output_name(name)) != 0)
158                 fail("running %s had error", name);
159 }
160
161 static void cleanup(const char *name)
162 {
163         unlink(output_name(name));
164 }
165
166 static struct test_type test_types[] = {
167         { "compile_ok", compile_ok, no_run },
168         { "compile_fail", compile_fail, no_run },
169         { "run", compile_ok, run },
170         { "api", compile_api_ok, run },
171 };
172
173 int main(int argc, char *argv[])
174 {
175         DIR *dir;
176         struct dirent *d;
177         char *testdir, *cwd;
178         const char *apiobj = "";
179         struct test *test;
180         unsigned int num_tests = 0, num_objs = 0, i;
181
182         if (argc > 1 && streq(argv[1], "--verbose")) {
183                 verbose = 1;
184                 argc--;
185                 argv++;
186         }
187
188         if (argc > 1 && strstarts(argv[1], "--apiobj=")) {
189                 apiobj = argv[1] + strlen("--apiobj=");
190                 argc--;
191                 argv++;
192         }
193
194         if (argc < 2)
195                 errx(1, "Usage: run_tests [--verbose] [--apiobj=<obj>] <dir> [<extra-objs>...]");
196
197         testdir = talloc_asprintf(NULL, "%s/test", argv[1]);
198         dir = opendir(testdir);
199         if (!dir)
200                 err(1, "Opening '%s'", testdir);
201
202         while ((d = readdir(dir)) != NULL) {
203                 if (d->d_name[0] == '.' || !strends(d->d_name, ".c"))
204                         continue;
205
206                 for (i = 0; i < ARRAY_SIZE(test_types); i++) {
207                         if (strstarts(d->d_name, test_types[i].name)) {
208                                 add_test(testdir, d->d_name, &test_types[i]);
209                                 num_tests++;
210                                 break;
211                         }
212                 }
213                 if (i == ARRAY_SIZE(test_types)) {
214                         add_obj(testdir, d->d_name, true);
215                         num_objs++;
216                 }
217         }
218
219         plan_tests(num_tests + num_objs + (num_objs ? 1 : 0));
220         /* First all the extra object compilations. */
221         compile_objs();
222
223         /* Now add any object files from the command line */
224         cwd = talloc_strdup(testdir, ".");
225         for (i = 2; i < argc; i++)
226                 add_obj(cwd, argv[i], false);
227
228         /* Do all the test compilations. */
229         for (test = tests; test; test = test->next)
230                 test->type->buildfn(argv[1], test->type, test->name, apiobj);
231
232         cleanup_objs();
233
234         /* Now run all the ones which wanted to run. */
235         for (test = tests; test; test = test->next) {
236                 test->type->runfn(test->name);
237                 cleanup(test->name);
238         }
239
240         exit(exit_status());
241 }