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/str_talloc/str_talloc.h>
31 #include <ccan/talloc/talloc.h>
32 #include <ccan/opt/opt.h>
33 #include <ccan/foreach/foreach.h>
34 #include <ccan/grab_file/grab_file.h>
35 #include <ccan/cast/cast.h>
36 #include <ccan/tlist/tlist.h>
37 #include <ccan/strmap/strmap.h>
40 STRMAP_MEMBERS(struct ccanlint *);
44 static struct ccanlint_map tests;
45 bool safe_mode = false;
46 bool keep_results = false;
47 static bool targeting = false;
48 static unsigned int timeout;
50 /* These are overridden at runtime if we can find config.h */
51 const char *compiler = NULL;
52 const char *cflags = NULL;
54 const char *config_header;
59 static void indent_print(const char *string)
62 unsigned int line = strcspn(string, "\n");
63 printf("\t%.*s", line, string);
64 if (string[line] == '\n') {
73 bool ask(const char *question)
77 printf("%s ", question);
80 return fgets(reply, sizeof(reply), stdin) != NULL
81 && toupper(reply[0]) == 'Y';
84 /* Skip, but don't remove. */
85 static bool skip_test(struct dgraph_node *node, const char *why)
87 struct ccanlint *c = container_of(node, struct ccanlint, node);
92 static const char *dep_failed(struct manifest *m)
94 return "dependency couldn't run";
97 static bool cannot_run(struct dgraph_node *node, void *unused)
99 struct ccanlint *c = container_of(node, struct ccanlint, node);
100 c->can_run = dep_failed;
106 unsigned int score, total;
112 static bool run_test(struct dgraph_node *n, struct run_info *run)
114 struct ccanlint *i = container_of(n, struct ccanlint, node);
115 unsigned int timeleft;
121 score = talloc(run->m, struct score);
122 list_head_init(&score->per_file_errors);
128 /* We can see skipped things in two cases:
129 * (1) _info excluded them (presumably because they fail).
130 * (2) A prerequisite failed.
134 printf("%s%s: skipped (%s)\n",
135 run->prefix, i->name, i->skip);
136 /* Pass us up to the test which failed, not us. */
142 i->skip = i->can_run(run->m);
144 /* Test doesn't apply, or can't run? That's OK. */
146 printf("%s%s: skipped (%s)\n",
147 run->prefix, i->name, i->skip);
148 /* Mark our dependencies to skip. */
149 dgraph_traverse_from(&i->node, cannot_run, NULL);
156 timeleft = timeout ? timeout : default_timeout_ms;
157 i->check(run->m, &timeleft, score);
158 if (timeout && timeleft == 0) {
161 printf("%s%s: skipped (%s)\n",
162 run->prefix, i->name, i->skip);
163 /* Mark our dependencies to skip. */
164 dgraph_traverse_from(&i->node, skip_test,
165 "dependency timed out");
171 assert(score->score <= score->total);
172 if ((!score->pass && !run->quiet)
173 || (score->score < score->total && verbose)
175 printf("%s%s (%s): %s",
176 run->prefix, i->name, i->key,
177 score->pass ? "PASS" : "FAIL");
178 if (score->total > 1)
179 printf(" (+%u/%u)", score->score, score->total);
183 if ((!run->quiet && !score->pass) || verbose) {
185 printf("%s%s", score->error,
186 strends(score->error, "\n") ? "" : "\n");
189 if (!run->quiet && score->score < score->total && i->handle)
190 i->handle(run->m, score);
193 /* Skip any tests which depend on this one. */
194 dgraph_traverse_from(&i->node, skip_test, "dependency failed");
198 run->score += score->score;
199 run->total += score->total;
201 /* FIXME: Free score. */
202 run->pass &= score->pass;
205 if (!score->pass && i->compulsory) {
206 warnx("%s%s failed", run->prefix, i->name);
213 static void register_test(struct ccanlint *test)
215 if (!strmap_add(&tests, test->key, test))
216 err(1, "Adding test %s", test->key);
217 test->options = talloc_array(NULL, char *, 1);
218 test->options[0] = NULL;
219 dgraph_init_node(&test->node);
222 static bool get_test(const char *member, struct ccanlint *i,
223 struct ccanlint **ret)
225 if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
233 * get_next_test - retrieves the next test to be processed
235 static inline struct ccanlint *get_next_test(void)
237 struct ccanlint *i = NULL;
239 strmap_iterate(&tests, get_test, &i);
243 if (strmap_empty(&tests))
246 errx(1, "Can't make process; test dependency cycle");
249 static struct ccanlint *find_test(const char *key)
251 return strmap_get(&tests, key);
254 bool is_excluded(const char *name)
256 return find_test(name)->skip != NULL;
259 static bool init_deps(const char *member, struct ccanlint *c, void *unused)
261 char **deps = strsplit(NULL, c->needs, " ");
264 for (i = 0; deps[i]; i++) {
265 struct ccanlint *dep;
267 dep = find_test(deps[i]);
269 errx(1, "BUG: unknown dep '%s' for %s",
271 dgraph_add_edge(&dep->node, &c->node);
277 static bool check_names(const char *member, struct ccanlint *c,
278 struct ccanlint_map *names)
280 if (!strmap_add(names, c->name, c))
281 err(1, "Duplicate name %s", c->name);
285 static void init_tests(void)
287 struct ccanlint_map names;
288 struct ccanlint **table;
293 table = autodata_get(ccanlint_tests, &num);
294 for (i = 0; i < num; i++)
295 register_test(table[i]);
296 autodata_free(table);
298 strmap_iterate(&tests, init_deps, NULL);
300 /* Check for duplicate names. */
302 strmap_iterate(&tests, check_names, &names);
303 strmap_clear(&names);
306 static bool reset_test(struct dgraph_node *node, void *unused)
308 struct ccanlint *c = container_of(node, struct ccanlint, node);
314 static void reset_tests(struct dgraph_node *all)
316 dgraph_traverse_to(all, reset_test, NULL);
319 static bool print_deps(const char *member, struct ccanlint *c, void *unused)
321 if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
322 struct dgraph_edge *e;
324 printf("These depend on %s:\n", c->key);
325 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
326 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
329 printf("\t%s\n", to->key);
335 static void print_test_depends(void)
339 strmap_iterate(&tests, print_deps, NULL);
343 static int show_tmpdir(const char *dir)
345 printf("You can find ccanlint working files in '%s'\n", dir);
349 static char *keep_tests(void *unused)
353 /* Don't automatically destroy temporary dir. */
354 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
358 static bool remove_test(struct dgraph_node *node, const char *why)
360 struct ccanlint *c = container_of(node, struct ccanlint, node);
362 dgraph_clear_node(node);
366 static char *exclude_test(const char *testname, void *unused)
368 struct ccanlint *i = find_test(testname);
370 return talloc_asprintf(NULL, "No test %s to --exclude",
373 /* Remove this, and everything which depends on it. */
374 dgraph_traverse_from(&i->node, remove_test, "excluded on command line");
375 remove_test(&i->node, "excluded on command line");
379 static void skip_test_and_deps(struct ccanlint *c, const char *why)
381 /* Skip this, and everything which depends on us. */
382 dgraph_traverse_from(&c->node, skip_test, why);
383 skip_test(&c->node, why);
386 static char *list_tests(void *arg)
391 /* This makes them print in topological order. */
392 while ((i = get_next_test()) != NULL) {
393 printf(" %-25s %s\n", i->key, i->name);
394 dgraph_clear_node(&i->node);
395 strmap_del(&tests, i->key, NULL);
400 static bool draw_test(const char *member, struct ccanlint *c, const char *style)
403 * todo: escape labels in case ccanlint test keys have
404 * characters interpreted as GraphViz syntax.
406 printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
410 static void test_dgraph_vertices(const char *style)
412 strmap_iterate(&tests, draw_test, style);
415 static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
417 struct dgraph_edge *e;
419 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
420 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
423 printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
428 static void test_dgraph_edges(void)
430 strmap_iterate(&tests, draw_edges, NULL);
433 static char *test_dependency_graph(void *arg)
437 test_dgraph_vertices("");
445 /* Remove empty lines. */
446 static char **collapse(char **lines, unsigned int *nump)
449 for (i = j = 0; lines[i]; i++) {
451 lines[j++] = lines[i];
460 static void add_options(struct ccanlint *test, char **options,
461 unsigned int num_options)
468 /* -1, because last one is NULL. */
469 num = talloc_array_length(test->options) - 1;
471 test->options = talloc_realloc(NULL, test->options,
473 num + num_options + 1);
474 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
477 void add_info_options(struct ccan_file *info)
479 struct doc_section *d;
481 struct ccanlint *test;
483 list_for_each(get_ccan_file_docs(info), d, list) {
484 if (!streq(d->type, "ccanlint"))
487 for (i = 0; i < d->num_lines; i++) {
488 unsigned int num_words;
489 char **words = collapse(strsplit(d, d->lines[i], " \t"),
494 if (strncmp(words[0], "//", 2) == 0)
497 test = find_test(words[0]);
499 warnx("%s: unknown ccanlint test '%s'",
500 info->fullname, words[0]);
505 warnx("%s: no argument to test '%s'",
506 info->fullname, words[0]);
511 if (strcasecmp(words[1], "FAIL") == 0) {
513 skip_test_and_deps(test,
517 if (!test->takes_options)
518 warnx("%s: %s doesn't take options",
519 info->fullname, words[0]);
520 add_options(test, words+1, num_words-1);
526 /* If options are of form "filename:<option>" they only apply to that file */
527 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
530 unsigned int i, j = 0;
533 if (!test->options[0])
534 return test->options;
536 ret = talloc_array(f, char *, talloc_array_length(test->options));
537 for (i = 0; test->options[i]; i++) {
540 if (!test->options[i] || !strchr(test->options[i], ':')) {
541 optname = test->options[i];
542 } else if (strstarts(test->options[i], f->name)
543 && test->options[i][strlen(f->name)] == ':') {
544 optname = test->options[i] + strlen(f->name) + 1;
548 /* FAIL overrides anything else. */
549 if (streq(optname, "FAIL")) {
550 ret = talloc_array(f, char *, 2);
551 ret[0] = (char *)"FAIL";
559 /* Shrink it to size so talloc_array_length() works as expected. */
560 return talloc_realloc(NULL, ret, char *, j + 1);
563 static char *opt_set_const_charp(const char *arg, const char **p)
565 return opt_set_charp(arg, cast_const2(char **, p));
568 static char *opt_set_target(const char *arg, struct dgraph_node *all)
570 struct ccanlint *t = find_test(arg);
572 return talloc_asprintf(NULL, "unknown --target %s", arg);
575 dgraph_add_edge(&t->node, all);
579 static bool run_tests(struct dgraph_node *all,
589 run.score = run.total = 0;
592 dgraph_traverse_to(all, run_test, &run);
594 printf("%sTotal score: %u/%u\n", prefix, run.score, run.total);
598 static bool add_to_all(const char *member, struct ccanlint *c,
599 struct dgraph_node *all)
601 /* If we're excluded on cmdline, don't add. */
603 dgraph_add_edge(&c->node, all);
607 int main(int argc, char *argv[])
609 bool summary = false, pass = true;
612 const char *prefix = "";
613 char *dir = talloc_getcwd(NULL), *base_dir = dir, *testlink;
614 struct dgraph_node all;
616 /* Empty graph node to which we attach everything else. */
617 dgraph_init_node(&all);
619 opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
620 "verbose mode (up to -vvvv)");
621 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
622 "do not compile anything");
623 opt_register_noarg("-l|--list-tests", list_tests, NULL,
624 "list tests ccanlint performs (and exit)");
625 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
626 "print dependency graph of tests in Graphviz .dot format");
627 opt_register_noarg("-k|--keep", keep_tests, NULL,
628 "do not delete ccanlint working files");
629 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
630 "simply give one line summary");
631 opt_register_arg("-x|--exclude <testname>", exclude_test, NULL, NULL,
632 "exclude <testname> (can be used multiple times)");
633 opt_register_arg("--timeout <milleseconds>", opt_set_uintval,
635 "ignore (terminate) tests that are slower than this");
636 opt_register_arg("-t|--target <testname>", opt_set_target, NULL, &all,
637 "only run one test (and its prerequisites)");
638 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
639 NULL, &compiler, "set the compiler");
640 opt_register_arg("--cflags <flags>", opt_set_const_charp,
641 NULL, &cflags, "set the compiler flags");
642 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
643 "\nA program for checking and guiding development"
645 "This usage message");
647 /* Do verbose before anything else... */
648 opt_early_parse(argc, argv, opt_log_stderr_exit);
650 /* We move into temporary directory, so gcov dumps its files there. */
651 if (chdir(temp_dir(talloc_autofree_context())) != 0)
652 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
657 compile_verbose = true;
658 print_test_depends();
661 tools_verbose = true;
663 opt_parse(&argc, argv, opt_log_stderr_exit);
666 strmap_iterate(&tests, add_to_all, &all);
668 /* This links back to the module's test dir. */
669 testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
671 /* Defaults to pwd. */
677 for (i = 1; i < argc; i++) {
678 unsigned int score, total_score;
683 dir = talloc_asprintf_append(NULL, "%s/%s",
685 while (strends(dir, "/"))
686 dir[strlen(dir)-1] = '\0';
689 /* We assume there's a ccan/ in there somewhere... */
691 ccan_dir = find_ccan_dir(dir);
693 errx(1, "Cannot find ccan/ base directory in %s",
695 config_header = read_config_header(ccan_dir,
701 prefix = talloc_append_string(talloc_basename(NULL,dir),
704 m = get_manifest(talloc_autofree_context(), dir);
706 /* Create a symlink from temp dir back to src dir's
709 if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
710 err(1, "Creating test symlink in %s", temp_dir(NULL));
712 score = total_score = 0;
713 if (!run_tests(&all, summary, m, prefix))