X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Frun_tests.c;h=e50d1910a210596f09832a691c36267b492d33a2;hp=a1fbc614918851e50657adb8bfa53caeba695297;hb=c1daa044b22fce3ca80d3430e3e1ad9360f8a4f1;hpb=16b7eb13fbcb1a04a71622e6310020baccc3c39c diff --git a/tools/run_tests.c b/tools/run_tests.c index a1fbc614..e50d1910 100644 --- a/tools/run_tests.c +++ b/tools/run_tests.c @@ -4,34 +4,34 @@ #include #include #include +#include +#include #include "ccan/tap/tap.h" #include "ccan/talloc/talloc.h" -#include "ccan/string/string.h" +#include "ccan/str/str.h" +#include "ccan/array_size/array_size.h" #include "tools.h" -/* FIXME: Use build bug later. */ -#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) - static struct test *tests = NULL; static struct obj *objs = NULL; static int verbose; -struct test_type -{ +struct test_type { const char *name; - void (*testfn)(const char *dir, struct test_type *t, const char *name); + void (*buildfn)(const char *dir, struct test_type *t, const char *name, + const char *apiobj, const char *libs); + void (*runfn)(const char *name); }; -struct test -{ +struct test { struct test *next; struct test_type *type; char *name; }; -struct obj -{ +struct obj { struct obj *next; + bool generate; char *name; }; @@ -75,8 +75,11 @@ static void cleanup_objs(void) { struct obj *i; - for (i = objs; i; i = i->next) + for (i = objs; i; i = i->next) { + if (!i->generate) + continue; unlink(talloc_asprintf(i, "%s.o", output_name(i->name))); + } } static void add_test(const char *testdir, const char *name, struct test_type *t) @@ -89,33 +92,25 @@ static void add_test(const char *testdir, const char *name, struct test_type *t) tests = test; } -static void add_obj(const char *testdir, const char *name) +static void add_obj(const char *testdir, const char *name, bool generate) { struct obj *obj = talloc(testdir, struct obj); obj->next = objs; obj->name = talloc_asprintf(obj, "%s/%s", testdir, name); + obj->generate = generate; objs = obj; } -static int build(const char *dir, const char *name, int fail) +static int build(const char *dir, const char *name, const char *apiobj, + const char *libs, int fail) { const char *cmd; int ret; - char *externals = talloc_strdup(name, ""); - char **deps; - - for (deps = get_deps(objs, dir); *deps; deps++) { - if (!strstarts(*deps, "ccan/")) - continue; - /* ccan/foo -> ccan/foo.o */ - externals = talloc_asprintf_append(externals, " %s.o", *deps); - } - - cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s%s%s", + cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s %s%s %s", fail ? "-DFAIL" : "", - output_name(name), name, obj_list(), externals, + output_name(name), name, apiobj, obj_list(), libs, verbose ? "" : "> /dev/null 2>&1"); if (verbose) @@ -128,23 +123,37 @@ static int build(const char *dir, const char *name, int fail) return ret; } -static void compile_ok(const char *dir, struct test_type *t, const char *name) +static void compile_ok(const char *dir, struct test_type *t, const char *name, + const char *apiobj, const char *libs) { - ok(build(dir, name, 0) == 0, "%s %s", t->name, name); + ok(build(dir, name, "", libs, 0) == 0, "%s %s", t->name, name); } -static void compile_fail(const char *dir, struct test_type *t, const char *name) +/* api tests get the API obj linked in as well. */ +static void compile_api_ok(const char *dir, struct test_type *t, + const char *name, const char *apiobj, + const char *libs) { - if (build(dir, name, 0) != 0) + ok(build(dir, name, apiobj, libs, 0) == 0, "%s %s", t->name, name); +} + +static void compile_fail(const char *dir, struct test_type *t, const char *name, + const char *apiobj, const char *libs) +{ + if (build(dir, name, "", libs, 0) != 0) fail("non-FAIL build %s", name); else - ok(build(dir, name, 1) > 0, "%s %s", t->name, name); + ok(build(dir, name, "", libs, 1) > 0, "%s %s", t->name, name); +} + +static void no_run(const char *name) +{ } static void run(const char *name) { - if (system(output_name(name)) == -1) - fail("running %s had error %m", name); + if (system(output_name(name)) != 0) + fail("running %s had error", name); } static void cleanup(const char *name) @@ -153,18 +162,21 @@ static void cleanup(const char *name) } static struct test_type test_types[] = { - { "compile_ok", compile_ok }, - { "compile_fail", compile_fail }, - { "run", compile_ok }, + { "compile_ok", compile_ok, no_run }, + { "compile_fail", compile_fail, no_run }, + { "run", compile_ok, run }, + { "api", compile_api_ok, run }, }; int main(int argc, char *argv[]) { DIR *dir; struct dirent *d; - char *testdir; + char *testdir, *cwd; + const char *apiobj = ""; + char *libs = talloc_strdup(NULL, ""); struct test *test; - unsigned int num_tests = 0, num_objs = 0; + unsigned int num_tests = 0, num_objs = 0, i; if (argc > 1 && streq(argv[1], "--verbose")) { verbose = 1; @@ -172,8 +184,21 @@ int main(int argc, char *argv[]) argv++; } - if (argc != 2) - errx(1, "Usage: run_tests [--verbose] "); + while (argc > 1 && strstarts(argv[1], "--lib=")) { + libs = talloc_asprintf_append(libs, " -l%s", + argv[1] + strlen("--lib=")); + argc--; + argv++; + } + + if (argc > 1 && strstarts(argv[1], "--apiobj=")) { + apiobj = argv[1] + strlen("--apiobj="); + argc--; + argv++; + } + + if (argc < 2) + errx(1, "Usage: run_tests [--verbose] [--apiobj=] [...]"); testdir = talloc_asprintf(NULL, "%s/test", argv[1]); dir = opendir(testdir); @@ -181,7 +206,6 @@ int main(int argc, char *argv[]) err(1, "Opening '%s'", testdir); while ((d = readdir(dir)) != NULL) { - unsigned int i; if (d->d_name[0] == '.' || !strends(d->d_name, ".c")) continue; @@ -193,7 +217,7 @@ int main(int argc, char *argv[]) } } if (i == ARRAY_SIZE(test_types)) { - add_obj(testdir, d->d_name); + add_obj(testdir, d->d_name, true); num_objs++; } } @@ -202,16 +226,21 @@ int main(int argc, char *argv[]) /* First all the extra object compilations. */ compile_objs(); + /* Now add any object files from the command line */ + cwd = talloc_strdup(testdir, "."); + for (i = 2; i < argc; i++) + add_obj(cwd, argv[i], false); + /* Do all the test compilations. */ for (test = tests; test; test = test->next) - test->type->testfn(argv[1], test->type, test->name); + test->type->buildfn(argv[1], test->type, test->name, + apiobj, libs); cleanup_objs(); /* Now run all the ones which wanted to run. */ for (test = tests; test; test = test->next) { - if (streq(test->type->name, "run")) - run(test->name); + test->type->runfn(test->name); cleanup(test->name); }