run-coverage.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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%... */
  33. static unsigned int score_coverage(float covered, unsigned total)
  34. {
  35. float thresh, uncovered = 1.0 - covered;
  36. unsigned int i;
  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 = strsplit(score, output, "\n", NULL);
  48. float covered_lines = 0.0;
  49. unsigned int i, total_lines = 0;
  50. bool lines_matter = false;
  51. /*
  52. Output looks like:
  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. */
  59. for (i = 0; lines[i]; i++) {
  60. if (strstarts(lines[i], "File '")) {
  61. char *file = lines[i] + strlen("File '");
  62. file[strcspn(file, "'")] = '\0';
  63. if (find_source_file(m, file))
  64. lines_matter = true;
  65. else
  66. lines_matter = false;
  67. } else if (lines_matter
  68. && strstarts(lines[i], "Lines executed:")) {
  69. float ex;
  70. unsigned of;
  71. if (sscanf(lines[i], "Lines executed:%f%% of %u",
  72. &ex, &of) != 2)
  73. errx(1, "Could not parse line '%s'", lines[i]);
  74. total_lines += of;
  75. covered_lines += ex / 100.0 * of;
  76. } else if (full_gcov && strstr(lines[i], ":creating '")) {
  77. char *file, *filename, *apostrophe;
  78. apostrophe = strchr(lines[i], '\'');
  79. filename = apostrophe + 1;
  80. apostrophe = strchr(filename, '\'');
  81. *apostrophe = '\0';
  82. if (lines_matter) {
  83. file = grab_file(score, filename, NULL);
  84. if (!file) {
  85. score->error = talloc_asprintf(score,
  86. "Reading %s",
  87. filename);
  88. return;
  89. }
  90. printf("%s", file);
  91. }
  92. if (tools_verbose)
  93. printf("Unlinking %s", filename);
  94. unlink(filename);
  95. }
  96. }
  97. score->pass = true;
  98. /* Nothing covered? We can't tell if there's a source file which
  99. * was never executed, or there really is no code to execute, so
  100. * assume the latter: this test deserves no score. */
  101. if (total_lines == 0)
  102. score->total = score->score = 0;
  103. else {
  104. score->total = 5;
  105. score->score = score_coverage(covered_lines / total_lines,
  106. score->total);
  107. }
  108. }
  109. static void do_run_coverage_tests(struct manifest *m,
  110. bool keep,
  111. unsigned int *timeleft, struct score *score)
  112. {
  113. struct ccan_file *i;
  114. char *cmdout;
  115. char *covcmd;
  116. bool full_gcov = (verbose > 1);
  117. struct list_head *list;
  118. /* This tells gcov where we put those .gcno files. */
  119. covcmd = talloc_asprintf(m, "gcov %s -o %s",
  120. full_gcov ? "" : "-n",
  121. talloc_dirname(score, m->info_file->compiled));
  122. /* Run them all. */
  123. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  124. list_for_each(list, i, list) {
  125. if (run_command(score, timeleft, &cmdout,
  126. "%s", i->cov_compiled)) {
  127. covcmd = talloc_asprintf_append(covcmd, " %s",
  128. i->fullname);
  129. } else {
  130. score->error = "Running test with coverage";
  131. score_file_error(score, i, 0, cmdout);
  132. return;
  133. }
  134. }
  135. }
  136. /* Now run gcov: we want output even if it succeeds. */
  137. if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
  138. score->error = talloc_asprintf(score, "Running gcov: %s",
  139. cmdout);
  140. return;
  141. }
  142. analyze_coverage(m, full_gcov, cmdout, score);
  143. }
  144. struct ccanlint run_coverage_tests = {
  145. .key = "test-coverage",
  146. .name = "Code coverage of module tests",
  147. .check = do_run_coverage_tests,
  148. };
  149. REGISTER_TEST(run_coverage_tests, &compile_coverage_tests, &run_tests, NULL);