tests_coverage.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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:
  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. if (verbose > 1)
  102. printf("%u of %u lines covered\n",
  103. (unsigned)covered_lines, total_lines);
  104. /* Nothing covered? We can't tell if there's a source file which
  105. * was never executed, or there really is no code to execute, so
  106. * assume the latter: this test deserves no score. */
  107. if (total_lines == 0)
  108. score->total = score->score = 0;
  109. else {
  110. score->total = 6;
  111. score->score = score_coverage(covered_lines / total_lines,
  112. score->total);
  113. }
  114. }
  115. static void do_run_coverage_tests(struct manifest *m,
  116. bool keep,
  117. unsigned int *timeleft, struct score *score)
  118. {
  119. struct ccan_file *i;
  120. char *cmdout, *outdir;
  121. char *covcmd;
  122. bool full_gcov = (verbose > 1);
  123. struct list_head *list;
  124. bool ran_some = false;
  125. /* This tells gcov where we put those .gcno files. */
  126. outdir = talloc_dirname(score, m->info_file->compiled);
  127. covcmd = talloc_asprintf(m, "gcov %s -o %s",
  128. full_gcov ? "" : "-n",
  129. outdir);
  130. /* Unlink these files afterwards. */
  131. if (!keep) {
  132. talloc_set_destructor(talloc_asprintf(score,
  133. "%s/run.gcno", outdir),
  134. unlink_file_destructor);
  135. talloc_set_destructor(talloc_asprintf(score,
  136. "%s/run.gcda", outdir),
  137. unlink_file_destructor);
  138. }
  139. /* Run them all. */
  140. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  141. list_for_each(list, i, list) {
  142. if (run_command(score, timeleft, &cmdout,
  143. "%s", i->cov_compiled)) {
  144. covcmd = talloc_asprintf_append(covcmd, " %s",
  145. i->fullname);
  146. } else {
  147. score_file_error(score, i, 0,
  148. "Running test with coverage"
  149. " failed: %s", cmdout);
  150. return;
  151. }
  152. ran_some = true;
  153. }
  154. }
  155. /* No tests at all? 0 out of 0 for you... */
  156. if (!ran_some) {
  157. score->total = score->score = 0;
  158. score->pass = true;
  159. return;
  160. }
  161. /* Now run gcov: we want output even if it succeeds. */
  162. if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
  163. score->error = talloc_asprintf(score, "Running gcov: %s",
  164. cmdout);
  165. return;
  166. }
  167. analyze_coverage(m, full_gcov, cmdout, score);
  168. }
  169. struct ccanlint tests_coverage = {
  170. .key = "tests_coverage",
  171. .name = "Module's tests cover all the code",
  172. .check = do_run_coverage_tests,
  173. .needs = "tests_compile_coverage tests_pass"
  174. };
  175. REGISTER_TEST(tests_coverage);