tests_coverage.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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");
  51. float covered_lines = 0.0;
  52. unsigned int i, total_lines = 0;
  53. bool lines_matter = false;
  54. /*
  55. Output looks like: (gcov 4.6.3)
  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. For gcov 4.7.2:
  62. File '/home/dwg/src/ccan/ccan/rfc822/test/run-check-check.c'
  63. Lines executed:100.00% of 19
  64. Creating 'run-check-check.c.gcov'
  65. */
  66. for (i = 0; lines[i]; i++) {
  67. if (strstarts(lines[i], "File '")) {
  68. char *file = lines[i] + strlen("File '");
  69. file[strcspn(file, "'")] = '\0';
  70. if (find_source_file(m, file))
  71. lines_matter = true;
  72. else
  73. lines_matter = false;
  74. } else if (lines_matter
  75. && strstarts(lines[i], "Lines executed:")) {
  76. float ex;
  77. unsigned of;
  78. if (sscanf(lines[i], "Lines executed:%f%% of %u",
  79. &ex, &of) != 2)
  80. errx(1, "Could not parse line '%s'", lines[i]);
  81. total_lines += of;
  82. covered_lines += ex / 100.0 * of;
  83. } else if (full_gcov
  84. && (strstr(lines[i], ":creating '")
  85. || strstarts(lines[i], "Creating '"))) {
  86. char *file, *filename, *apostrophe;
  87. apostrophe = strchr(lines[i], '\'');
  88. filename = apostrophe + 1;
  89. apostrophe = strchr(filename, '\'');
  90. *apostrophe = '\0';
  91. if (lines_matter) {
  92. file = grab_file(score, filename, NULL);
  93. if (!file) {
  94. score->error = talloc_asprintf(score,
  95. "Reading %s",
  96. filename);
  97. return;
  98. }
  99. printf("%s", file);
  100. }
  101. if (tools_verbose)
  102. printf("Unlinking %s", filename);
  103. unlink(filename);
  104. }
  105. }
  106. score->pass = true;
  107. if (verbose > 1)
  108. printf("%u of %u lines covered\n",
  109. (unsigned)covered_lines, total_lines);
  110. /* Nothing covered? We can't tell if there's a source file which
  111. * was never executed, or there really is no code to execute, so
  112. * assume the latter: this test deserves no score. */
  113. if (total_lines == 0)
  114. score->total = score->score = 0;
  115. else {
  116. score->total = 6;
  117. score->score = score_coverage(covered_lines / total_lines,
  118. score->total);
  119. }
  120. }
  121. static void do_run_coverage_tests(struct manifest *m,
  122. unsigned int *timeleft, struct score *score)
  123. {
  124. struct ccan_file *i;
  125. char *cmdout, *outdir;
  126. char *covcmd;
  127. bool full_gcov = (verbose > 1);
  128. struct list_head *list;
  129. bool ran_some = false;
  130. /* This tells gcov where we put those .gcno files. */
  131. outdir = talloc_dirname(score,
  132. m->info_file->compiled[COMPILE_NORMAL]);
  133. covcmd = talloc_asprintf(m, "gcov %s -o %s",
  134. full_gcov ? "" : "-n",
  135. outdir);
  136. /* Run them all. */
  137. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  138. list_for_each(list, i, list) {
  139. if (run_command(score, timeleft, &cmdout,
  140. "%s", i->compiled[COMPILE_COVERAGE])) {
  141. covcmd = talloc_asprintf_append(covcmd, " %s",
  142. i->fullname);
  143. } else {
  144. score_file_error(score, i, 0,
  145. "Running test with coverage"
  146. " failed: %s", cmdout);
  147. return;
  148. }
  149. ran_some = true;
  150. }
  151. }
  152. /* No tests at all? 0 out of 0 for you... */
  153. if (!ran_some) {
  154. score->total = score->score = 0;
  155. score->pass = true;
  156. return;
  157. }
  158. /* Now run gcov: we want output even if it succeeds. */
  159. if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
  160. score->error = talloc_asprintf(score, "Running gcov: %s",
  161. cmdout);
  162. return;
  163. }
  164. analyze_coverage(m, full_gcov, cmdout, score);
  165. }
  166. struct ccanlint tests_coverage = {
  167. .key = "tests_coverage",
  168. .name = "Module's tests cover all the code",
  169. .check = do_run_coverage_tests,
  170. .needs = "tests_compile_coverage tests_pass"
  171. };
  172. REGISTER_TEST(tests_coverage);