run-coverage.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str_talloc/str_talloc.h>
  5. #include <ccan/str/str.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 "build-coverage.h"
  18. struct coverage_result {
  19. float uncovered;
  20. const char *what;
  21. const char *output;
  22. };
  23. static bool find_source_file(struct manifest *m, const char *filename)
  24. {
  25. struct ccan_file *i;
  26. list_for_each(&m->c_files, i, list) {
  27. if (streq(i->fullname, filename))
  28. return true;
  29. }
  30. list_for_each(&m->h_files, i, list) {
  31. if (streq(i->fullname, filename))
  32. return true;
  33. }
  34. return false;
  35. }
  36. /* FIXME: Don't know how stable this is. Read cov files directly? */
  37. static void analyze_coverage(struct manifest *m,
  38. struct coverage_result *res, const char *output)
  39. {
  40. char **lines = strsplit(res, output, "\n", NULL);
  41. float covered_lines = 0.0;
  42. unsigned int i, total_lines = 0;
  43. bool lines_matter = false;
  44. /* FIXME: We assume GCOV mentions all files!
  45. Output looks like:
  46. File '../../../ccan/tdb2/private.h'
  47. Lines executed:0.00% of 8
  48. File '../../../ccan/tdb2/tdb.c'
  49. Lines executed:0.00% of 450
  50. */
  51. for (i = 0; lines[i]; i++) {
  52. if (strstarts(lines[i], "File '")) {
  53. char *file = lines[i] + strlen("File '");
  54. file[strcspn(file, "'")] = '\0';
  55. if (find_source_file(m, file))
  56. lines_matter = true;
  57. else
  58. lines_matter = false;
  59. } else if (lines_matter
  60. && strstarts(lines[i], "Lines executed:")) {
  61. float ex;
  62. unsigned of;
  63. if (sscanf(lines[i], "Lines executed:%f%% of %u",
  64. &ex, &of) != 2)
  65. errx(1, "Could not parse line '%s'", lines[i]);
  66. total_lines += of;
  67. covered_lines += ex / 100.0 * of;
  68. }
  69. }
  70. /* Nothing covered? */
  71. if (total_lines == 0)
  72. res->uncovered = 1.0;
  73. else
  74. res->uncovered = 1.0 - covered_lines / total_lines;
  75. }
  76. static void *do_run_coverage_tests(struct manifest *m,
  77. bool keep,
  78. unsigned int *timeleft)
  79. {
  80. struct coverage_result *res;
  81. struct ccan_file *i;
  82. char *cmdout;
  83. char *olddir;
  84. char *covcmd;
  85. bool ok;
  86. /* We run tests in the module directory, so any paths
  87. * referenced can all be module-local. */
  88. olddir = talloc_getcwd(m);
  89. if (!olddir)
  90. err(1, "Could not save cwd");
  91. if (chdir(m->dir) != 0)
  92. err(1, "Could not chdir to %s", m->dir);
  93. res = talloc(m, struct coverage_result);
  94. res->what = NULL;
  95. res->uncovered = 1.0;
  96. /* This tells gcov where we put those .gcno files. */
  97. covcmd = talloc_asprintf(m, "gcov -n -o %s",
  98. talloc_dirname(res, m->info_file->compiled));
  99. /* Run them all. */
  100. list_for_each(&m->run_tests, i, list) {
  101. cmdout = run_command(m, timeleft, i->cov_compiled);
  102. if (cmdout) {
  103. res->what = i->fullname;
  104. res->output = talloc_steal(res, cmdout);
  105. return res;
  106. }
  107. covcmd = talloc_asprintf_append(covcmd, " %s", i->name);
  108. move_gcov_turd(olddir, i, ".gcda");
  109. }
  110. list_for_each(&m->api_tests, i, list) {
  111. cmdout = run_command(m, timeleft, i->cov_compiled);
  112. if (cmdout) {
  113. res->what = i->fullname;
  114. res->output = talloc_steal(res, cmdout);
  115. return res;
  116. }
  117. covcmd = talloc_asprintf_append(covcmd, " %s", i->name);
  118. move_gcov_turd(olddir, i, ".gcda");
  119. }
  120. /* Now run gcov: we want output even if it succeeds. */
  121. cmdout = run_with_timeout(m, covcmd, &ok, timeleft);
  122. if (!ok) {
  123. res->what = "Running gcov";
  124. res->output = talloc_steal(res, cmdout);
  125. return res;
  126. }
  127. analyze_coverage(m, res, cmdout);
  128. return res;
  129. }
  130. /* 1 point for 50%, 2 points for 75%, 3 points for 87.5%... */
  131. static unsigned int score_coverage(struct manifest *m, void *check_result)
  132. {
  133. struct coverage_result *res = check_result;
  134. float thresh;
  135. unsigned int i;
  136. for (i = 0, thresh = 0.5;
  137. i < run_coverage_tests.total_score;
  138. i++, thresh /= 2) {
  139. if (res->uncovered > thresh)
  140. break;
  141. }
  142. return i;
  143. }
  144. static const char *describe_run_coverage_tests(struct manifest *m,
  145. void *check_result)
  146. {
  147. struct coverage_result *res = check_result;
  148. if (res->what)
  149. return talloc_asprintf(m, "%s: %s", res->what, res->output);
  150. return talloc_asprintf(m, "Tests achieved %0.2f%% coverage",
  151. (1.0 - res->uncovered) * 100);
  152. }
  153. struct ccanlint run_coverage_tests = {
  154. .key = "test-coverage",
  155. .name = "Code coverage of module tests",
  156. .total_score = 5,
  157. .score = score_coverage,
  158. .check = do_run_coverage_tests,
  159. .describe = describe_run_coverage_tests,
  160. };
  161. REGISTER_TEST(run_coverage_tests, &compile_coverage_tests, NULL);