tests_pass.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/take/take.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 = tal_strdup(score, "");
  35. for (i = 0; bits[i]; i++) {
  36. if (i)
  37. ret = tal_strcat(score, take(ret), " ");
  38. ret = tal_strcat(score, take(ret), bits[i]);
  39. }
  40. return ret;
  41. }
  42. static void run_test(void *ctx,
  43. struct manifest *m,
  44. unsigned int *timeleft,
  45. struct ccan_file *i)
  46. {
  47. if (do_valgrind) {
  48. const char *options;
  49. options = concat(ctx,
  50. per_file_options(&tests_pass_valgrind, i));
  51. if (!streq(options, "FAIL")) {
  52. /* FIXME: Valgrind's output sucks. XML is
  53. * unreadable by humans *and* doesn't support
  54. * children reporting. */
  55. i->valgrind_log = tal_fmt(m,
  56. "%s.valgrind-log",
  57. i->compiled[COMPILE_NORMAL]);
  58. run_command_async(i, *timeleft,
  59. "valgrind -q"
  60. " --leak-check=full"
  61. " --log-fd=3 %s %s"
  62. " 3> %s",
  63. options,
  64. i->compiled[COMPILE_NORMAL],
  65. i->valgrind_log);
  66. return;
  67. }
  68. }
  69. run_command_async(i, *timeleft, "%s",
  70. i->compiled[COMPILE_NORMAL]);
  71. }
  72. static void do_run_tests(struct manifest *m,
  73. unsigned int *timeleft,
  74. struct score *score)
  75. {
  76. struct list_head *list;
  77. struct ccan_file *i;
  78. char *cmdout;
  79. bool ok;
  80. score->total = 0;
  81. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  82. list_for_each(list, i, list) {
  83. score->total++;
  84. if (verbose >= 2)
  85. printf(" %s...\n", i->name);
  86. run_test(score, m, timeleft, i);
  87. }
  88. }
  89. while ((i = collect_command(&ok, &cmdout)) != NULL) {
  90. if (!ok)
  91. score_file_error(score, i, 0, "%s", cmdout);
  92. else
  93. score->score++;
  94. if (verbose >= 2)
  95. printf(" ...%s\n", i->name);
  96. }
  97. if (score->score == score->total)
  98. score->pass = true;
  99. }
  100. /* Gcc's warn_unused_result is fascist bullshit. */
  101. #define doesnt_matter()
  102. static void run_under_debugger(struct manifest *m, struct score *score)
  103. {
  104. char *command;
  105. struct file_error *first;
  106. first = list_top(&score->per_file_errors, struct file_error, list);
  107. if (!ask("Should I run the first failing test under the debugger?"))
  108. return;
  109. command = tal_fmt(m, "gdb -ex 'break tap.c:139' -ex 'run' %s",
  110. first->file->compiled[COMPILE_NORMAL]);
  111. if (system(command))
  112. doesnt_matter();
  113. }
  114. struct ccanlint tests_pass = {
  115. .key = "tests_pass",
  116. .name = "Module's run and api tests pass",
  117. .check = do_run_tests,
  118. .handle = run_under_debugger,
  119. .can_run = can_run,
  120. .needs = "tests_compile"
  121. };
  122. REGISTER_TEST(tests_pass);