1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/foreach/foreach.h>
17 static bool find_source_file(const struct manifest *m, const char *filename)
19 const struct ccan_file *i;
21 list_for_each(&m->c_files, i, list) {
22 if (streq(i->fullname, filename))
25 list_for_each(&m->h_files, i, list) {
26 if (streq(i->fullname, filename))
32 /* 1 point for 50%, 2 points for 75%, 3 points for 87.5%... Bonus for 100%. */
33 static unsigned int score_coverage(float covered, unsigned total)
35 float thresh, uncovered = 1.0 - covered;
42 for (i = 0, thresh = 0.5; i < total; i++, thresh /= 2) {
43 if (uncovered > thresh)
50 /* FIXME: Don't know how stable this is. Read cov files directly? */
51 static void analyze_coverage(struct manifest *m, bool full_gcov,
52 const char *output, struct score *score)
54 char **lines = tal_strsplit(score, output, "\n", STR_EMPTY_OK);
55 float covered_lines = 0.0;
56 unsigned int i, total_lines = 0;
57 bool lines_matter = false;
60 Output looks like: (gcov 4.6.3)
61 File '../../../ccan/tdb2/private.h'
62 Lines executed:0.00% of 8
63 /home/ccan/ccan/tdb2/test/run-simple-delete.c:creating 'run-simple-delete.c.gcov'
65 File '../../../ccan/tdb2/tdb.c'
66 Lines executed:0.00% of 450
70 File '/home/dwg/src/ccan/ccan/rfc822/test/run-check-check.c'
71 Lines executed:100.00% of 19
72 Creating 'run-check-check.c.gcov'
75 for (i = 0; lines[i]; i++) {
76 if (strstarts(lines[i], "File '")) {
77 char *file = lines[i] + strlen("File '");
78 file[strcspn(file, "'")] = '\0';
79 if (find_source_file(m, file))
83 } else if (lines_matter
84 && strstarts(lines[i], "Lines executed:")) {
87 if (sscanf(lines[i], "Lines executed:%f%% of %u",
89 errx(1, "Could not parse line '%s'", lines[i]);
91 covered_lines += ex / 100.0 * of;
93 && (strstr(lines[i], ":creating '")
94 || strstarts(lines[i], "Creating '"))) {
95 char *file, *filename, *apostrophe;
96 apostrophe = strchr(lines[i], '\'');
97 filename = apostrophe + 1;
98 apostrophe = strchr(filename, '\'');
101 file = tal_grab_file(score, filename, NULL);
103 score->error = tal_fmt(score,
111 printf("Unlinking %s", filename);
119 printf("%u of %u lines covered\n",
120 (unsigned)covered_lines, total_lines);
122 /* Nothing covered? We can't tell if there's a source file which
123 * was never executed, or there really is no code to execute, so
124 * assume the latter: this test deserves no score. */
125 if (total_lines == 0)
126 score->total = score->score = 0;
129 score->score = score_coverage(covered_lines / total_lines,
134 static void do_run_coverage_tests(struct manifest *m,
135 unsigned int *timeleft, struct score *score)
138 char *cmdout, *outdir;
140 bool full_gcov = (verbose > 1);
141 struct list_head *list;
142 bool ran_some = false;
144 /* This tells gcov where we put those .gcno files. */
145 outdir = tal_dirname(score,
146 m->info_file->compiled[COMPILE_NORMAL]);
147 covcmd = tal_fmt(m, "gcov %s -o %s",
148 full_gcov ? "" : "-n",
152 foreach_ptr(list, &m->run_tests, &m->api_tests) {
153 list_for_each(list, i, list) {
154 if (run_command(score, timeleft, &cmdout,
155 "%s", i->compiled[COMPILE_COVERAGE])) {
156 tal_append_fmt(&covcmd, " %s", i->fullname);
158 score_file_error(score, i, 0,
159 "Running test with coverage"
160 " failed: %s", cmdout);
167 /* No tests at all? 0 out of 0 for you... */
169 score->total = score->score = 0;
174 /* Now run gcov: we want output even if it succeeds. */
175 if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
176 score->error = tal_fmt(score, "Running gcov: %s", cmdout);
180 analyze_coverage(m, full_gcov, cmdout, score);
183 struct ccanlint tests_coverage = {
184 .key = "tests_coverage",
185 .name = "Module's tests cover all the code",
186 .check = do_run_coverage_tests,
187 .needs = "tests_compile_coverage tests_pass"
190 REGISTER_TEST(tests_coverage);