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>
18 static bool find_source_file(const struct manifest *m, const char *filename)
20 const struct ccan_file *i;
22 list_for_each(&m->c_files, i, list) {
23 if (streq(i->fullname, filename))
26 list_for_each(&m->h_files, i, list) {
27 if (streq(i->fullname, filename))
33 /* 1 point for 50%, 2 points for 75%, 3 points for 87.5%... Bonus for 100%. */
34 static unsigned int score_coverage(float covered, unsigned total)
36 float thresh, uncovered = 1.0 - covered;
43 for (i = 0, thresh = 0.5; i < total; i++, thresh /= 2) {
44 if (uncovered > thresh)
51 /* FIXME: Don't know how stable this is. Read cov files directly? */
52 static void analyze_coverage(struct manifest *m, bool full_gcov,
53 const char *output, struct score *score)
55 char **lines = tal_strsplit(score, output, "\n", STR_EMPTY_OK);
56 float covered_lines = 0.0;
57 unsigned int i, total_lines = 0;
58 bool lines_matter = false;
61 Output looks like: (gcov 4.6.3)
62 File '../../../ccan/tdb2/private.h'
63 Lines executed:0.00% of 8
64 /home/ccan/ccan/tdb2/test/run-simple-delete.c:creating 'run-simple-delete.c.gcov'
66 File '../../../ccan/tdb2/tdb.c'
67 Lines executed:0.00% of 450
71 File '/home/dwg/src/ccan/ccan/rfc822/test/run-check-check.c'
72 Lines executed:100.00% of 19
73 Creating 'run-check-check.c.gcov'
76 for (i = 0; lines[i]; i++) {
77 if (strstarts(lines[i], "File '")) {
78 char *file = lines[i] + strlen("File '");
79 file[strcspn(file, "'")] = '\0';
80 if (find_source_file(m, file))
84 } else if (lines_matter
85 && strstarts(lines[i], "Lines executed:")) {
88 if (sscanf(lines[i], "Lines executed:%f%% of %u",
90 errx(1, "Could not parse line '%s'", lines[i]);
92 covered_lines += ex / 100.0 * of;
94 && (strstr(lines[i], ":creating '")
95 || strstarts(lines[i], "Creating '"))) {
96 char *file, *filename, *apostrophe;
97 apostrophe = strchr(lines[i], '\'');
98 filename = apostrophe + 1;
99 apostrophe = strchr(filename, '\'');
102 file = tal_grab_file(score, filename, NULL);
104 score->error = tal_fmt(score,
112 printf("Unlinking %s", filename);
120 printf("%u of %u lines covered\n",
121 (unsigned)covered_lines, total_lines);
123 /* Nothing covered? We can't tell if there's a source file which
124 * was never executed, or there really is no code to execute, so
125 * assume the latter: this test deserves no score. */
126 if (total_lines == 0)
127 score->total = score->score = 0;
130 score->score = score_coverage(covered_lines / total_lines,
135 static void do_run_coverage_tests(struct manifest *m,
136 unsigned int *timeleft, struct score *score)
139 char *cmdout, *outdir;
141 bool full_gcov = (verbose > 1);
142 struct list_head *list;
143 bool ran_some = false;
145 /* This tells gcov where we put those .gcno files. */
146 outdir = path_dirname(score,
147 m->info_file->compiled[COMPILE_NORMAL]);
148 covcmd = tal_fmt(m, "gcov %s -o %s",
149 full_gcov ? "" : "-n",
153 foreach_ptr(list, &m->run_tests, &m->api_tests) {
154 list_for_each(list, i, list) {
155 if (run_command(score, timeleft, &cmdout,
156 "%s", i->compiled[COMPILE_COVERAGE])) {
157 tal_append_fmt(&covcmd, " %s", i->fullname);
159 score_file_error(score, i, 0,
160 "Running test with coverage"
161 " failed: %s", cmdout);
168 /* No tests at all? 0 out of 0 for you... */
170 score->total = score->score = 0;
175 /* Now run gcov: we want output even if it succeeds. */
176 if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
177 score->error = tal_fmt(score, "Running gcov: %s", cmdout);
181 analyze_coverage(m, full_gcov, cmdout, score);
184 struct ccanlint tests_coverage = {
185 .key = "tests_coverage",
186 .name = "Module's tests cover all the code",
187 .check = do_run_coverage_tests,
188 .needs = "tests_compile_coverage tests_pass"
191 REGISTER_TEST(tests_coverage);