]> git.ozlabs.org Git - ccan/blob - tools/run_tests.c
zero-append test for tdb.
[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(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                 if (!i->generate)
80                         continue;
81                 unlink(talloc_asprintf(i, "%s.o", output_name(i->name)));
82         }
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, bool generate)
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         obj->generate = generate;
102         objs = obj;
103 }
104
105 static int build(const char *dir, const char *name, const char *apiobj,
106                  const char *libs, int fail)
107 {
108         const char *cmd;
109         int ret;
110
111         cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s %s%s %s",
112                               fail ? "-DFAIL" : "",
113                               output_name(name), name, apiobj, obj_list(), libs,
114                               verbose ? "" : "> /dev/null 2>&1");
115
116         if (verbose)
117                 fprintf(stderr, "Running %s\n", cmd);
118
119         ret = system(cmd);
120         if (ret == -1)
121                 diag("cmd '%s' failed to execute", cmd);
122
123         return ret;
124 }
125
126 static void compile_ok(const char *dir, struct test_type *t, const char *name,
127                        const char *apiobj, const char *libs)
128 {
129         ok(build(dir, name, "", libs, 0) == 0, "%s %s", t->name, name);
130 }
131
132 /* api tests get the API obj linked in as well. */
133 static void compile_api_ok(const char *dir, struct test_type *t,
134                            const char *name, const char *apiobj,
135                            const char *libs)
136 {
137         ok(build(dir, name, apiobj, libs, 0) == 0, "%s %s", t->name, name);
138 }
139
140 static void compile_fail(const char *dir, struct test_type *t, const char *name,
141                          const char *apiobj, const char *libs)
142 {
143         if (build(dir, name, "", libs, 0) != 0)
144                 fail("non-FAIL build %s", name);
145         else
146                 ok(build(dir, name, "", libs, 1) > 0, "%s %s", t->name, name);
147 }
148
149 static void no_run(const char *name)
150 {
151 }
152
153 static void run(const char *name)
154 {
155         if (system(output_name(name)) != 0)
156                 fail("running %s had error", name);
157 }
158
159 static void cleanup(const char *name)
160 {
161         unlink(output_name(name));
162 }
163
164 static struct test_type test_types[] = {
165         { "compile_ok", compile_ok, no_run },
166         { "compile_fail", compile_fail, no_run },
167         { "run", compile_ok, run },
168         { "api", compile_api_ok, run },
169 };
170
171 int main(int argc, char *argv[])
172 {
173         DIR *dir;
174         struct dirent *d;
175         char *testdir, *cwd;
176         const char *apiobj = "";
177         char *libs = talloc_strdup(NULL, "");
178         struct test *test;
179         unsigned int num_tests = 0, num_objs = 0, i;
180
181         if (argc > 1 && streq(argv[1], "--verbose")) {
182                 verbose = 1;
183                 argc--;
184                 argv++;
185         }
186
187         while (argc > 1 && strstarts(argv[1], "--lib=")) {
188                 libs = talloc_asprintf_append(libs, " -l%s",
189                                               argv[1] + strlen("--lib="));
190                 argc--;
191                 argv++;
192         }
193
194         if (argc > 1 && strstarts(argv[1], "--apiobj=")) {
195                 apiobj = argv[1] + strlen("--apiobj=");
196                 argc--;
197                 argv++;
198         }
199
200         if (argc < 2)
201                 errx(1, "Usage: run_tests [--verbose] [--apiobj=<obj>] <dir> [<extra-objs>...]");
202
203         testdir = talloc_asprintf(NULL, "%s/test", argv[1]);
204         dir = opendir(testdir);
205         if (!dir)
206                 err(1, "Opening '%s'", testdir);
207
208         while ((d = readdir(dir)) != NULL) {
209                 if (d->d_name[0] == '.' || !strends(d->d_name, ".c"))
210                         continue;
211
212                 for (i = 0; i < ARRAY_SIZE(test_types); i++) {
213                         if (strstarts(d->d_name, test_types[i].name)) {
214                                 add_test(testdir, d->d_name, &test_types[i]);
215                                 num_tests++;
216                                 break;
217                         }
218                 }
219                 if (i == ARRAY_SIZE(test_types)) {
220                         add_obj(testdir, d->d_name, true);
221                         num_objs++;
222                 }
223         }
224
225         plan_tests(num_tests + num_objs + (num_objs ? 1 : 0));
226         /* First all the extra object compilations. */
227         compile_objs();
228
229         /* Now add any object files from the command line */
230         cwd = talloc_strdup(testdir, ".");
231         for (i = 2; i < argc; i++)
232                 add_obj(cwd, argv[i], false);
233
234         /* Do all the test compilations. */
235         for (test = tests; test; test = test->next)
236                 test->type->buildfn(argv[1], test->type, test->name,
237                                     apiobj, libs);
238
239         cleanup_objs();
240
241         /* Now run all the ones which wanted to run. */
242         for (test = tests; test; test = test->next) {
243                 test->type->runfn(test->name);
244                 cleanup(test->name);
245         }
246
247         exit(exit_status());
248 }