tests_compile_coverage.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "../compulsory_tests/build.h"
  18. #include "tests_compile.h"
  19. /* Note: we already test safe_mode in run_tests.c */
  20. static const char *can_run_coverage(struct manifest *m)
  21. {
  22. unsigned int timeleft = default_timeout_ms;
  23. char *output;
  24. if (!run_command(m, &timeleft, &output, "gcov -h"))
  25. return talloc_asprintf(m, "No gcov support: %s", output);
  26. return NULL;
  27. }
  28. static char *cov_compile(const void *ctx,
  29. struct manifest *m,
  30. struct ccan_file *file,
  31. bool link_with_module,
  32. bool keep)
  33. {
  34. char *output;
  35. char *flags = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
  36. file->compiled[COMPILE_COVERAGE]
  37. = maybe_temp_file(ctx, "", keep, file->fullname);
  38. if (!compile_and_link(ctx, file->fullname, ccan_dir,
  39. test_obj_list(m, link_with_module,
  40. COMPILE_NORMAL,
  41. COMPILE_COVERAGE),
  42. compiler, flags,
  43. lib_list(m, COMPILE_NORMAL),
  44. file->compiled[COMPILE_COVERAGE], &output)) {
  45. talloc_free(file->compiled[COMPILE_COVERAGE]);
  46. file->compiled[COMPILE_COVERAGE] = NULL;
  47. return output;
  48. }
  49. talloc_free(output);
  50. return NULL;
  51. }
  52. /* FIXME: Coverage from testable examples as well. */
  53. static void do_compile_coverage_tests(struct manifest *m,
  54. bool keep,
  55. unsigned int *timeleft,
  56. struct score *score)
  57. {
  58. char *cmdout;
  59. struct ccan_file *i;
  60. struct list_head *h;
  61. char *f = talloc_asprintf(score, "%s %s", cflags, COVERAGE_CFLAGS);
  62. /* For API tests, we need coverage version of module. */
  63. if (!list_empty(&m->api_tests)) {
  64. build_objects(m, keep, score, f, COMPILE_COVERAGE);
  65. if (!score->pass) {
  66. score->error = talloc_strdup(score,
  67. "Failed to compile module objects with coverage");
  68. return;
  69. }
  70. }
  71. foreach_ptr(h, &m->run_tests, &m->api_tests) {
  72. list_for_each(h, i, list) {
  73. cmdout = cov_compile(m, m, i,
  74. h == &m->api_tests,
  75. keep);
  76. if (cmdout) {
  77. score_file_error(score, i, 0,
  78. "Failed to compile test with coverage: %s",
  79. cmdout);
  80. }
  81. }
  82. }
  83. if (!score->error) {
  84. score->pass = true;
  85. score->score = score->total;
  86. }
  87. }
  88. struct ccanlint tests_compile_coverage = {
  89. .key = "tests_compile_coverage",
  90. .name = "Module tests compile with " COVERAGE_CFLAGS,
  91. .check = do_compile_coverage_tests,
  92. .can_run = can_run_coverage,
  93. .needs = "tests_compile"
  94. };
  95. REGISTER_TEST(tests_compile_coverage);