tests_pass.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 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 = talloc_asprintf(m,
  56. "%s.valgrind-log",
  57. i->compiled[COMPILE_NORMAL]);
  58. talloc_set_destructor(i->valgrind_log,
  59. unlink_file_destructor);
  60. run_command_async(i, *timeleft,
  61. "valgrind -q"
  62. " --leak-check=full"
  63. " --log-fd=3 %s %s"
  64. " 3> %s",
  65. options,
  66. i->compiled[COMPILE_NORMAL],
  67. i->valgrind_log);
  68. return;
  69. }
  70. }
  71. run_command_async(i, *timeleft, "%s",
  72. i->compiled[COMPILE_NORMAL]);
  73. }
  74. static void do_run_tests(struct manifest *m,
  75. bool keep,
  76. unsigned int *timeleft,
  77. struct score *score)
  78. {
  79. struct list_head *list;
  80. struct ccan_file *i;
  81. char *cmdout;
  82. bool ok;
  83. score->total = 0;
  84. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  85. list_for_each(list, i, list) {
  86. score->total++;
  87. if (verbose >= 2)
  88. printf(" %s...\n", i->name);
  89. run_test(score, m, timeleft, i);
  90. }
  91. }
  92. while ((i = collect_command(&ok, &cmdout)) != NULL) {
  93. if (!ok)
  94. score_file_error(score, i, 0, "%s", cmdout);
  95. else
  96. score->score++;
  97. if (verbose >= 2)
  98. printf(" ...%s\n", i->name);
  99. }
  100. if (score->score == score->total)
  101. score->pass = true;
  102. }
  103. /* Gcc's warn_unused_result is fascist bullshit. */
  104. #define doesnt_matter()
  105. static void run_under_debugger(struct manifest *m, struct score *score)
  106. {
  107. char *command;
  108. struct file_error *first;
  109. first = list_top(&score->per_file_errors, struct file_error, list);
  110. if (!ask("Should I run the first failing test under the debugger?"))
  111. return;
  112. command = talloc_asprintf(m, "gdb -ex 'break tap.c:139' -ex 'run' %s",
  113. first->file->compiled[COMPILE_NORMAL]);
  114. if (system(command))
  115. doesnt_matter();
  116. }
  117. struct ccanlint tests_pass = {
  118. .key = "tests_pass",
  119. .name = "Module's run and api tests pass",
  120. .check = do_run_tests,
  121. .handle = run_under_debugger,
  122. .can_run = can_run,
  123. .needs = "tests_compile"
  124. };
  125. REGISTER_TEST(tests_pass);