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"
15 static struct test *tests = NULL;
16 static struct obj *objs = NULL;
21 void (*buildfn)(const char *dir, struct test_type *t, const char *name,
23 void (*runfn)(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) {
81 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, bool generate)
97 struct obj *obj = talloc(testdir, struct obj);
100 obj->name = talloc_asprintf(obj, "%s/%s", testdir, name);
101 obj->generate = generate;
105 static int build(const char *dir, const char *name, const char *apiobj,
111 cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s %s %s",
112 fail ? "-DFAIL" : "",
113 output_name(name), name, apiobj, obj_list(),
114 verbose ? "" : "> /dev/null 2>&1");
117 fprintf(stderr, "Running %s\n", cmd);
121 diag("cmd '%s' failed to execute", cmd);
126 static void compile_ok(const char *dir, struct test_type *t, const char *name,
129 ok(build(dir, name, "", 0) == 0, "%s %s", t->name, name);
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)
136 ok(build(dir, name, apiobj, 0) == 0, "%s %s", t->name, name);
139 static void compile_fail(const char *dir, struct test_type *t, const char *name,
142 if (build(dir, name, "", 0) != 0)
143 fail("non-FAIL build %s", name);
145 ok(build(dir, name, "", 1) > 0, "%s %s", t->name, name);
148 static void no_run(const char *name)
152 static void run(const char *name)
154 if (system(output_name(name)) != 0)
155 fail("running %s had error", name);
158 static void cleanup(const char *name)
160 unlink(output_name(name));
163 static struct test_type test_types[] = {
164 { "compile_ok", compile_ok, no_run },
165 { "compile_fail", compile_fail, no_run },
166 { "run", compile_ok, run },
167 { "api", compile_api_ok, run },
170 int main(int argc, char *argv[])
175 const char *apiobj = "";
177 unsigned int num_tests = 0, num_objs = 0, i;
179 if (argc > 1 && streq(argv[1], "--verbose")) {
185 if (argc > 1 && strstarts(argv[1], "--apiobj=")) {
186 apiobj = argv[1] + strlen("--apiobj=");
192 errx(1, "Usage: run_tests [--verbose] [--apiobj=<obj>] <dir> [<extra-objs>...]");
194 testdir = talloc_asprintf(NULL, "%s/test", argv[1]);
195 dir = opendir(testdir);
197 err(1, "Opening '%s'", testdir);
199 while ((d = readdir(dir)) != NULL) {
200 if (d->d_name[0] == '.' || !strends(d->d_name, ".c"))
203 for (i = 0; i < ARRAY_SIZE(test_types); i++) {
204 if (strstarts(d->d_name, test_types[i].name)) {
205 add_test(testdir, d->d_name, &test_types[i]);
210 if (i == ARRAY_SIZE(test_types)) {
211 add_obj(testdir, d->d_name, true);
216 plan_tests(num_tests + num_objs + (num_objs ? 1 : 0));
217 /* First all the extra object compilations. */
220 /* Now add any object files from the command line */
221 cwd = talloc_strdup(testdir, ".");
222 for (i = 2; i < argc; i++)
223 add_obj(cwd, argv[i], false);
225 /* Do all the test compilations. */
226 for (test = tests; test; test = test->next)
227 test->type->buildfn(argv[1], test->type, test->name, apiobj);
231 /* Now run all the ones which wanted to run. */
232 for (test = tests; test; test = test->next) {
233 test->type->runfn(test->name);