run_tests.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include <err.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <dirent.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <sys/stat.h>
  8. #include <errno.h>
  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"
  13. #include "tools.h"
  14. static struct test *tests = NULL;
  15. static struct obj *objs = NULL;
  16. static int verbose;
  17. struct test_type {
  18. const char *name;
  19. void (*buildfn)(const char *dir, struct test_type *t, const char *name,
  20. const char *apiobj, const char *libs);
  21. void (*runfn)(const char *name);
  22. };
  23. struct test {
  24. struct test *next;
  25. struct test_type *type;
  26. char *name;
  27. };
  28. struct obj {
  29. struct obj *next;
  30. bool generate;
  31. char *name;
  32. };
  33. static char *output_name(const char *name)
  34. {
  35. char *ret;
  36. assert(strends(name, ".c"));
  37. ret = talloc_strdup(name, name);
  38. ret[strlen(ret) - 2] = '\0';
  39. return ret;
  40. }
  41. static char *obj_list(void)
  42. {
  43. char *list = talloc_strdup(objs, "");
  44. struct obj *i;
  45. for (i = objs; i; i = i->next)
  46. list = talloc_asprintf_append(list, "%s ", i->name);
  47. /* FIXME */
  48. list = talloc_asprintf_append(list, "ccan/tap/tap.o");
  49. return list;
  50. }
  51. static void compile_objs(void)
  52. {
  53. struct obj *i;
  54. for (i = objs; i; i = i->next) {
  55. char *cmd = talloc_asprintf(i, "gcc " CFLAGS " -o %s.o -c %s%s",
  56. output_name(i->name), i->name,
  57. verbose ? "" : "> /dev/null 2>&1");
  58. ok(system(cmd) == 0, "%s", cmd);
  59. }
  60. }
  61. static void cleanup_objs(void)
  62. {
  63. struct obj *i;
  64. for (i = objs; i; i = i->next) {
  65. if (!i->generate)
  66. continue;
  67. unlink(talloc_asprintf(i, "%s.o", output_name(i->name)));
  68. }
  69. }
  70. static void add_test(const char *testdir, const char *name, struct test_type *t)
  71. {
  72. struct test *test = talloc(testdir, struct test);
  73. test->next = tests;
  74. test->type = t;
  75. test->name = talloc_asprintf(test, "%s/%s", testdir, name);
  76. tests = test;
  77. }
  78. static void add_obj(const char *testdir, const char *name, bool generate)
  79. {
  80. struct obj *obj = talloc(testdir, struct obj);
  81. obj->next = objs;
  82. obj->name = talloc_asprintf(obj, "%s/%s", testdir, name);
  83. obj->generate = generate;
  84. objs = obj;
  85. }
  86. static int build(const char *dir, const char *name, const char *apiobj,
  87. const char *libs, int fail)
  88. {
  89. const char *cmd;
  90. int ret;
  91. cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s %s%s %s",
  92. fail ? "-DFAIL" : "",
  93. output_name(name), name, apiobj, obj_list(), libs,
  94. verbose ? "" : "> /dev/null 2>&1");
  95. if (verbose)
  96. fprintf(stderr, "Running %s\n", cmd);
  97. ret = system(cmd);
  98. if (ret == -1)
  99. diag("cmd '%s' failed to execute", cmd);
  100. return ret;
  101. }
  102. static void compile_ok(const char *dir, struct test_type *t, const char *name,
  103. const char *apiobj, const char *libs)
  104. {
  105. ok(build(dir, name, "", libs, 0) == 0, "%s %s", t->name, name);
  106. }
  107. /* api tests get the API obj linked in as well. */
  108. static void compile_api_ok(const char *dir, struct test_type *t,
  109. const char *name, const char *apiobj,
  110. const char *libs)
  111. {
  112. ok(build(dir, name, apiobj, libs, 0) == 0, "%s %s", t->name, name);
  113. }
  114. static void compile_fail(const char *dir, struct test_type *t, const char *name,
  115. const char *apiobj, const char *libs)
  116. {
  117. if (build(dir, name, "", libs, 0) != 0)
  118. fail("non-FAIL build %s", name);
  119. else
  120. ok(build(dir, name, "", libs, 1) > 0, "%s %s", t->name, name);
  121. }
  122. static void no_run(const char *name)
  123. {
  124. }
  125. static void run(const char *name)
  126. {
  127. if (system(output_name(name)) != 0)
  128. fail("running %s had error", name);
  129. }
  130. static void cleanup(const char *name)
  131. {
  132. unlink(output_name(name));
  133. }
  134. static struct test_type test_types[] = {
  135. { "compile_ok", compile_ok, no_run },
  136. { "compile_fail", compile_fail, no_run },
  137. { "run", compile_ok, run },
  138. { "api", compile_api_ok, run },
  139. };
  140. int main(int argc, char *argv[])
  141. {
  142. DIR *dir;
  143. struct dirent *d;
  144. char *testdir, *cwd;
  145. const char *apiobj = "";
  146. char *libs = talloc_strdup(NULL, "");
  147. struct test *test;
  148. unsigned int num_tests = 0, num_objs = 0, i;
  149. if (argc > 1 && streq(argv[1], "--verbose")) {
  150. verbose = 1;
  151. argc--;
  152. argv++;
  153. }
  154. while (argc > 1 && strstarts(argv[1], "--lib=")) {
  155. libs = talloc_asprintf_append(libs, " -l%s",
  156. argv[1] + strlen("--lib="));
  157. argc--;
  158. argv++;
  159. }
  160. if (argc > 1 && strstarts(argv[1], "--apiobj=")) {
  161. apiobj = argv[1] + strlen("--apiobj=");
  162. argc--;
  163. argv++;
  164. }
  165. if (argc < 2)
  166. errx(1, "Usage: run_tests [--verbose] [--apiobj=<obj>] <dir> [<extra-objs>...]");
  167. testdir = talloc_asprintf(NULL, "%s/test", argv[1]);
  168. dir = opendir(testdir);
  169. if (!dir)
  170. err(1, "Opening '%s'", testdir);
  171. while ((d = readdir(dir)) != NULL) {
  172. if (d->d_name[0] == '.' || !strends(d->d_name, ".c"))
  173. continue;
  174. for (i = 0; i < ARRAY_SIZE(test_types); i++) {
  175. if (strstarts(d->d_name, test_types[i].name)) {
  176. add_test(testdir, d->d_name, &test_types[i]);
  177. num_tests++;
  178. break;
  179. }
  180. }
  181. if (i == ARRAY_SIZE(test_types)) {
  182. add_obj(testdir, d->d_name, true);
  183. num_objs++;
  184. }
  185. }
  186. plan_tests(num_tests + num_objs + (num_objs ? 1 : 0));
  187. /* First all the extra object compilations. */
  188. compile_objs();
  189. /* Now add any object files from the command line */
  190. cwd = talloc_strdup(testdir, ".");
  191. for (i = 2; i < argc; i++)
  192. add_obj(cwd, argv[i], false);
  193. /* Do all the test compilations. */
  194. for (test = tests; test; test = test->next)
  195. test->type->buildfn(argv[1], test->type, test->name,
  196. apiobj, libs);
  197. cleanup_objs();
  198. /* Now run all the ones which wanted to run. */
  199. for (test = tests; test; test = test->next) {
  200. test->type->runfn(test->name);
  201. cleanup(test->name);
  202. }
  203. exit(exit_status());
  204. }