run_tests.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "ccan/tap/tap.h"
  8. #include "ccan/talloc/talloc.h"
  9. #include "ccan/string/string.h"
  10. #include "tools.h"
  11. /* FIXME: Use build bug later. */
  12. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  13. static struct test *tests = NULL;
  14. static struct obj *objs = NULL;
  15. static int verbose;
  16. struct test_type
  17. {
  18. const char *name;
  19. void (*buildfn)(const char *dir, struct test_type *t, const char *name);
  20. void (*runfn)(const char *name);
  21. };
  22. struct test
  23. {
  24. struct test *next;
  25. struct test_type *type;
  26. char *name;
  27. };
  28. struct obj
  29. {
  30. struct obj *next;
  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. unlink(talloc_asprintf(i, "%s.o", output_name(i->name)));
  66. }
  67. static void add_test(const char *testdir, const char *name, struct test_type *t)
  68. {
  69. struct test *test = talloc(testdir, struct test);
  70. test->next = tests;
  71. test->type = t;
  72. test->name = talloc_asprintf(test, "%s/%s", testdir, name);
  73. tests = test;
  74. }
  75. static void add_obj(const char *testdir, const char *name)
  76. {
  77. struct obj *obj = talloc(testdir, struct obj);
  78. obj->next = objs;
  79. obj->name = talloc_asprintf(obj, "%s/%s", testdir, name);
  80. objs = obj;
  81. }
  82. static int build(const char *dir, const char *name, int fail)
  83. {
  84. const char *cmd;
  85. int ret;
  86. char *externals = talloc_strdup(name, "");
  87. char **deps;
  88. for (deps = get_deps(objs, dir); *deps; deps++) {
  89. if (!strstarts(*deps, "ccan/"))
  90. continue;
  91. /* ccan/foo -> ccan/foo.o */
  92. externals = talloc_asprintf_append(externals, " %s.o", *deps);
  93. }
  94. cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s%s%s",
  95. fail ? "-DFAIL" : "",
  96. output_name(name), name, obj_list(), externals,
  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. {
  107. ok(build(dir, name, 0) == 0, "%s %s", t->name, name);
  108. }
  109. static void compile_fail(const char *dir, struct test_type *t, const char *name)
  110. {
  111. if (build(dir, name, 0) != 0)
  112. fail("non-FAIL build %s", name);
  113. else
  114. ok(build(dir, name, 1) > 0, "%s %s", t->name, name);
  115. }
  116. static void no_run(const char *name)
  117. {
  118. }
  119. static void run(const char *name)
  120. {
  121. if (system(output_name(name)) == -1)
  122. fail("running %s had error %m", name);
  123. }
  124. static void cleanup(const char *name)
  125. {
  126. unlink(output_name(name));
  127. }
  128. static struct test_type test_types[] = {
  129. { "compile_ok", compile_ok, no_run },
  130. { "compile_fail", compile_fail, no_run },
  131. { "run", compile_ok, run },
  132. { "api", compile_ok, run },
  133. };
  134. int main(int argc, char *argv[])
  135. {
  136. DIR *dir;
  137. struct dirent *d;
  138. char *testdir;
  139. struct test *test;
  140. unsigned int num_tests = 0, num_objs = 0;
  141. if (argc > 1 && streq(argv[1], "--verbose")) {
  142. verbose = 1;
  143. argc--;
  144. argv++;
  145. }
  146. if (argc != 2)
  147. errx(1, "Usage: run_tests [--verbose] <dir>");
  148. testdir = talloc_asprintf(NULL, "%s/test", argv[1]);
  149. dir = opendir(testdir);
  150. if (!dir)
  151. err(1, "Opening '%s'", testdir);
  152. while ((d = readdir(dir)) != NULL) {
  153. unsigned int i;
  154. if (d->d_name[0] == '.' || !strends(d->d_name, ".c"))
  155. continue;
  156. for (i = 0; i < ARRAY_SIZE(test_types); i++) {
  157. if (strstarts(d->d_name, test_types[i].name)) {
  158. add_test(testdir, d->d_name, &test_types[i]);
  159. num_tests++;
  160. break;
  161. }
  162. }
  163. if (i == ARRAY_SIZE(test_types)) {
  164. add_obj(testdir, d->d_name);
  165. num_objs++;
  166. }
  167. }
  168. plan_tests(num_tests + num_objs + (num_objs ? 1 : 0));
  169. /* First all the extra object compilations. */
  170. compile_objs();
  171. /* Do all the test compilations. */
  172. for (test = tests; test; test = test->next)
  173. test->type->buildfn(argv[1], test->type, test->name);
  174. cleanup_objs();
  175. /* Now run all the ones which wanted to run. */
  176. for (test = tests; test; test = test->next) {
  177. test->type->runfn(test->name);
  178. cleanup(test->name);
  179. }
  180. exit(exit_status());
  181. }