run_tests.c 4.4 KB

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