tests_pass.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <ccan/foreach/foreach.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <limits.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <err.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include "tests_pass.h"
  18. bool do_valgrind = false;
  19. static const char *can_run(struct manifest *m)
  20. {
  21. unsigned int timeleft = default_timeout_ms;
  22. char *output;
  23. if (safe_mode)
  24. return "Safe mode enabled";
  25. if (!is_excluded("tests_pass_valgrind")
  26. && run_command(m, &timeleft, &output,
  27. "valgrind -q true"))
  28. do_valgrind = true;
  29. return NULL;
  30. }
  31. static const char *concat(struct score *score, char *bits[])
  32. {
  33. unsigned int i;
  34. char *ret = talloc_strdup(score, "");
  35. for (i = 0; bits[i]; i++) {
  36. if (i)
  37. ret = talloc_append_string(ret, " ");
  38. ret = talloc_append_string(ret, bits[i]);
  39. }
  40. return ret;
  41. }
  42. static bool run_test(void *ctx,
  43. struct manifest *m,
  44. unsigned int *timeleft, char **cmdout,
  45. struct ccan_file *i,
  46. bool use_valgrind)
  47. {
  48. if (use_valgrind) {
  49. const char *options;
  50. options = concat(ctx,
  51. per_file_options(&tests_pass_valgrind, i));
  52. if (!streq(options, "FAIL")) {
  53. /* FIXME: Valgrind's output sucks. XML is
  54. * unreadable by humans *and* doesn't support
  55. * children reporting. */
  56. i->valgrind_log = talloc_asprintf(m,
  57. "%s.valgrind-log",
  58. i->compiled);
  59. talloc_set_destructor(i->valgrind_log,
  60. unlink_file_destructor);
  61. return run_command(ctx, timeleft, cmdout,
  62. "valgrind -q"
  63. " --leak-check=full"
  64. " --log-fd=3 %s %s"
  65. " 3> %s",
  66. options,
  67. i->compiled, i->valgrind_log);
  68. }
  69. }
  70. return run_command(m, timeleft, cmdout, "%s", i->compiled);
  71. }
  72. static void run_tests(struct manifest *m,
  73. bool keep,
  74. unsigned int *timeleft,
  75. struct score *score,
  76. bool use_valgrind)
  77. {
  78. struct list_head *list;
  79. struct ccan_file *i;
  80. char *cmdout;
  81. score->total = 0;
  82. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  83. list_for_each(list, i, list) {
  84. score->total++;
  85. if (run_test(score, m, timeleft, &cmdout, i,
  86. use_valgrind))
  87. score->score++;
  88. else
  89. score_file_error(score, i, 0, "%s", cmdout);
  90. }
  91. }
  92. if (score->score == score->total)
  93. score->pass = true;
  94. }
  95. static void do_run_tests(struct manifest *m,
  96. bool keep,
  97. unsigned int *timeleft,
  98. struct score *score)
  99. {
  100. run_tests(m, keep, timeleft, score, do_valgrind);
  101. }
  102. static void do_run_tests_without_features(struct manifest *m,
  103. bool keep,
  104. unsigned int *timeleft,
  105. struct score *score)
  106. {
  107. run_tests(m, keep, timeleft, score, false);
  108. }
  109. /* Gcc's warn_unused_result is fascist bullshit. */
  110. #define doesnt_matter()
  111. static void run_under_debugger(struct manifest *m, struct score *score)
  112. {
  113. char *command;
  114. struct file_error *first;
  115. first = list_top(&score->per_file_errors, struct file_error, list);
  116. if (!ask("Should I run the first failing test under the debugger?"))
  117. return;
  118. command = talloc_asprintf(m, "gdb -ex 'break tap.c:139' -ex 'run' %s",
  119. first->file->compiled);
  120. if (system(command))
  121. doesnt_matter();
  122. }
  123. struct ccanlint tests_pass = {
  124. .key = "tests_pass",
  125. .name = "Module's run and api tests pass",
  126. .check = do_run_tests,
  127. .handle = run_under_debugger,
  128. .can_run = can_run,
  129. .needs = "tests_compile"
  130. };
  131. REGISTER_TEST(tests_pass);
  132. struct ccanlint tests_pass_without_features = {
  133. .key = "tests_pass_without_features",
  134. .name = "Module's run and api tests pass (without features)",
  135. .check = do_run_tests_without_features,
  136. .handle = run_under_debugger,
  137. .needs = "tests_compile_without_features"
  138. };
  139. REGISTER_TEST(tests_pass_without_features);