2 * ccanlint: assorted checks and advice for a ccan package
3 * Copyright (C) 2008 Rusty Russell, Idris Soule
4 * Copyright (C) 2010 Rusty Russell, Idris Soule
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <ccan/btree/btree.h>
29 #include <ccan/str/str.h>
30 #include <ccan/str_talloc/str_talloc.h>
31 #include <ccan/talloc/talloc.h>
32 #include <ccan/opt/opt.h>
33 #include <ccan/foreach/foreach.h>
36 static LIST_HEAD(compulsory_tests);
37 static LIST_HEAD(normal_tests);
38 static LIST_HEAD(finished_tests);
39 bool safe_mode = false;
40 static struct btree *cmdline_exclude;
41 static struct btree *info_exclude;
42 static unsigned int timeout;
45 static void indent_print(const char *string)
48 unsigned int line = strcspn(string, "\n");
49 printf("\t%.*s", line, string);
50 if (string[line] == '\n') {
59 bool ask(const char *question)
63 printf("%s ", question);
66 return fgets(reply, sizeof(reply), stdin) != NULL
67 && toupper(reply[0]) == 'Y';
70 static const char *should_skip(struct manifest *m, struct ccanlint *i)
72 if (btree_lookup(cmdline_exclude, i->key))
73 return "excluded on command line";
75 if (btree_lookup(info_exclude, i->key))
76 return "excluded in _info file";
82 return "dependency failed";
89 static bool run_test(struct ccanlint *i,
91 unsigned int *running_score,
92 unsigned int *running_total,
95 unsigned int timeleft;
96 const struct dependent *d;
100 //one less test to run through
101 list_for_each(&i->dependencies, d, node)
102 d->dependent->num_depends--;
104 score = talloc(m, struct score);
105 list_head_init(&score->per_file_errors);
111 skip = should_skip(m, i);
115 if (verbose && !streq(skip, "not relevant to target"))
116 printf("%s: skipped (%s)\n", i->name, skip);
118 /* If we're skipping this because a prereq failed, we fail:
119 * count it as a score of 1. */
124 list_add_tail(&finished_tests, &i->list);
125 list_for_each(&i->dependencies, d, node) {
126 if (d->dependent->skip)
128 d->dependent->skip = "dependency was skipped";
129 d->dependent->skip_fail = i->skip_fail;
131 return i->skip_fail ? false : true;
134 timeleft = timeout ? timeout : default_timeout_ms;
135 i->check(m, i->keep_results, &timeleft, score);
136 if (timeout && timeleft == 0) {
141 assert(score->score <= score->total);
142 if ((!score->pass && !quiet)
143 || (score->score < score->total && verbose)
145 printf("%s (%s): %s", i->name, i->key, score->pass ? "PASS" : "FAIL");
146 if (score->total > 1)
147 printf(" (+%u/%u)", score->score, score->total);
151 if ((!quiet && !score->pass) || verbose) {
152 struct file_error *f;
153 unsigned int lines = 1;
156 printf("%s%s\n", score->error,
157 list_empty(&score->per_file_errors) ? "" : ":");
159 list_for_each(&score->per_file_errors, f, list) {
162 f->file->fullname, f->line, f->error);
164 printf("%s:%s\n", f->file->fullname, f->error);
166 printf("%s\n", f->error);
167 if (verbose < 2 && ++lines > 5) {
168 printf("... more (use -vv to see them all)\n");
172 if (!quiet && !score->pass && i->handle)
176 *running_score += score->score;
177 *running_total += score->total;
180 list_add_tail(&finished_tests, &i->list);
183 /* Skip any tests which depend on this one. */
184 list_for_each(&i->dependencies, d, node) {
185 if (d->dependent->skip)
187 d->dependent->skip = "dependency failed";
188 d->dependent->skip_fail = true;
194 static void register_test(struct list_head *h, struct ccanlint *test)
196 list_add(h, &test->list);
200 * get_next_test - retrieves the next test to be processed
202 static inline struct ccanlint *get_next_test(struct list_head *test)
206 if (list_empty(test))
209 list_for_each(test, i, list) {
210 if (i->num_depends == 0)
213 errx(1, "Can't make process; test dependency cycle");
216 static struct ccanlint *find_test(const char *key)
220 list_for_each(&compulsory_tests, i, list)
221 if (streq(i->key, key))
224 list_for_each(&normal_tests, i, list)
225 if (streq(i->key, key))
232 #define REGISTER_TEST(name, ...) extern struct ccanlint name
233 #include "generated-normal-tests"
234 #include "generated-compulsory-tests"
236 static void init_tests(void)
239 struct btree *keys, *names;
240 struct list_head *list;
243 #define REGISTER_TEST(name) register_test(&normal_tests, &name)
244 #include "generated-normal-tests"
246 #define REGISTER_TEST(name) register_test(&compulsory_tests, &name)
247 #include "generated-compulsory-tests"
249 /* Initialize dependency lists. */
250 foreach_ptr(list, &compulsory_tests, &normal_tests) {
251 list_for_each(list, c, list) {
252 list_head_init(&c->dependencies);
256 /* Resolve dependencies. */
257 foreach_ptr(list, &compulsory_tests, &normal_tests) {
258 list_for_each(list, c, list) {
259 char **deps = strsplit(NULL, c->needs, " ", NULL);
262 for (i = 0; deps[i]; i++) {
263 struct ccanlint *dep;
264 struct dependent *dchild;
266 dep = find_test(deps[i]);
268 errx(1, "BUG: unknown dep '%s' for %s",
270 dchild = talloc(NULL, struct dependent);
271 dchild->dependent = c;
272 list_add_tail(&dep->dependencies,
280 /* Self-consistency check: make sure no two tests
281 have the same key or name. */
282 keys = btree_new(btree_strcmp);
283 names = btree_new(btree_strcmp);
284 foreach_ptr(list, &compulsory_tests, &normal_tests) {
285 list_for_each(list, c, list) {
286 if (!btree_insert(keys, c->key))
287 errx(1, "BUG: Duplicate test key '%s'",
289 if (!btree_insert(names, c->name))
290 errx(1, "BUG: Duplicate test name '%s'",
300 foreach_ptr(list, &compulsory_tests, &normal_tests) {
301 printf("\%s Tests\n",
302 list == &compulsory_tests ? "Compulsory" : "Normal");
304 if (!list_empty(&c->dependencies)) {
305 const struct dependent *d;
306 printf("These depend on us:\n");
307 list_for_each(&c->dependencies, d, node)
308 printf("\t%s\n", d->dependent->name);
313 static char *keep_test(const char *testname, void *unused)
315 struct ccanlint *i = find_test(testname);
317 errx(1, "No test %s to --keep", testname);
318 i->keep_results = true;
322 static char *skip_test(const char *testname, void *unused)
324 btree_insert(cmdline_exclude, testname);
328 static void print_tests(struct list_head *tests, const char *type)
332 printf("%s tests:\n", type);
333 /* This makes them print in topological order. */
334 while ((i = get_next_test(tests)) != NULL) {
335 const struct dependent *d;
336 printf(" %-25s %s\n", i->key, i->name);
338 list_for_each(&i->dependencies, d, node)
339 d->dependent->num_depends--;
343 static char *list_tests(void *arg)
345 print_tests(&compulsory_tests, "Compulsory");
346 print_tests(&normal_tests, "Normal");
350 static void test_dgraph_vertices(struct list_head *tests, const char *style)
352 const struct ccanlint *i;
354 list_for_each(tests, i, list) {
356 * todo: escape labels in case ccanlint test keys have
357 * characters interpreted as GraphViz syntax.
359 printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
363 static void test_dgraph_edges(struct list_head *tests)
365 const struct ccanlint *i;
366 const struct dependent *d;
368 list_for_each(tests, i, list)
369 list_for_each(&i->dependencies, d, node)
370 printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
373 static char *test_dependency_graph(void *arg)
377 test_dgraph_vertices(&compulsory_tests, ", style=filled, fillcolor=yellow");
378 test_dgraph_vertices(&normal_tests, "");
380 test_dgraph_edges(&compulsory_tests);
381 test_dgraph_edges(&normal_tests);
388 /* Remove empty lines. */
389 static char **collapse(char **lines, unsigned int *nump)
392 for (i = j = 0; lines[i]; i++) {
394 lines[j++] = lines[i];
401 static void add_info_options(struct ccan_file *info, bool mark_fails)
403 struct doc_section *d;
405 struct ccanlint *test;
407 list_for_each(get_ccan_file_docs(info), d, list) {
408 if (!streq(d->type, "ccanlint"))
411 for (i = 0; i < d->num_lines; i++) {
412 char **words = collapse(strsplit(d, d->lines[i], " \t",
417 if (strncmp(words[0], "//", 2) == 0)
420 test = find_test(words[0]);
422 warnx("%s: unknown ccanlint test '%s'",
423 info->fullname, words[0]);
428 warnx("%s: no argument to test '%s'",
429 info->fullname, words[0]);
434 if (strcasecmp(words[1], "FAIL") == 0) {
436 btree_insert(info_exclude, words[0]);
438 if (!test->takes_options)
439 warnx("%s: %s doesn't take options",
440 info->fullname, words[0]);
441 /* Copy line exactly into options. */
442 test->options = strstr(d->lines[i], words[0])
449 static bool depends_on(struct ccanlint *i, struct ccanlint *target)
451 const struct dependent *d;
456 list_for_each(&i->dependencies, d, node) {
457 if (depends_on(d->dependent, target))
463 /* O(N^2), who cares? */
464 static void skip_unrelated_tests(struct ccanlint *target)
467 struct list_head *list;
469 foreach_ptr(list, &compulsory_tests, &normal_tests)
470 list_for_each(list, i, list)
471 if (!depends_on(i, target))
472 i->skip = "not relevant to target";
475 int main(int argc, char *argv[])
477 bool summary = false;
478 unsigned int score = 0, total_score = 0;
481 const char *prefix = "";
482 char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
486 cmdline_exclude = btree_new(btree_strcmp);
487 info_exclude = btree_new(btree_strcmp);
489 opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
490 "use this directory");
491 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
492 "do not compile anything");
493 opt_register_noarg("-l|--list-tests", list_tests, NULL,
494 "list tests ccanlint performs (and exit)");
495 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
496 "print dependency graph of tests in Graphviz .dot format");
497 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
498 "keep results of <testname> (can be used multiple times)");
499 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
500 "simply give one line summary");
501 opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
502 "verbose mode (up to -vvvv)");
503 opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
504 "exclude <testname> (can be used multiple times)");
505 opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
507 "ignore (terminate) tests that are slower than this");
508 opt_register_arg("--target <testname>", opt_set_charp,
510 "only run one test (and its prerequisites)");
511 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
512 "\nA program for checking and guiding development"
514 "This usage message");
516 opt_parse(&argc, argv, opt_log_stderr_exit);
519 dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
521 prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
523 compile_verbose = true;
525 tools_verbose = true;
527 /* We move into temporary directory, so gcov dumps its files there. */
528 if (chdir(temp_dir(talloc_autofree_context())) != 0)
529 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
531 m = get_manifest(talloc_autofree_context(), dir);
533 /* Create a symlink from temp dir back to src dir's test directory. */
534 if (symlink(talloc_asprintf(m, "%s/test", dir),
535 talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
536 err(1, "Creating test symlink in %s", temp_dir(NULL));
539 struct ccanlint *test;
541 test = find_test(target);
543 errx(1, "Unknown test to run '%s'", target);
544 skip_unrelated_tests(test);
547 /* If you don't pass the compulsory tests, you get a score of 0. */
548 while ((i = get_next_test(&compulsory_tests)) != NULL) {
549 if (!run_test(i, summary, &score, &total_score, m)) {
550 printf("%sTotal score: 0/%u\n", prefix, total_score);
551 errx(1, "%s%s failed", prefix, i->name);
555 /* --target overrides known FAIL from _info */
556 add_info_options(m->info_file, !target);
558 while ((i = get_next_test(&normal_tests)) != NULL)
559 run_test(i, summary, &score, &total_score, m);
561 printf("%sTotal score: %u/%u\n", prefix, score, total_score);