tests_coverage.c 4.8 KB

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