]> git.ozlabs.org Git - ccan/blob - tools/run_tests.c
Proper recursive dependencies (came from libantithread work)
[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 "ccan/tap/tap.h"
8 #include "ccan/talloc/talloc.h"
9 #include "ccan/string/string.h"
10 #include "tools.h"
11
12 /* FIXME: Use build bug later. */
13 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
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 (*testfn)(const char *dir, struct test_type *t, const char *name);
23 };
24
25 struct test
26 {
27         struct test *next;
28         struct test_type *type;
29         char *name;
30 };
31
32 struct obj
33 {
34         struct obj *next;
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(void)
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         list = talloc_asprintf_append(list, "ccan/tap/tap.o");
59         return list;
60 }
61
62 static void compile_objs(void)
63 {
64         struct obj *i;
65
66         for (i = objs; i; i = i->next) {
67                 char *cmd = talloc_asprintf(i, "gcc " CFLAGS " -o %s.o -c %s%s",
68                                             output_name(i->name), i->name,
69                                             verbose ? "" : "> /dev/null 2>&1");
70                 ok(system(cmd) == 0, "%s", cmd);
71         }
72 }
73
74 static void cleanup_objs(void)
75 {
76         struct obj *i;
77
78         for (i = objs; i; i = i->next)
79                 unlink(talloc_asprintf(i, "%s.o", output_name(i->name)));
80 }
81
82 static void add_test(const char *testdir, const char *name, struct test_type *t)
83 {
84         struct test *test = talloc(testdir, struct test);
85
86         test->next = tests;
87         test->type = t;
88         test->name = talloc_asprintf(test, "%s/%s", testdir, name);
89         tests = test;
90 }
91
92 static void add_obj(const char *testdir, const char *name)
93 {
94         struct obj *obj = talloc(testdir, struct obj);
95
96         obj->next = objs;
97         obj->name = talloc_asprintf(obj, "%s/%s", testdir, name);
98         objs = obj;
99 }
100
101 static int build(const char *dir, const char *name, int fail)
102 {
103         const char *cmd;
104         int ret;
105         char *externals = talloc_strdup(name, "");
106         char **deps;
107
108         for (deps = get_deps(objs, dir); *deps; deps++) {
109                 char *end;
110                 if (!strstarts(*deps, "ccan/"))
111                         continue;
112
113                 end = strrchr(*deps, '/') + 1;
114                 /* ccan/foo -> ccan/libfoo.a */
115                 externals = talloc_asprintf_append(externals,
116                                                    " ccan/lib%s.a", end);
117         }
118
119         cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s%s%s",
120                               fail ? "-DFAIL" : "",
121                               output_name(name), name, obj_list(), externals,
122                               verbose ? "" : "> /dev/null 2>&1");
123
124         if (verbose)
125                 fprintf(stderr, "Running %s\n", cmd);
126
127         ret = system(cmd);
128         if (ret == -1)
129                 diag("cmd '%s' failed to execute", cmd);
130
131         return ret;
132 }
133
134 static void compile_ok(const char *dir, struct test_type *t, const char *name)
135 {
136         ok(build(dir, name, 0) == 0, "%s %s", t->name, name);
137 }
138
139 static void compile_fail(const char *dir, struct test_type *t, const char *name)
140 {
141         if (build(dir, name, 0) != 0)
142                 fail("non-FAIL build %s", name);
143         else
144                 ok(build(dir, name, 1) > 0, "%s %s", t->name, name);
145 }
146
147 static void run(const char *name)
148 {
149         if (system(output_name(name)) == -1)
150                 fail("running %s had error %m", name);
151 }
152
153 static void cleanup(const char *name)
154 {
155         unlink(output_name(name));
156 }
157
158 static struct test_type test_types[] = {
159         { "compile_ok", compile_ok },
160         { "compile_fail", compile_fail },
161         { "run", compile_ok },
162 };
163
164 int main(int argc, char *argv[])
165 {
166         DIR *dir;
167         struct dirent *d;
168         char *testdir;
169         struct test *test;
170         unsigned int num_tests = 0, num_objs = 0;
171
172         if (argc > 1 && streq(argv[1], "--verbose")) {
173                 verbose = 1;
174                 argc--;
175                 argv++;
176         }
177
178         if (argc != 2)
179                 errx(1, "Usage: run_tests [--verbose] <dir>");
180
181         testdir = talloc_asprintf(NULL, "%s/test", argv[1]);
182         dir = opendir(testdir);
183         if (!dir)
184                 err(1, "Opening '%s'", testdir);
185
186         while ((d = readdir(dir)) != NULL) {
187                 unsigned int i;
188                 if (d->d_name[0] == '.' || !strends(d->d_name, ".c"))
189                         continue;
190
191                 for (i = 0; i < ARRAY_SIZE(test_types); i++) {
192                         if (strstarts(d->d_name, test_types[i].name)) {
193                                 add_test(testdir, d->d_name, &test_types[i]);
194                                 num_tests++;
195                                 break;
196                         }
197                 }
198                 if (i == ARRAY_SIZE(test_types)) {
199                         add_obj(testdir, d->d_name);
200                         num_objs++;
201                 }
202         }
203
204         plan_tests(num_tests + num_objs + (num_objs ? 1 : 0));
205         /* First all the extra object compilations. */
206         compile_objs();
207
208         /* Do all the test compilations. */
209         for (test = tests; test; test = test->next)
210                 test->type->testfn(argv[1], test->type, test->name);
211
212         cleanup_objs();
213
214         /* Now run all the ones which wanted to run. */
215         for (test = tests; test; test = test->next) {
216                 if (streq(test->type->name, "run"))
217                         run(test->name);
218                 cleanup(test->name);
219         }
220
221         exit(exit_status());
222 }