run-coverage.c 4.4 KB

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