tools: use tal/path instead of writing own path handlers.
[ccan] / tools / ccanlint / tests / tests_coverage.c
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>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <err.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 static bool find_source_file(const struct manifest *m, const char *filename)
19 {
20         const struct ccan_file *i;
21
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
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)
35 {
36         float thresh, uncovered = 1.0 - covered;
37         unsigned int i;
38
39         if (covered == 1.0)
40                 return total;
41
42         total--;
43         for (i = 0, thresh = 0.5; i < total; i++, thresh /= 2) {
44                 if (uncovered > thresh)
45                         break;
46         }
47         return i;
48 }
49
50
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)
54 {
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;
59
60         /*
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'
65
66            File '../../../ccan/tdb2/tdb.c'
67            Lines executed:0.00% of 450
68
69          For gcov 4.7.2:
70
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'
74         */
75
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))
81                                 lines_matter = true;
82                         else
83                                 lines_matter = false;
84                 } else if (lines_matter
85                            && strstarts(lines[i], "Lines executed:")) {
86                         float ex;
87                         unsigned of;
88                         if (sscanf(lines[i], "Lines executed:%f%% of %u",
89                                    &ex, &of) != 2)
90                                 errx(1, "Could not parse line '%s'", lines[i]);
91                         total_lines += of;
92                         covered_lines += ex / 100.0 * of;
93                 } else if (full_gcov
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, '\'');
100                         *apostrophe = '\0';
101                         if (lines_matter) {
102                                 file = tal_grab_file(score, filename, NULL);
103                                 if (!file) {
104                                         score->error = tal_fmt(score,
105                                                                "Reading %s",
106                                                                filename);
107                                         return;
108                                 }
109                                 printf("%s", file);
110                         }
111                         if (tools_verbose)
112                                 printf("Unlinking %s", filename);
113                         unlink(filename);
114                 }
115         }
116
117         score->pass = true;
118
119         if (verbose > 1)
120                 printf("%u of %u lines covered\n",
121                        (unsigned)covered_lines, total_lines);
122
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;
128         else {
129                 score->total = 6;
130                 score->score = score_coverage(covered_lines / total_lines,
131                                               score->total);
132         }
133 }
134
135 static void do_run_coverage_tests(struct manifest *m,
136                                   unsigned int *timeleft, struct score *score)
137 {
138         struct ccan_file *i;
139         char *cmdout, *outdir;
140         char *covcmd;
141         bool full_gcov = (verbose > 1);
142         struct list_head *list;
143         bool ran_some = false;
144
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",
150                          outdir);
151
152         /* Run them all. */
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);
158                         } else {
159                                 score_file_error(score, i, 0,
160                                                  "Running test with coverage"
161                                                  " failed: %s", cmdout);
162                                 return;
163                         }
164                         ran_some = true;
165                 }
166         }
167
168         /* No tests at all?  0 out of 0 for you... */
169         if (!ran_some) {
170                 score->total = score->score = 0;
171                 score->pass = true;
172                 return;
173         }
174
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);
178                 return;
179         }
180
181         analyze_coverage(m, full_gcov, cmdout, score);
182 }
183
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"
189 };
190
191 REGISTER_TEST(tests_coverage);