run-coverage.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <limits.h>
  12. #include <errno.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <err.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. struct coverage_result {
  19. float uncovered;
  20. const char *what;
  21. char *output;
  22. };
  23. static bool find_source_file(struct manifest *m, const char *filename)
  24. {
  25. struct ccan_file *i;
  26. list_for_each(&m->c_files, i, list) {
  27. if (streq(i->fullname, filename))
  28. return true;
  29. }
  30. list_for_each(&m->h_files, i, list) {
  31. if (streq(i->fullname, filename))
  32. return true;
  33. }
  34. return false;
  35. }
  36. /* FIXME: Don't know how stable this is. Read cov files directly? */
  37. static void analyze_coverage(struct manifest *m,
  38. struct coverage_result *res, const char *output,
  39. bool full_gcov)
  40. {
  41. char **lines = strsplit(res, output, "\n", NULL);
  42. float covered_lines = 0.0;
  43. unsigned int i, total_lines = 0;
  44. bool lines_matter = false;
  45. /* FIXME: We assume GCOV mentions all files!
  46. Output looks like:
  47. File '../../../ccan/tdb2/private.h'
  48. Lines executed:0.00% of 8
  49. /home/ccan/ccan/tdb2/test/run-simple-delete.c:creating 'run-simple-delete.c.gcov'
  50. File '../../../ccan/tdb2/tdb.c'
  51. Lines executed:0.00% of 450
  52. */
  53. for (i = 0; lines[i]; i++) {
  54. if (strstarts(lines[i], "File '")) {
  55. char *file = lines[i] + strlen("File '");
  56. file[strcspn(file, "'")] = '\0';
  57. if (find_source_file(m, file))
  58. lines_matter = true;
  59. else
  60. lines_matter = false;
  61. } else if (lines_matter
  62. && strstarts(lines[i], "Lines executed:")) {
  63. float ex;
  64. unsigned of;
  65. if (sscanf(lines[i], "Lines executed:%f%% of %u",
  66. &ex, &of) != 2)
  67. errx(1, "Could not parse line '%s'", lines[i]);
  68. total_lines += of;
  69. covered_lines += ex / 100.0 * of;
  70. } else if (full_gcov && strstr(lines[i], ":creating '")) {
  71. char *file, *filename, *apostrophe;
  72. apostrophe = strchr(lines[i], '\'');
  73. filename = apostrophe + 1;
  74. apostrophe = strchr(filename, '\'');
  75. *apostrophe = '\0';
  76. if (lines_matter) {
  77. file = grab_file(res, filename, NULL);
  78. if (!file) {
  79. res->what = talloc_asprintf(res,
  80. "Reading %s",
  81. filename);
  82. res->output = talloc_strdup(res,
  83. strerror(errno));
  84. return;
  85. }
  86. res->output = talloc_append_string(res->output,
  87. file);
  88. }
  89. if (tools_verbose)
  90. printf("Unlinking %s", filename);
  91. unlink(filename);
  92. }
  93. }
  94. /* Nothing covered? */
  95. if (total_lines == 0)
  96. res->uncovered = 1.0;
  97. else
  98. res->uncovered = 1.0 - covered_lines / total_lines;
  99. }
  100. static void *do_run_coverage_tests(struct manifest *m,
  101. bool keep,
  102. unsigned int *timeleft)
  103. {
  104. struct coverage_result *res;
  105. struct ccan_file *i;
  106. char *cmdout;
  107. char *covcmd;
  108. bool ok;
  109. bool full_gcov = (verbose > 1);
  110. res = talloc(m, struct coverage_result);
  111. res->what = NULL;
  112. res->output = talloc_strdup(res, "");
  113. res->uncovered = 1.0;
  114. /* This tells gcov where we put those .gcno files. */
  115. covcmd = talloc_asprintf(m, "gcov %s -o %s",
  116. full_gcov ? "" : "-n",
  117. talloc_dirname(res, m->info_file->compiled));
  118. /* Run them all. */
  119. list_for_each(&m->run_tests, i, list) {
  120. cmdout = run_command(m, timeleft, i->cov_compiled);
  121. if (cmdout) {
  122. res->what = i->fullname;
  123. res->output = talloc_steal(res, cmdout);
  124. return res;
  125. }
  126. covcmd = talloc_asprintf_append(covcmd, " %s", i->fullname);
  127. }
  128. list_for_each(&m->api_tests, i, list) {
  129. cmdout = run_command(m, timeleft, i->cov_compiled);
  130. if (cmdout) {
  131. res->what = i->fullname;
  132. res->output = talloc_steal(res, cmdout);
  133. return res;
  134. }
  135. covcmd = talloc_asprintf_append(covcmd, " %s", i->fullname);
  136. }
  137. /* Now run gcov: we want output even if it succeeds. */
  138. cmdout = run_with_timeout(m, covcmd, &ok, timeleft);
  139. if (!ok) {
  140. res->what = "Running gcov";
  141. res->output = talloc_steal(res, cmdout);
  142. return res;
  143. }
  144. analyze_coverage(m, res, cmdout, full_gcov);
  145. return res;
  146. }
  147. /* 1 point for 50%, 2 points for 75%, 3 points for 87.5%... */
  148. static unsigned int score_coverage(struct manifest *m, void *check_result)
  149. {
  150. struct coverage_result *res = check_result;
  151. float thresh;
  152. unsigned int i;
  153. for (i = 0, thresh = 0.5;
  154. i < run_coverage_tests.total_score;
  155. i++, thresh /= 2) {
  156. if (res->uncovered > thresh)
  157. break;
  158. }
  159. return i;
  160. }
  161. static const char *describe_run_coverage_tests(struct manifest *m,
  162. void *check_result)
  163. {
  164. struct coverage_result *res = check_result;
  165. bool full_gcov = (verbose > 1);
  166. char *ret;
  167. if (res->what)
  168. return talloc_asprintf(m, "%s: %s", res->what, res->output);
  169. if (!verbose)
  170. return NULL;
  171. ret = talloc_asprintf(m, "Tests achieved %0.2f%% coverage",
  172. (1.0 - res->uncovered) * 100);
  173. if (full_gcov)
  174. ret = talloc_asprintf_append(ret, "\n%s", res->output);
  175. return ret;
  176. }
  177. struct ccanlint run_coverage_tests = {
  178. .key = "test-coverage",
  179. .name = "Code coverage of module tests",
  180. .total_score = 5,
  181. .score = score_coverage,
  182. .check = do_run_coverage_tests,
  183. .describe = describe_run_coverage_tests,
  184. };
  185. REGISTER_TEST(run_coverage_tests, &compile_coverage_tests, NULL);