]> git.ozlabs.org Git - ccan/blob - tools/run_tests.c
Fix up line numbers after commit 109
[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 "tools.h"
13
14 /* FIXME: Use build bug later. */
15 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
16
17 static struct test *tests = NULL;
18 static struct obj *objs = NULL;
19 static int verbose;
20
21 struct test_type
22 {
23         const char *name;
24         void (*buildfn)(const char *dir, struct test_type *t, const char *name);
25         void (*runfn)(const char *name);
26 };
27
28 struct test
29 {
30         struct test *next;
31         struct test_type *type;
32         char *name;
33 };
34
35 struct obj
36 {
37         struct obj *next;
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                 unlink(talloc_asprintf(i, "%s.o", output_name(i->name)));
83 }
84
85 static void add_test(const char *testdir, const char *name, struct test_type *t)
86 {
87         struct test *test = talloc(testdir, struct test);
88
89         test->next = tests;
90         test->type = t;
91         test->name = talloc_asprintf(test, "%s/%s", testdir, name);
92         tests = test;
93 }
94
95 static void add_obj(const char *testdir, const char *name)
96 {
97         struct obj *obj = talloc(testdir, struct obj);
98
99         obj->next = objs;
100         obj->name = talloc_asprintf(obj, "%s/%s", testdir, name);
101         objs = obj;
102 }
103
104 static int build(const char *dir, const char *name, int fail)
105 {
106         const char *cmd;
107         int ret;
108         char *externals = talloc_strdup(name, "");
109         char **deps;
110
111         for (deps = get_deps(talloc_autofree_context(), dir, true); *deps; deps++) {
112                 char *newobj;
113                 struct stat st;
114
115                 if (!strstarts(*deps, "ccan/"))
116                         continue;
117
118                 /* ccan/foo -> ccan/foo.o */
119                 newobj = talloc_asprintf(name, "%s.o", *deps);
120
121                 /* Only if it exists.  Makefile sorts this out. */
122                 if (stat(newobj, &st) == 0 || errno != ENOENT)
123                         externals = talloc_asprintf_append(externals,
124                                                            " %s", newobj);
125         }
126
127         cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s%s%s",
128                               fail ? "-DFAIL" : "",
129                               output_name(name), name, obj_list(), externals,
130                               verbose ? "" : "> /dev/null 2>&1");
131
132         if (verbose)
133                 fprintf(stderr, "Running %s\n", cmd);
134
135         ret = system(cmd);
136         if (ret == -1)
137                 diag("cmd '%s' failed to execute", cmd);
138
139         return ret;
140 }
141
142 static void compile_ok(const char *dir, struct test_type *t, const char *name)
143 {
144         ok(build(dir, name, 0) == 0, "%s %s", t->name, name);
145 }
146
147 static void compile_fail(const char *dir, struct test_type *t, const char *name)
148 {
149         if (build(dir, name, 0) != 0)
150                 fail("non-FAIL build %s", name);
151         else
152                 ok(build(dir, name, 1) > 0, "%s %s", t->name, name);
153 }
154
155 static void no_run(const char *name)
156 {
157 }
158
159 static void run(const char *name)
160 {
161         if (system(output_name(name)) == -1)
162                 fail("running %s had error %m", name);
163 }
164
165 static void cleanup(const char *name)
166 {
167         unlink(output_name(name));
168 }
169
170 static struct test_type test_types[] = {
171         { "compile_ok", compile_ok, no_run },
172         { "compile_fail", compile_fail, no_run },
173         { "run", compile_ok, run },
174         { "api", compile_ok, run },
175 };
176
177 int main(int argc, char *argv[])
178 {
179         DIR *dir;
180         struct dirent *d;
181         char *testdir;
182         struct test *test;
183         unsigned int num_tests = 0, num_objs = 0;
184
185         if (argc > 1 && streq(argv[1], "--verbose")) {
186                 verbose = 1;
187                 argc--;
188                 argv++;
189         }
190
191         if (argc != 2)
192                 errx(1, "Usage: run_tests [--verbose] <dir>");
193
194         testdir = talloc_asprintf(NULL, "%s/test", argv[1]);
195         dir = opendir(testdir);
196         if (!dir)
197                 err(1, "Opening '%s'", testdir);
198
199         while ((d = readdir(dir)) != NULL) {
200                 unsigned int i;
201                 if (d->d_name[0] == '.' || !strends(d->d_name, ".c"))
202                         continue;
203
204                 for (i = 0; i < ARRAY_SIZE(test_types); i++) {
205                         if (strstarts(d->d_name, test_types[i].name)) {
206                                 add_test(testdir, d->d_name, &test_types[i]);
207                                 num_tests++;
208                                 break;
209                         }
210                 }
211                 if (i == ARRAY_SIZE(test_types)) {
212                         add_obj(testdir, d->d_name);
213                         num_objs++;
214                 }
215         }
216
217         plan_tests(num_tests + num_objs + (num_objs ? 1 : 0));
218         /* First all the extra object compilations. */
219         compile_objs();
220
221         /* Do all the test compilations. */
222         for (test = tests; test; test = test->next)
223                 test->type->buildfn(argv[1], test->type, test->name);
224
225         cleanup_objs();
226
227         /* Now run all the ones which wanted to run. */
228         for (test = tests; test; test = test->next) {
229                 test->type->runfn(test->name);
230                 cleanup(test->name);
231         }
232
233         exit(exit_status());
234 }