run_tests.c 4.6 KB

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