2 * ccanlint: assorted checks and advice for a ccan package
3 * Copyright (C) 2008 Rusty Russell, Idris Soule
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)
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
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.
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>
34 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 unsigned int timeout;
42 static void usage(const char *name)
44 fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-t <ms>] [-d <dirname>] [-x <tests>] [-k <test>]*\n"
45 " -v: verbose mode (can specify more than once)\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 " -k: keep results of this test (can be used multiple times)\n"
51 " -x: exclude tests (e.g. -x trailing_whitespace,valgrind)\n"
52 " -t: ignore (terminate) tests that are slower than this\n",
58 static void indent_print(const char *string)
61 unsigned int line = strcspn(string, "\n");
62 printf("\t%.*s", line, string);
63 if (string[line] == '\n') {
72 bool ask(const char *question)
76 printf("%s ", question);
79 return fgets(reply, sizeof(reply), stdin) != NULL
80 && toupper(reply[0]) == 'Y';
83 static const char *should_skip(struct manifest *m, struct ccanlint *i)
85 if (btree_lookup(exclude, i->key))
86 return "excluded on command line";
89 return "dependency failed";
92 return "dependency was skipped";
99 static bool run_test(struct ccanlint *i,
102 unsigned int *total_score,
106 unsigned int this_score, timeleft;
107 const struct dependent *d;
111 //one less test to run through
112 list_for_each(&i->dependencies, d, node)
113 d->dependent->num_depends--;
115 skip = should_skip(m, i);
120 printf(" %s: skipped (%s)\n", i->name, skip);
122 /* If we're skipping this because a prereq failed, we fail. */
124 *total_score += i->total_score;
127 list_add_tail(&finished_tests, &i->list);
128 list_for_each(&i->dependencies, d, node) {
129 d->dependent->skip = true;
130 d->dependent->skip_fail = i->skip_fail;
135 timeleft = timeout ? timeout : default_timeout_ms;
136 result = i->check(m, i->keep_results, &timeleft);
137 if (timeout && timeleft == 0) {
143 this_score = i->total_score ? i->total_score : 1;
145 this_score = i->score(m, result);
149 bad = (this_score == 0);
150 good = (this_score >= i->total_score);
152 if (verbose || (bad && !quiet)) {
153 printf(" %s: %s", i->name,
154 bad ? "FAIL" : good ? "PASS" : "PARTIAL");
157 this_score, i->total_score);
161 if (!quiet && result) {
163 if (i->describe && (desc = i->describe(m, result)) != NULL)
164 printf(" %s\n", desc);
166 i->handle(m, result);
169 if (i->total_score) {
170 *score += this_score;
171 *total_score += i->total_score;
175 list_add_tail(&finished_tests, &i->list);
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;
187 static void register_test(struct list_head *h, struct ccanlint *test, ...)
190 struct ccanlint *depends;
191 struct dependent *dchild;
193 list_add(h, &test->list);
196 /* Careful: we might have been initialized by a dependent. */
197 if (test->dependencies.n.next == NULL)
198 list_head_init(&test->dependencies);
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);
214 * get_next_test - retrieves the next test to be processed
216 static inline struct ccanlint *get_next_test(struct list_head *test)
220 if (list_empty(test))
223 list_for_each(test, i, list) {
224 if (i->num_depends == 0)
227 errx(1, "Can't make process; test dependency cycle");
230 static void init_tests(void)
232 const struct ccanlint *i;
233 struct btree *keys, *names;
236 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
237 #include "generated-normal-tests"
239 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
240 #include "generated-compulsory-tests"
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);
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);
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);
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);
287 static struct ccanlint *find_test(const char *key)
291 list_for_each(&compulsory_tests, i, list)
292 if (streq(i->key, key))
295 list_for_each(&normal_tests, i, list)
296 if (streq(i->key, key))
302 static void keep_test(const char *testname)
304 struct ccanlint *i = find_test(testname);
306 errx(1, "No test %s to --keep", testname);
307 i->keep_results = true;
310 static void print_tests(struct list_head *tests, const char *type)
314 printf("%s tests:\n", type);
315 /* This makes them print in topological order. */
316 while ((i = get_next_test(tests)) != NULL) {
317 const struct dependent *d;
318 printf(" %-25s %s\n", i->key, i->name);
320 list_for_each(&i->dependencies, d, node)
321 d->dependent->num_depends--;
325 static void list_tests(void)
327 print_tests(&compulsory_tests, "Compulsory");
328 print_tests(&normal_tests, "Normal");
332 int main(int argc, char *argv[])
335 bool summary = false;
336 unsigned int score = 0, total_score = 0;
339 const char *prefix = "", *dir = talloc_getcwd(NULL);
343 exclude = btree_new(btree_strcmp);
345 /* I'd love to use long options, but that's not standard. */
346 /* FIXME: popt ccan package? */
347 while ((c = getopt(argc, argv, "sd:vnlx:t:k:")) != -1) {
350 if (optarg[0] != '/')
351 dir = talloc_asprintf_append(NULL, "%s/%s",
355 prefix = talloc_append_string(talloc_basename(NULL,
374 char **exclude_strs = strsplit(NULL, optarg, ",", NULL);
376 for (i = 0; exclude_strs[i]; i++)
377 btree_insert(exclude, exclude_strs[i]);
380 timeout = atoi(optarg);
382 errx(1, "Invalid timeout %s: 1 ms minumum",
394 tools_verbose = true;
396 /* We move into temporary directory, so gcov dumps its files there. */
397 if (chdir(temp_dir(talloc_autofree_context())) != 0)
398 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
400 m = get_manifest(talloc_autofree_context(), dir);
402 /* Create a symlink from temp dir back to src dir's test directory. */
403 if (symlink(talloc_asprintf(m, "%s/test", dir),
404 talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
405 err(1, "Creating test symlink in %s", temp_dir(NULL));
407 /* If you don't pass the compulsory tests, you don't even get a score */
409 printf("Compulsory tests:\n");
411 while ((i = get_next_test(&compulsory_tests)) != NULL) {
412 if (!run_test(i, summary, &score, &total_score, m)) {
413 errx(1, "%s%s failed", prefix, i->name);
418 printf("\nNormal tests:\n");
419 while ((i = get_next_test(&normal_tests)) != NULL)
420 run_test(i, summary, &score, &total_score, m);
422 printf("%sTotal score: %u/%u\n", prefix, score, total_score);