7 #include "ccan/tap/tap.h"
8 #include "ccan/talloc/talloc.h"
9 #include "ccan/string/string.h"
12 /* FIXME: Use build bug later. */
13 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
15 static struct test *tests = NULL;
16 static struct obj *objs = NULL;
22 void (*testfn)(struct test_type *t, const char *name);
28 struct test_type *type;
38 static char *output_name(const char *name)
42 assert(strends(name, ".c"));
44 ret = talloc_strdup(name, name);
45 ret[strlen(ret) - 2] = '\0';
49 static char *obj_list(void)
51 char *list = talloc_strdup(objs, "");
54 for (i = objs; i; i = i->next)
55 list = talloc_asprintf_append(list, "%s ", i->name);
58 list = talloc_asprintf_append(list, "ccan/tap/tap.o");
62 static void compile_objs(void)
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);
74 static void cleanup_objs(void)
78 for (i = objs; i; i = i->next)
79 unlink(talloc_asprintf(i, "%s.o", output_name(i->name)));
82 static void add_test(const char *testdir, const char *name, struct test_type *t)
84 struct test *test = talloc(testdir, struct test);
88 test->name = talloc_asprintf(test, "%s/%s", testdir, name);
92 static void add_obj(const char *testdir, const char *name)
94 struct obj *obj = talloc(testdir, struct obj);
97 obj->name = talloc_asprintf(obj, "%s/%s", testdir, name);
101 static int build(const char *name, int fail)
106 cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s%s",
107 fail ? "-DFAIL" : "",
108 output_name(name), name, obj_list(),
109 verbose ? "" : "> /dev/null 2>&1");
112 fprintf(stderr, "Running %s\n", cmd);
116 diag("cmd '%s' failed to execute", cmd);
121 static void compile_ok(struct test_type *t, const char *name)
123 ok(build(name, 0) == 0, "%s %s", t->name, name);
126 static void compile_fail(struct test_type *t, const char *name)
128 if (build(name, 0) != 0)
129 fail("non-FAIL build %s", name);
131 ok(build(name, 1) > 0, "%s %s", t->name, name);
134 static void run(const char *name)
136 if (system(output_name(name)) == -1)
137 fail("running %s had error %m", name);
140 static void cleanup(const char *name)
142 unlink(output_name(name));
145 static struct test_type test_types[] = {
146 { "compile_ok", compile_ok },
147 { "compile_fail", compile_fail },
148 { "run", compile_ok },
151 int main(int argc, char *argv[])
157 unsigned int num_tests = 0, num_objs = 0;
159 if (argc > 1 && streq(argv[1], "--verbose")) {
166 errx(1, "Usage: run_tests [--verbose] <dir>");
168 testdir = talloc_asprintf(NULL, "%s/test", argv[1]);
169 dir = opendir(testdir);
171 err(1, "Opening '%s'", testdir);
173 while ((d = readdir(dir)) != NULL) {
175 if (d->d_name[0] == '.' || !strends(d->d_name, ".c"))
178 for (i = 0; i < ARRAY_SIZE(test_types); i++) {
179 if (strstarts(d->d_name, test_types[i].name)) {
180 add_test(testdir, d->d_name, &test_types[i]);
185 if (i == ARRAY_SIZE(test_types)) {
186 add_obj(testdir, d->d_name);
191 plan_tests(num_tests + num_objs);
192 /* First all the extra object compilations. */
195 /* Do all the test compilations. */
196 for (test = tests; test; test = test->next)
197 test->type->testfn(test->type, test->name);
201 /* Now run all the ones which wanted to run. */
202 for (test = tests; test; test = test->next) {
203 if (streq(test->type->name, "run"))