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.
22 #include "../read_config_header.h"
29 #include <ccan/str/str.h>
30 #include <ccan/take/take.h>
31 #include <ccan/opt/opt.h>
32 #include <ccan/foreach/foreach.h>
33 #include <ccan/cast/cast.h>
34 #include <ccan/tlist/tlist.h>
35 #include <ccan/strmap/strmap.h>
38 STRMAP_MEMBERS(struct ccanlint *);
42 static struct ccanlint_map tests;
43 bool safe_mode = false;
44 bool keep_results = false;
45 static bool targeting = false;
46 static unsigned int timeout;
48 /* These are overridden at runtime if we can find config.h */
49 const char *compiler = NULL;
50 const char *cflags = NULL;
52 const char *config_header;
57 static void indent_print(const char *string)
60 unsigned int line = strcspn(string, "\n");
61 printf("\t%.*s", line, string);
62 if (string[line] == '\n') {
71 bool ask(const char *question)
75 printf("%s ", question);
78 return fgets(reply, sizeof(reply), stdin) != NULL
79 && toupper(reply[0]) == 'Y';
82 /* Skip, but don't remove. */
83 static bool skip_test(struct dgraph_node *node, const char *why)
85 struct ccanlint *c = container_of(node, struct ccanlint, node);
90 static const char *dep_failed(struct manifest *m)
92 return "dependency couldn't run";
95 static bool cannot_run(struct dgraph_node *node, void *unused)
97 struct ccanlint *c = container_of(node, struct ccanlint, node);
98 c->can_run = dep_failed;
104 unsigned int score, total;
110 static bool run_test(struct dgraph_node *n, struct run_info *run)
112 struct ccanlint *i = container_of(n, struct ccanlint, node);
113 unsigned int timeleft;
119 score = tal(run->m, struct score);
120 list_head_init(&score->per_file_errors);
126 /* We can see skipped things in two cases:
127 * (1) _info excluded them (presumably because they fail).
128 * (2) A prerequisite failed.
132 printf("%s%s: skipped (%s)\n",
133 run->prefix, i->name, i->skip);
134 /* Pass us up to the test which failed, not us. */
140 i->skip = i->can_run(run->m);
142 /* Test doesn't apply, or can't run? That's OK. */
144 printf("%s%s: skipped (%s)\n",
145 run->prefix, i->name, i->skip);
146 /* Mark our dependencies to skip. */
147 dgraph_traverse_from(&i->node, cannot_run, NULL);
154 timeleft = timeout ? timeout : default_timeout_ms;
155 i->check(run->m, &timeleft, score);
156 if (timeout && timeleft == 0) {
159 printf("%s%s: skipped (%s)\n",
160 run->prefix, i->name, i->skip);
161 /* Mark our dependencies to skip. */
162 dgraph_traverse_from(&i->node, skip_test,
163 "dependency timed out");
169 assert(score->score <= score->total);
170 if ((!score->pass && !run->quiet)
171 || (score->score < score->total && verbose)
173 printf("%s%s (%s): %s",
174 run->prefix, i->name, i->key,
175 score->pass ? "PASS" : "FAIL");
176 if (score->total > 1)
177 printf(" (+%u/%u)", score->score, score->total);
181 if ((!run->quiet && !score->pass) || verbose) {
183 printf("%s%s", score->error,
184 strends(score->error, "\n") ? "" : "\n");
187 if (!run->quiet && score->score < score->total && i->handle)
188 i->handle(run->m, score);
191 /* Skip any tests which depend on this one. */
192 dgraph_traverse_from(&i->node, skip_test, "dependency failed");
196 run->score += score->score;
197 run->total += score->total;
199 /* FIXME: Free score. */
200 run->pass &= score->pass;
203 if (!score->pass && i->compulsory) {
204 warnx("%s%s failed", run->prefix, i->name);
211 static void register_test(struct ccanlint *test)
213 if (!strmap_add(&tests, test->key, test))
214 err(1, "Adding test %s", test->key);
215 test->options = tal_arr(NULL, char *, 1);
216 test->options[0] = NULL;
217 dgraph_init_node(&test->node);
220 static bool get_test(const char *member, struct ccanlint *i,
221 struct ccanlint **ret)
223 if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
231 * get_next_test - retrieves the next test to be processed
233 static inline struct ccanlint *get_next_test(void)
235 struct ccanlint *i = NULL;
237 strmap_iterate(&tests, get_test, &i);
241 if (strmap_empty(&tests))
244 errx(1, "Can't make process; test dependency cycle");
247 static struct ccanlint *find_test(const char *key)
249 return strmap_get(&tests, key);
252 bool is_excluded(const char *name)
254 return find_test(name)->skip != NULL;
257 static bool init_deps(const char *member, struct ccanlint *c, void *unused)
259 char **deps = tal_strsplit(NULL, c->needs, " ", STR_EMPTY_OK);
262 for (i = 0; deps[i]; i++) {
263 struct ccanlint *dep;
265 dep = find_test(deps[i]);
267 errx(1, "BUG: unknown dep '%s' for %s",
269 dgraph_add_edge(&dep->node, &c->node);
275 static bool check_names(const char *member, struct ccanlint *c,
276 struct ccanlint_map *names)
278 if (!strmap_add(names, c->name, c))
279 err(1, "Duplicate name %s", c->name);
283 static void init_tests(void)
285 struct ccanlint_map names;
286 struct ccanlint **table;
291 table = autodata_get(ccanlint_tests, &num);
292 for (i = 0; i < num; i++)
293 register_test(table[i]);
294 autodata_free(table);
296 strmap_iterate(&tests, init_deps, NULL);
298 /* Check for duplicate names. */
300 strmap_iterate(&tests, check_names, &names);
301 strmap_clear(&names);
304 static bool reset_test(struct dgraph_node *node, void *unused)
306 struct ccanlint *c = container_of(node, struct ccanlint, node);
312 static void reset_tests(struct dgraph_node *all)
314 dgraph_traverse_to(all, reset_test, NULL);
317 static bool print_deps(const char *member, struct ccanlint *c, void *unused)
319 if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
320 struct dgraph_edge *e;
322 printf("These depend on %s:\n", c->key);
323 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
324 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
327 printf("\t%s\n", to->key);
333 static void print_test_depends(void)
337 strmap_iterate(&tests, print_deps, NULL);
341 static void show_tmpdir(const char *dir)
343 printf("You can find ccanlint working files in '%s'\n", dir);
346 static char *keep_tests(void *unused)
350 /* Don't automatically destroy temporary dir. */
352 tal_add_destructor(temp_dir(), show_tmpdir);
356 static bool remove_test(struct dgraph_node *node, const char *why)
358 struct ccanlint *c = container_of(node, struct ccanlint, node);
360 dgraph_clear_node(node);
364 static char *exclude_test(const char *testname, void *unused)
366 struct ccanlint *i = find_test(testname);
368 return tal_fmt(NULL, "No test %s to --exclude", testname);
370 /* Remove this, and everything which depends on it. */
371 dgraph_traverse_from(&i->node, remove_test, "excluded on command line");
372 remove_test(&i->node, "excluded on command line");
376 static void skip_test_and_deps(struct ccanlint *c, const char *why)
378 /* Skip this, and everything which depends on us. */
379 dgraph_traverse_from(&c->node, skip_test, why);
380 skip_test(&c->node, why);
383 static char *list_tests(void *arg)
388 /* This makes them print in topological order. */
389 while ((i = get_next_test()) != NULL) {
390 printf(" %-25s %s\n", i->key, i->name);
391 dgraph_clear_node(&i->node);
392 strmap_del(&tests, i->key, NULL);
397 static bool draw_test(const char *member, struct ccanlint *c, const char *style)
400 * todo: escape labels in case ccanlint test keys have
401 * characters interpreted as GraphViz syntax.
403 printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
407 static void test_dgraph_vertices(const char *style)
409 strmap_iterate(&tests, draw_test, style);
412 static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
414 struct dgraph_edge *e;
416 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
417 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
420 printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
425 static void test_dgraph_edges(void)
427 strmap_iterate(&tests, draw_edges, NULL);
430 static char *test_dependency_graph(void *arg)
434 test_dgraph_vertices("");
442 static void add_options(struct ccanlint *test, char **options,
443 unsigned int num_options)
450 /* -1, because last one is NULL. */
451 num = tal_count(test->options) - 1;
453 tal_resize(&test->options, num + num_options + 1);
454 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
457 void add_info_options(struct ccan_file *info)
459 struct doc_section *d;
461 struct ccanlint *test;
463 list_for_each(get_ccan_file_docs(info), d, list) {
464 if (!streq(d->type, "ccanlint"))
467 for (i = 0; i < d->num_lines; i++) {
468 char **words = tal_strsplit(d, d->lines[i], " \t",
473 if (strncmp(words[0], "//", 2) == 0)
476 test = find_test(words[0]);
478 warnx("%s: unknown ccanlint test '%s'",
479 info->fullname, words[0]);
484 warnx("%s: no argument to test '%s'",
485 info->fullname, words[0]);
490 if (strcasecmp(words[1], "FAIL") == 0) {
492 skip_test_and_deps(test,
496 if (!test->takes_options)
497 warnx("%s: %s doesn't take options",
498 info->fullname, words[0]);
499 add_options(test, words+1, tal_count(words)-1);
505 /* If options are of form "filename:<option>" they only apply to that file */
506 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
509 unsigned int i, j = 0;
512 if (!test->options[0])
513 return test->options;
515 ret = tal_arr(f, char *, tal_count(test->options));
516 for (i = 0; test->options[i]; i++) {
519 if (!test->options[i] || !strchr(test->options[i], ':')) {
520 optname = test->options[i];
521 } else if (strstarts(test->options[i], f->name)
522 && test->options[i][strlen(f->name)] == ':') {
523 optname = test->options[i] + strlen(f->name) + 1;
527 /* FAIL overrides anything else. */
528 if (streq(optname, "FAIL")) {
529 ret = tal_arr(f, char *, 2);
530 ret[0] = (char *)"FAIL";
538 /* Shrink it to size so tal_array_length() works as expected. */
539 tal_resize(&ret, j + 1);
543 static char *opt_set_const_charp(const char *arg, const char **p)
545 return opt_set_charp(arg, cast_const2(char **, p));
548 static char *opt_set_target(const char *arg, struct dgraph_node *all)
550 struct ccanlint *t = find_test(arg);
552 return tal_fmt(NULL, "unknown --target %s", arg);
555 dgraph_add_edge(&t->node, all);
559 static bool run_tests(struct dgraph_node *all,
569 run.score = run.total = 0;
572 dgraph_traverse_to(all, run_test, &run);
574 printf("%sTotal score: %u/%u\n", prefix, run.score, run.total);
578 static bool add_to_all(const char *member, struct ccanlint *c,
579 struct dgraph_node *all)
581 /* If we're excluded on cmdline, don't add. */
583 dgraph_add_edge(&c->node, all);
587 int main(int argc, char *argv[])
589 bool summary = false, pass = true;
592 const char *prefix = "";
593 char *dir = tal_getcwd(NULL), *base_dir = dir, *testlink;
594 struct dgraph_node all;
596 /* Empty graph node to which we attach everything else. */
597 dgraph_init_node(&all);
599 opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
600 "verbose mode (up to -vvvv)");
601 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
602 "do not compile anything");
603 opt_register_noarg("-l|--list-tests", list_tests, NULL,
604 "list tests ccanlint performs (and exit)");
605 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
606 "print dependency graph of tests in Graphviz .dot format");
607 opt_register_noarg("-k|--keep", keep_tests, NULL,
608 "do not delete ccanlint working files");
609 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
610 "simply give one line summary");
611 opt_register_arg("-x|--exclude <testname>", exclude_test, NULL, NULL,
612 "exclude <testname> (can be used multiple times)");
613 opt_register_arg("--timeout <milleseconds>", opt_set_uintval,
615 "ignore (terminate) tests that are slower than this");
616 opt_register_arg("-t|--target <testname>", opt_set_target, NULL, &all,
617 "only run one test (and its prerequisites)");
618 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
619 NULL, &compiler, "set the compiler");
620 opt_register_arg("--cflags <flags>", opt_set_const_charp,
621 NULL, &cflags, "set the compiler flags");
622 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
623 "\nA program for checking and guiding development"
625 "This usage message");
627 /* Do verbose before anything else... */
628 opt_early_parse(argc, argv, opt_log_stderr_exit);
630 /* We move into temporary directory, so gcov dumps its files there. */
631 if (chdir(temp_dir()) != 0)
632 err(1, "Error changing to %s temporary dir", temp_dir());
637 compile_verbose = true;
638 print_test_depends();
641 tools_verbose = true;
643 opt_parse(&argc, argv, opt_log_stderr_exit);
646 strmap_iterate(&tests, add_to_all, &all);
648 /* This links back to the module's test dir. */
649 testlink = tal_fmt(NULL, "%s/test", temp_dir());
651 /* Defaults to pwd. */
657 for (i = 1; i < argc; i++) {
661 dir = tal_fmt(NULL, "%s/%s", base_dir, dir);
662 while (strends(dir, "/"))
663 dir[strlen(dir)-1] = '\0';
666 /* We assume there's a ccan/ in there somewhere... */
668 ccan_dir = find_ccan_dir(dir);
670 errx(1, "Cannot find ccan/ base directory in %s",
672 config_header = read_config_header(ccan_dir,
678 prefix = tal_strcat(NULL, take(tal_basename(NULL,dir)),
681 m = get_manifest(autofree(), dir);
683 /* Create a symlink from temp dir back to src dir's
686 if (symlink(tal_fmt(m, "%s/test", dir), testlink) != 0)
687 err(1, "Creating test symlink in %s", temp_dir());
689 if (!run_tests(&all, summary, m, prefix))