tests_coverage.c 4.8 KB

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