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