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