tests_coverage.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/grab_file/grab_file.h>
  6. #include <ccan/str/str.h>
  7. #include <ccan/foreach/foreach.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <limits.h>
  13. #include <errno.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <err.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19. static bool find_source_file(const struct manifest *m, const char *filename)
  20. {
  21. const struct ccan_file *i;
  22. list_for_each(&m->c_files, i, list) {
  23. if (streq(i->fullname, filename))
  24. return true;
  25. }
  26. list_for_each(&m->h_files, i, list) {
  27. if (streq(i->fullname, filename))
  28. return true;
  29. }
  30. return false;
  31. }
  32. /* 1 point for 50%, 2 points for 75%, 3 points for 87.5%... Bonus for 100%. */
  33. static unsigned int score_coverage(float covered, unsigned total)
  34. {
  35. float thresh, uncovered = 1.0 - covered;
  36. unsigned int i;
  37. if (covered == 1.0)
  38. return total;
  39. total--;
  40. for (i = 0, thresh = 0.5; i < total; i++, thresh /= 2) {
  41. if (uncovered > thresh)
  42. break;
  43. }
  44. return i;
  45. }
  46. /* FIXME: Don't know how stable this is. Read cov files directly? */
  47. static void analyze_coverage(struct manifest *m, bool full_gcov,
  48. const char *output, struct score *score)
  49. {
  50. char **lines = strsplit(score, output, "\n", NULL);
  51. float covered_lines = 0.0;
  52. unsigned int i, total_lines = 0;
  53. bool lines_matter = false;
  54. /*
  55. Output looks like:
  56. File '../../../ccan/tdb2/private.h'
  57. Lines executed:0.00% of 8
  58. /home/ccan/ccan/tdb2/test/run-simple-delete.c:creating 'run-simple-delete.c.gcov'
  59. File '../../../ccan/tdb2/tdb.c'
  60. Lines executed:0.00% of 450
  61. */
  62. for (i = 0; lines[i]; i++) {
  63. if (strstarts(lines[i], "File '")) {
  64. char *file = lines[i] + strlen("File '");
  65. file[strcspn(file, "'")] = '\0';
  66. if (find_source_file(m, file))
  67. lines_matter = true;
  68. else
  69. lines_matter = false;
  70. } else if (lines_matter
  71. && strstarts(lines[i], "Lines executed:")) {
  72. float ex;
  73. unsigned of;
  74. if (sscanf(lines[i], "Lines executed:%f%% of %u",
  75. &ex, &of) != 2)
  76. errx(1, "Could not parse line '%s'", lines[i]);
  77. total_lines += of;
  78. covered_lines += ex / 100.0 * of;
  79. } else if (full_gcov && strstr(lines[i], ":creating '")) {
  80. char *file, *filename, *apostrophe;
  81. apostrophe = strchr(lines[i], '\'');
  82. filename = apostrophe + 1;
  83. apostrophe = strchr(filename, '\'');
  84. *apostrophe = '\0';
  85. if (lines_matter) {
  86. file = grab_file(score, filename, NULL);
  87. if (!file) {
  88. score->error = talloc_asprintf(score,
  89. "Reading %s",
  90. filename);
  91. return;
  92. }
  93. printf("%s", file);
  94. }
  95. if (tools_verbose)
  96. printf("Unlinking %s", filename);
  97. unlink(filename);
  98. }
  99. }
  100. score->pass = true;
  101. /* Nothing covered? We can't tell if there's a source file which
  102. * was never executed, or there really is no code to execute, so
  103. * assume the latter: this test deserves no score. */
  104. if (total_lines == 0)
  105. score->total = score->score = 0;
  106. else {
  107. score->total = 6;
  108. score->score = score_coverage(covered_lines / total_lines,
  109. score->total);
  110. }
  111. }
  112. static void do_run_coverage_tests(struct manifest *m,
  113. bool keep,
  114. unsigned int *timeleft, struct score *score)
  115. {
  116. struct ccan_file *i;
  117. char *cmdout;
  118. char *covcmd;
  119. bool full_gcov = (verbose > 1);
  120. struct list_head *list;
  121. /* This tells gcov where we put those .gcno files. */
  122. covcmd = talloc_asprintf(m, "gcov %s -o %s",
  123. full_gcov ? "" : "-n",
  124. talloc_dirname(score, m->info_file->compiled));
  125. /* Run them all. */
  126. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  127. list_for_each(list, i, list) {
  128. if (run_command(score, timeleft, &cmdout,
  129. "%s", i->cov_compiled)) {
  130. covcmd = talloc_asprintf_append(covcmd, " %s",
  131. i->fullname);
  132. } else {
  133. score_file_error(score, i, 0,
  134. "Running test with coverage"
  135. " failed: %s", cmdout);
  136. return;
  137. }
  138. }
  139. }
  140. /* Now run gcov: we want output even if it succeeds. */
  141. if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
  142. score->error = talloc_asprintf(score, "Running gcov: %s",
  143. cmdout);
  144. return;
  145. }
  146. analyze_coverage(m, full_gcov, cmdout, score);
  147. }
  148. struct ccanlint tests_coverage = {
  149. .key = "tests_coverage",
  150. .name = "Module's tests cover all the code",
  151. .check = do_run_coverage_tests,
  152. .needs = "tests_compile_coverage tests_pass"
  153. };
  154. REGISTER_TEST(tests_coverage);