tests_pass.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. {
  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. return run_command(ctx, timeleft, cmdout,
  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. }
  69. }
  70. return run_command(m, timeleft, cmdout, "%s",
  71. i->compiled[COMPILE_NORMAL]);
  72. }
  73. static void do_run_tests(struct manifest *m,
  74. bool keep,
  75. unsigned int *timeleft,
  76. struct score *score)
  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 (verbose >= 2)
  86. printf(" %s\n", i->name);
  87. if (run_test(score, m, timeleft, &cmdout, i))
  88. score->score++;
  89. else
  90. score_file_error(score, i, 0, "%s", cmdout);
  91. }
  92. }
  93. if (score->score == score->total)
  94. score->pass = true;
  95. }
  96. /* Gcc's warn_unused_result is fascist bullshit. */
  97. #define doesnt_matter()
  98. static void run_under_debugger(struct manifest *m, struct score *score)
  99. {
  100. char *command;
  101. struct file_error *first;
  102. first = list_top(&score->per_file_errors, struct file_error, list);
  103. if (!ask("Should I run the first failing test under the debugger?"))
  104. return;
  105. command = talloc_asprintf(m, "gdb -ex 'break tap.c:139' -ex 'run' %s",
  106. first->file->compiled[COMPILE_NORMAL]);
  107. if (system(command))
  108. doesnt_matter();
  109. }
  110. struct ccanlint tests_pass = {
  111. .key = "tests_pass",
  112. .name = "Module's run and api tests pass",
  113. .check = do_run_tests,
  114. .handle = run_under_debugger,
  115. .can_run = can_run,
  116. .needs = "tests_compile"
  117. };
  118. REGISTER_TEST(tests_pass);