9 #include "ccan/tap/tap.h"
10 #include "ccan/talloc/talloc.h"
11 #include "ccan/str/str.h"
14 /* FIXME: Use build bug later. */
15 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
17 static struct test *tests = NULL;
18 static struct obj *objs = NULL;
24 void (*buildfn)(const char *dir, struct test_type *t, const char *name);
25 void (*runfn)(const char *name);
31 struct test_type *type;
41 static char *output_name(const char *name)
45 assert(strends(name, ".c"));
47 ret = talloc_strdup(name, name);
48 ret[strlen(ret) - 2] = '\0';
52 static char *obj_list(void)
54 char *list = talloc_strdup(objs, "");
57 for (i = objs; i; i = i->next)
58 list = talloc_asprintf_append(list, "%s ", i->name);
61 list = talloc_asprintf_append(list, "ccan/tap/tap.o");
65 static void compile_objs(void)
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);
77 static void cleanup_objs(void)
81 for (i = objs; i; i = i->next)
82 unlink(talloc_asprintf(i, "%s.o", output_name(i->name)));
85 static void add_test(const char *testdir, const char *name, struct test_type *t)
87 struct test *test = talloc(testdir, struct test);
91 test->name = talloc_asprintf(test, "%s/%s", testdir, name);
95 static void add_obj(const char *testdir, const char *name)
97 struct obj *obj = talloc(testdir, struct obj);
100 obj->name = talloc_asprintf(obj, "%s/%s", testdir, name);
104 static int build(const char *dir, const char *name, int fail)
109 cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s -L. -lccan %s",
110 fail ? "-DFAIL" : "",
111 output_name(name), name, obj_list(),
112 verbose ? "" : "> /dev/null 2>&1");
115 fprintf(stderr, "Running %s\n", cmd);
119 diag("cmd '%s' failed to execute", cmd);
124 static void compile_ok(const char *dir, struct test_type *t, const char *name)
126 ok(build(dir, name, 0) == 0, "%s %s", t->name, name);
129 static void compile_fail(const char *dir, struct test_type *t, const char *name)
131 if (build(dir, name, 0) != 0)
132 fail("non-FAIL build %s", name);
134 ok(build(dir, name, 1) > 0, "%s %s", t->name, name);
137 static void no_run(const char *name)
141 static void run(const char *name)
143 if (system(output_name(name)) != 0)
144 fail("running %s had error", name);
147 static void cleanup(const char *name)
149 unlink(output_name(name));
152 static struct test_type test_types[] = {
153 { "compile_ok", compile_ok, no_run },
154 { "compile_fail", compile_fail, no_run },
155 { "run", compile_ok, run },
156 { "api", compile_ok, run },
159 int main(int argc, char *argv[])
165 unsigned int num_tests = 0, num_objs = 0;
167 if (argc > 1 && streq(argv[1], "--verbose")) {
174 errx(1, "Usage: run_tests [--verbose] <dir>");
176 testdir = talloc_asprintf(NULL, "%s/test", argv[1]);
177 dir = opendir(testdir);
179 err(1, "Opening '%s'", testdir);
181 while ((d = readdir(dir)) != NULL) {
183 if (d->d_name[0] == '.' || !strends(d->d_name, ".c"))
186 for (i = 0; i < ARRAY_SIZE(test_types); i++) {
187 if (strstarts(d->d_name, test_types[i].name)) {
188 add_test(testdir, d->d_name, &test_types[i]);
193 if (i == ARRAY_SIZE(test_types)) {
194 add_obj(testdir, d->d_name);
199 plan_tests(num_tests + num_objs + (num_objs ? 1 : 0));
200 /* First all the extra object compilations. */
203 /* Do all the test compilations. */
204 for (test = tests; test; test = test->next)
205 test->type->buildfn(argv[1], test->type, test->name);
209 /* Now run all the ones which wanted to run. */
210 for (test = tests; test; test = test->next) {
211 test->type->runfn(test->name);