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>
20 static bool find_source_file(const struct manifest *m, const char *filename)
22 const struct ccan_file *i;
24 list_for_each(&m->c_files, i, list) {
25 if (streq(i->fullname, filename))
28 list_for_each(&m->h_files, i, list) {
29 if (streq(i->fullname, filename))
35 /* 1 point for 50%, 2 points for 75%, 3 points for 87.5%... Bonus for 100%. */
36 static unsigned int score_coverage(float covered, unsigned total)
38 float thresh, uncovered = 1.0 - covered;
45 for (i = 0, thresh = 0.5; i < total; i++, thresh /= 2) {
46 if (uncovered > thresh)
53 /* FIXME: Don't know how stable this is. Read cov files directly? */
54 static void analyze_coverage(struct manifest *m, bool full_gcov,
55 const char *output, struct score *score)
57 char **lines = strsplit(score, output, "\n");
58 float covered_lines = 0.0;
59 unsigned int i, total_lines = 0;
60 bool lines_matter = false;
64 File '../../../ccan/tdb2/private.h'
65 Lines executed:0.00% of 8
66 /home/ccan/ccan/tdb2/test/run-simple-delete.c:creating 'run-simple-delete.c.gcov'
68 File '../../../ccan/tdb2/tdb.c'
69 Lines executed:0.00% of 450
72 for (i = 0; lines[i]; i++) {
73 if (strstarts(lines[i], "File '")) {
74 char *file = lines[i] + strlen("File '");
75 file[strcspn(file, "'")] = '\0';
76 if (find_source_file(m, file))
80 } else if (lines_matter
81 && strstarts(lines[i], "Lines executed:")) {
84 if (sscanf(lines[i], "Lines executed:%f%% of %u",
86 errx(1, "Could not parse line '%s'", lines[i]);
88 covered_lines += ex / 100.0 * of;
89 } else if (full_gcov && strstr(lines[i], ":creating '")) {
90 char *file, *filename, *apostrophe;
91 apostrophe = strchr(lines[i], '\'');
92 filename = apostrophe + 1;
93 apostrophe = strchr(filename, '\'');
96 file = grab_file(score, filename, NULL);
98 score->error = talloc_asprintf(score,
106 printf("Unlinking %s", filename);
114 printf("%u of %u lines covered\n",
115 (unsigned)covered_lines, total_lines);
117 /* Nothing covered? We can't tell if there's a source file which
118 * was never executed, or there really is no code to execute, so
119 * assume the latter: this test deserves no score. */
120 if (total_lines == 0)
121 score->total = score->score = 0;
124 score->score = score_coverage(covered_lines / total_lines,
129 static void do_run_coverage_tests(struct manifest *m,
131 unsigned int *timeleft, struct score *score)
134 char *cmdout, *outdir;
136 bool full_gcov = (verbose > 1);
137 struct list_head *list;
138 bool ran_some = false;
140 /* This tells gcov where we put those .gcno files. */
141 outdir = talloc_dirname(score,
142 m->info_file->compiled[COMPILE_NORMAL]);
143 covcmd = talloc_asprintf(m, "gcov %s -o %s",
144 full_gcov ? "" : "-n",
147 /* Unlink these files afterwards. */
149 talloc_set_destructor(talloc_asprintf(score,
150 "%s/run.gcno", outdir),
151 unlink_file_destructor);
152 talloc_set_destructor(talloc_asprintf(score,
153 "%s/run.gcda", outdir),
154 unlink_file_destructor);
158 foreach_ptr(list, &m->run_tests, &m->api_tests) {
159 list_for_each(list, i, list) {
160 if (run_command(score, timeleft, &cmdout,
161 "%s", i->compiled[COMPILE_COVERAGE])) {
162 covcmd = talloc_asprintf_append(covcmd, " %s",
165 score_file_error(score, i, 0,
166 "Running test with coverage"
167 " failed: %s", cmdout);
174 /* No tests at all? 0 out of 0 for you... */
176 score->total = score->score = 0;
181 /* Now run gcov: we want output even if it succeeds. */
182 if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
183 score->error = talloc_asprintf(score, "Running gcov: %s",
188 analyze_coverage(m, full_gcov, cmdout, score);
191 struct ccanlint tests_coverage = {
192 .key = "tests_coverage",
193 .name = "Module's tests cover all the code",
194 .check = do_run_coverage_tests,
195 .needs = "tests_compile_coverage tests_pass"
198 REGISTER_TEST(tests_coverage);