7 #include "libtap/src/tap.h"
8 #include "talloc/talloc.h"
9 #include "../string/string.h"
11 #define CFLAGS "-O3 -Wall -Wundef -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Werror -I. -Iccan_tools/libtap/src/"
13 /* FIXME: Use build bug later. */
14 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
16 static struct test *tests = NULL;
17 static struct obj *objs = NULL;
23 void (*testfn)(struct test_type *t, const char *name);
29 struct test_type *type;
39 static char *output_name(const char *name)
43 assert(strends(name, ".c"));
45 ret = talloc_strdup(name, name);
46 ret[strlen(ret) - 2] = '\0';
50 static char *obj_list(void)
52 char *list = talloc_strdup(objs, "");
55 for (i = objs; i; i = i->next)
56 list = talloc_asprintf_append(list, "%s ", i->name);
59 list = talloc_asprintf_append(list, "ccan_tools/libtap/src/tap.o");
63 static void compile_objs(void)
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);
75 static void cleanup_objs(void)
79 for (i = objs; i; i = i->next)
80 unlink(talloc_asprintf(i, "%s.o", output_name(i->name)));
83 static void add_test(const char *testdir, const char *name, struct test_type *t)
85 struct test *test = talloc(testdir, struct test);
89 test->name = talloc_asprintf(test, "%s/%s", testdir, name);
93 static void add_obj(const char *testdir, const char *name)
95 struct obj *obj = talloc(testdir, struct obj);
98 obj->name = talloc_asprintf(obj, "%s/%s", testdir, name);
102 static int build(const char *name, int fail)
107 cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s%s",
108 fail ? "-DFAIL" : "",
109 output_name(name), name, obj_list(),
110 verbose ? "" : "> /dev/null 2>&1");
113 fprintf(stderr, "Running %s\n", cmd);
117 diag("cmd '%s' failed to execute", cmd);
122 static void compile_ok(struct test_type *t, const char *name)
124 ok(build(name, 0) == 0, "%s %s", t->name, name);
127 static void compile_fail(struct test_type *t, const char *name)
129 if (build(name, 0) != 0)
130 fail("non-FAIL build %s", name);
132 ok(build(name, 1) > 0, "%s %s", t->name, name);
135 static void run(const char *name)
137 if (system(output_name(name)) == -1)
138 fail("running %s had error %m", name);
141 static void cleanup(const char *name)
143 unlink(output_name(name));
146 static struct test_type test_types[] = {
147 { "compile_ok", compile_ok },
148 { "compile_fail", compile_fail },
149 { "run", compile_ok },
152 int main(int argc, char *argv[])
158 unsigned int num_tests = 0, num_objs = 0;
160 if (argc > 1 && streq(argv[1], "--verbose")) {
167 errx(1, "Usage: run_tests [--verbose] <dir>");
169 testdir = talloc_asprintf(NULL, "%s/test", argv[1]);
170 dir = opendir(testdir);
172 err(1, "Opening '%s'", testdir);
174 while ((d = readdir(dir)) != NULL) {
176 if (d->d_name[0] == '.' || !strends(d->d_name, ".c"))
179 for (i = 0; i < ARRAY_SIZE(test_types); i++) {
180 if (strstarts(d->d_name, test_types[i].name)) {
181 add_test(testdir, d->d_name, &test_types[i]);
186 if (i == ARRAY_SIZE(test_types)) {
187 add_obj(testdir, d->d_name);
192 plan_tests(num_tests + num_objs);
193 /* First all the extra object compilations. */
196 /* Do all the test compilations. */
197 for (test = tests; test; test = test->next)
198 test->type->testfn(test->type, test->name);
202 /* Now run all the ones which wanted to run. */
203 for (test = tests; test; test = test->next) {
204 if (streq(test->type->name, "run"))