]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/ccanlint.c
ccanlint: timeout, and implement -t option for quicker tests.
[ccan] / tools / ccanlint / ccanlint.c
1 /*
2  * ccanlint: assorted checks and advice for a ccan package
3  * Copyright (C) 2008 Rusty Russell, Idris Soule
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  *   This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 #include "ccanlint.h"
20 #include "../tools.h"
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <err.h>
28 #include <ctype.h>
29 #include <ccan/btree/btree.h>
30 #include <ccan/str/str.h>
31 #include <ccan/str_talloc/str_talloc.h>
32 #include <ccan/talloc/talloc.h>
33
34 static unsigned int verbose = 0;
35 static LIST_HEAD(compulsory_tests);
36 static LIST_HEAD(normal_tests);
37 static LIST_HEAD(finished_tests);
38 bool safe_mode = false;
39 static struct btree *exclude;
40 static bool fastmode = false;
41
42 static void usage(const char *name)
43 {
44         fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-t] [-d <dirname>]\n"
45                 "   -v: verbose mode\n"
46                 "   -s: simply give one line summary\n"
47                 "   -d: use this directory instead of the current one\n"
48                 "   -n: do not compile anything\n"
49                 "   -l: list tests ccanlint performs\n"
50                 "   -x: exclude tests (e.g. -x trailing_whitespace,valgrind)\n"
51                 "   -t: ignore (terminate) tests that are slow\n",
52                 name);
53         exit(1);
54 }
55
56 #if 0
57 static void indent_print(const char *string)
58 {
59         while (*string) {
60                 unsigned int line = strcspn(string, "\n");
61                 printf("\t%.*s", line, string);
62                 if (string[line] == '\n') {
63                         printf("\n");
64                         line++;
65                 }
66                 string += line;
67         }
68 }
69 #endif
70
71 bool ask(const char *question)
72 {
73         char reply[2];
74
75         printf("%s ", question);
76         fflush(stdout);
77
78         return fgets(reply, sizeof(reply), stdin) != NULL
79                 && toupper(reply[0]) == 'Y';
80 }
81
82 static const char *should_skip(struct manifest *m, struct ccanlint *i)
83 {
84         if (btree_lookup(exclude, i->key))
85                 return "excluded on command line";
86         
87         if (i->skip_fail)
88                 return "dependency failed";
89
90         if (i->skip)
91                 return "dependency was skipped";
92
93         if (i->can_run)
94                 return i->can_run(m);
95         return NULL;
96 }
97
98 static bool run_test(struct ccanlint *i,
99                      bool quiet,
100                      unsigned int *score,
101                      unsigned int *total_score,
102                      struct manifest *m)
103 {
104         void *result;
105         unsigned int this_score, timeleft;
106         const struct dependent *d;
107         const char *skip;
108
109         //one less test to run through
110         list_for_each(&i->dependencies, d, node)
111                 d->dependent->num_depends--;
112
113         skip = should_skip(m, i);
114
115         if (skip) {
116         skip:
117                 if (verbose)
118                         printf("  %s: skipped (%s)\n", i->name, skip);
119
120                 /* If we're skipping this because a prereq failed, we fail. */
121                 if (i->skip_fail)
122                         *total_score += i->total_score;
123                         
124                 list_del(&i->list);
125                 list_add_tail(&finished_tests, &i->list);
126                 list_for_each(&i->dependencies, d, node) {
127                         d->dependent->skip = true;
128                         d->dependent->skip_fail = i->skip_fail;
129                 }
130                 return true;
131         }
132
133         timeleft = fastmode ? 1000 : default_timeout_ms;
134         result = i->check(m, &timeleft);
135         if (fastmode && timeleft == 0) {
136                 skip = "timeout";
137                 goto skip;
138         }
139         if (!result) {
140                 if (verbose) {
141                         printf("  %s: OK", i->name);
142                         if (i->total_score)
143                                 printf(" (+%u/%u)",
144                                        i->total_score, i->total_score);
145                         printf("\n");
146                 }
147                 if (i->total_score) {
148                         *score += i->total_score;
149                         *total_score += i->total_score;
150                 }
151
152                 list_del(&i->list);
153                 list_add_tail(&finished_tests, &i->list);
154                 return true;
155         }
156
157         if (i->score)
158                 this_score = i->score(m, result);
159         else
160                 this_score = 0;
161
162         list_del(&i->list);
163         list_add_tail(&finished_tests, &i->list);
164
165         *total_score += i->total_score;
166         *score += this_score;
167         if (verbose) {
168                 printf("  %s: FAIL (+%u/%u)\n",
169                        i->name, this_score, i->total_score);
170         }
171         if (!quiet) {
172                 printf("%s\n", i->describe(m, result));
173
174                 if (i->handle)
175                         i->handle(m, result);
176         }
177
178         /* Skip any tests which depend on this one. */
179         list_for_each(&i->dependencies, d, node) {
180                 d->dependent->skip = true;
181                 d->dependent->skip_fail = true;
182         }
183
184         return false;
185 }
186
187 static void register_test(struct list_head *h, struct ccanlint *test, ...)
188 {
189         va_list ap;
190         struct ccanlint *depends;
191         struct dependent *dchild;
192
193         list_add(h, &test->list);
194
195         va_start(ap, test);
196         /* Careful: we might have been initialized by a dependent. */
197         if (test->dependencies.n.next == NULL)
198                 list_head_init(&test->dependencies);
199
200         //dependent(s) args (if any), last one is NULL
201         while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
202                 dchild = malloc(sizeof(*dchild));
203                 dchild->dependent = test;
204                 /* The thing we depend on might not be initialized yet! */
205                 if (depends->dependencies.n.next == NULL)
206                         list_head_init(&depends->dependencies);
207                 list_add_tail(&depends->dependencies, &dchild->node);
208                 test->num_depends++;
209         }
210         va_end(ap);
211 }
212
213 /**
214  * get_next_test - retrieves the next test to be processed
215  **/
216 static inline struct ccanlint *get_next_test(struct list_head *test)
217 {
218         struct ccanlint *i;
219
220         if (list_empty(test))
221                 return NULL;
222
223         list_for_each(test, i, list) {
224                 if (i->num_depends == 0)
225                         return i;
226         }
227         errx(1, "Can't make process; test dependency cycle");
228 }
229
230 static void init_tests(void)
231 {
232         const struct ccanlint *i;
233         struct btree *keys, *names;
234
235 #undef REGISTER_TEST
236 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
237 #include "generated-normal-tests"
238 #undef REGISTER_TEST
239 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
240 #include "generated-compulsory-tests"
241
242         /* Self-consistency check: make sure no two tests
243            have the same key or name. */
244         keys = btree_new(btree_strcmp);
245         names = btree_new(btree_strcmp);
246         list_for_each(&compulsory_tests, i, list) {
247                 if (!btree_insert(keys, i->key))
248                         errx(1, "BUG: Duplicate test key '%s'", i->key);
249                 if (!btree_insert(keys, i->name))
250                         errx(1, "BUG: Duplicate test name '%s'", i->name);
251         }
252         list_for_each(&normal_tests, i, list) {
253                 if (!btree_insert(keys, i->key))
254                         errx(1, "BUG: Duplicate test key '%s'", i->key);
255                 if (!btree_insert(keys, i->name))
256                         errx(1, "BUG: Duplicate test name '%s'", i->name);
257         }
258         btree_delete(keys);
259         btree_delete(names);
260         
261         if (!verbose)
262                 return;
263
264         printf("\nCompulsory Tests\n");
265         list_for_each(&compulsory_tests, i, list) {
266                 printf("%s depends on %u others\n", i->name, i->num_depends);
267                 if (!list_empty(&i->dependencies)) {
268                         const struct dependent *d;
269                         printf("These depend on us:\n");
270                         list_for_each(&i->dependencies, d, node)
271                                 printf("\t%s\n", d->dependent->name);
272                 }
273         }
274
275         printf("\nNormal Tests\n");
276         list_for_each(&normal_tests, i, list) {
277                 printf("%s depends on %u others\n", i->name, i->num_depends);
278                 if (!list_empty(&i->dependencies)) {
279                         const struct dependent *d;
280                         printf("These depend on us:\n");
281                         list_for_each(&i->dependencies, d, node)
282                                 printf("\t%s\n", d->dependent->name);
283                 }
284         }
285 }
286
287 static void print_tests(struct list_head *tests, const char *type)
288 {
289         struct ccanlint *i;
290
291         printf("%s tests:\n", type);
292         /* This makes them print in topological order. */
293         while ((i = get_next_test(tests)) != NULL) {
294                 const struct dependent *d;
295                 printf("   %-25s %s\n", i->key, i->name);
296                 list_del(&i->list);
297                 list_for_each(&i->dependencies, d, node)
298                         d->dependent->num_depends--;
299         }
300 }
301
302 static void list_tests(void)
303 {
304         init_tests();
305         print_tests(&compulsory_tests, "Compulsory");
306         print_tests(&normal_tests, "Normal");
307         exit(0);
308 }
309
310 int main(int argc, char *argv[])
311 {
312         int c;
313         bool summary = false;
314         unsigned int score = 0, total_score = 0;
315         struct manifest *m;
316         struct ccanlint *i;
317         const char *prefix = "", *dir = ".";
318         
319         exclude = btree_new(btree_strcmp);
320
321         /* I'd love to use long options, but that's not standard. */
322         /* FIXME: getopt_long ccan package? */
323         while ((c = getopt(argc, argv, "sd:vnlx:t")) != -1) {
324                 switch (c) {
325                 case 'd':
326                         dir = optarg;
327                         prefix = talloc_append_string(talloc_basename(NULL,
328                                                                       optarg),
329                                                       ": ");
330                         break;
331                 case 'l':
332                         list_tests();
333                 case 's':
334                         summary = true;
335                         break;
336                 case 'v':
337                         verbose++;
338                         break;
339                 case 'n':
340                         safe_mode = true;
341                         break;
342                 case 'x': {
343                         char **exclude_strs = strsplit(NULL, optarg, ",", NULL);
344                         size_t i;
345                         for (i = 0; exclude_strs[i]; i++)
346                                 btree_insert(exclude, exclude_strs[i]);
347                 } break;
348                 case 't':
349                         fastmode = true;
350                         break;
351                 default:
352                         usage(argv[0]);
353                 }
354         }
355
356         if (optind < argc)
357                 usage(argv[0]);
358
359         m = get_manifest(talloc_autofree_context(), dir);
360
361         init_tests();
362
363         /* If you don't pass the compulsory tests, you don't even get a score */
364         if (verbose)
365                 printf("Compulsory tests:\n");
366
367         while ((i = get_next_test(&compulsory_tests)) != NULL) {
368                 if (!run_test(i, summary, &score, &total_score, m)) {
369                         errx(1, "%s%s failed", prefix, i->name);
370                 }
371         }
372
373         if (verbose)
374                 printf("\nNormal tests:\n");
375         while ((i = get_next_test(&normal_tests)) != NULL)
376                 run_test(i, summary, &score, &total_score, m);
377
378         printf("%sTotal score: %u/%u\n", prefix, score, total_score);
379         return 0;
380 }