run_tests.c 5.1 KB

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