run_tests.c 4.4 KB

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