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/tal/grab_file/grab_file.h>
6 #include <ccan/foreach/foreach.h>
19 static bool find_source_file(const struct manifest *m, const char *filename)
21 const struct ccan_file *i;
23 list_for_each(&m->c_files, i, list) {
24 if (streq(i->fullname, filename))
27 list_for_each(&m->h_files, i, list) {
28 if (streq(i->fullname, filename))
34 /* 1 point for 50%, 2 points for 75%, 3 points for 87.5%... Bonus for 100%. */
35 static unsigned int score_coverage(float covered, unsigned total)
37 float thresh, uncovered = 1.0 - covered;
44 for (i = 0, thresh = 0.5; i < total; i++, thresh /= 2) {
45 if (uncovered > thresh)
52 /* FIXME: Don't know how stable this is. Read cov files directly? */
53 static void analyze_coverage(struct manifest *m, bool full_gcov,
54 const char *output, struct score *score)
56 char **lines = tal_strsplit(score, output, "\n", STR_EMPTY_OK);
57 float covered_lines = 0.0;
58 unsigned int i, total_lines = 0;
59 bool lines_matter = false;
62 Output looks like: (gcov 4.6.3)
63 File '../../../ccan/tdb2/private.h'
64 Lines executed:0.00% of 8
65 /home/ccan/ccan/tdb2/test/run-simple-delete.c:creating 'run-simple-delete.c.gcov'
67 File '../../../ccan/tdb2/tdb.c'
68 Lines executed:0.00% of 450
72 File '/home/dwg/src/ccan/ccan/rfc822/test/run-check-check.c'
73 Lines executed:100.00% of 19
74 Creating 'run-check-check.c.gcov'
77 for (i = 0; lines[i]; i++) {
78 if (strstarts(lines[i], "File '")) {
79 char *file = lines[i] + strlen("File '");
80 file[strcspn(file, "'")] = '\0';
81 if (find_source_file(m, file))
85 } else if (lines_matter
86 && strstarts(lines[i], "Lines executed:")) {
89 if (sscanf(lines[i], "Lines executed:%f%% of %u",
91 errx(1, "Could not parse line '%s'", lines[i]);
93 covered_lines += ex / 100.0 * of;
95 && (strstr(lines[i], ":creating '")
96 || strstarts(lines[i], "Creating '"))) {
97 char *file, *filename, *apostrophe;
98 apostrophe = strchr(lines[i], '\'');
99 filename = apostrophe + 1;
100 apostrophe = strchr(filename, '\'');
103 file = grab_file(score, filename);
105 score->error = tal_fmt(score,
113 printf("Unlinking %s", filename);
121 printf("%u of %u lines covered\n",
122 (unsigned)covered_lines, total_lines);
124 /* Nothing covered? We can't tell if there's a source file which
125 * was never executed, or there really is no code to execute, so
126 * assume the latter: this test deserves no score. */
127 if (total_lines == 0)
128 score->total = score->score = 0;
131 score->score = score_coverage(covered_lines / total_lines,
136 static void do_run_coverage_tests(struct manifest *m,
137 unsigned int *timeleft, struct score *score)
140 char *cmdout, *outdir;
142 bool full_gcov = (verbose > 1);
143 struct list_head *list;
144 bool ran_some = false;
146 /* This tells gcov where we put those .gcno files. */
147 outdir = path_dirname(score,
148 m->info_file->compiled[COMPILE_NORMAL]);
149 covcmd = tal_fmt(m, "gcov %s -o %s",
150 full_gcov ? "" : "-n",
154 foreach_ptr(list, &m->run_tests, &m->api_tests) {
155 list_for_each(list, i, list) {
156 if (run_command(score, timeleft, &cmdout,
157 "%s", i->compiled[COMPILE_COVERAGE])) {
158 tal_append_fmt(&covcmd, " %s", i->fullname);
160 score_file_error(score, i, 0,
161 "Running test with coverage"
162 " failed: %s", cmdout);
169 /* No tests at all? 0 out of 0 for you... */
171 score->total = score->score = 0;
176 /* Now run gcov: we want output even if it succeeds. */
177 if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
178 score->error = tal_fmt(score, "Running gcov: %s", cmdout);
182 analyze_coverage(m, full_gcov, cmdout, score);
185 struct ccanlint tests_coverage = {
186 .key = "tests_coverage",
187 .name = "Module's tests cover all the code",
188 .check = do_run_coverage_tests,
189 .needs = "tests_compile_coverage tests_pass"
192 REGISTER_TEST(tests_coverage);