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