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.
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 #include <ccan/opt/opt.h>
34 #include <ccan/foreach/foreach.h>
37 static LIST_HEAD(compulsory_tests);
38 static LIST_HEAD(normal_tests);
39 static LIST_HEAD(finished_tests);
40 bool safe_mode = false;
41 static struct btree *cmdline_exclude;
42 static struct btree *info_exclude;
43 static unsigned int timeout;
46 static void indent_print(const char *string)
49 unsigned int line = strcspn(string, "\n");
50 printf("\t%.*s", line, string);
51 if (string[line] == '\n') {
60 bool ask(const char *question)
64 printf("%s ", question);
67 return fgets(reply, sizeof(reply), stdin) != NULL
68 && toupper(reply[0]) == 'Y';
71 static const char *should_skip(struct manifest *m, struct ccanlint *i)
73 if (btree_lookup(cmdline_exclude, i->key))
74 return "excluded on command line";
76 if (btree_lookup(info_exclude, i->key))
77 return "excluded in _info file";
83 return "dependency failed";
90 static bool run_test(struct ccanlint *i,
92 unsigned int *running_score,
93 unsigned int *running_total,
96 unsigned int timeleft;
97 const struct dependent *d;
101 //one less test to run through
102 list_for_each(&i->dependencies, d, node)
103 d->dependent->num_depends--;
105 score = talloc(m, struct score);
106 list_head_init(&score->per_file_errors);
112 skip = should_skip(m, i);
116 if (verbose && !streq(skip, "not relevant to target"))
117 printf("%s: skipped (%s)\n", i->name, skip);
119 /* If we're skipping this because a prereq failed, we fail:
120 * count it as a score of 1. */
125 list_add_tail(&finished_tests, &i->list);
126 list_for_each(&i->dependencies, d, node) {
127 if (d->dependent->skip)
129 d->dependent->skip = "dependency was skipped";
130 d->dependent->skip_fail = i->skip_fail;
132 return i->skip_fail ? false : true;
135 timeleft = timeout ? timeout : default_timeout_ms;
136 i->check(m, i->keep_results, &timeleft, score);
137 if (timeout && timeleft == 0) {
142 assert(score->score <= score->total);
143 if ((!score->pass && !quiet)
144 || (score->score < score->total && verbose)
146 printf("%s: %s", i->name, score->pass ? "PASS" : "FAIL");
147 if (score->total > 1)
148 printf(" (+%u/%u)", score->score, score->total);
152 if ((!quiet && !score->pass) || verbose) {
153 struct file_error *f;
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);
168 if (!quiet && !score->pass && i->handle)
172 *running_score += score->score;
173 *running_total += score->total;
176 list_add_tail(&finished_tests, &i->list);
179 /* Skip any tests which depend on this one. */
180 list_for_each(&i->dependencies, d, node) {
181 if (d->dependent->skip)
183 d->dependent->skip = "dependency failed";
184 d->dependent->skip_fail = true;
190 static void register_test(struct list_head *h, struct ccanlint *test, ...)
193 struct ccanlint *depends;
194 struct dependent *dchild;
196 list_add(h, &test->list);
199 /* Careful: we might have been initialized by a dependent. */
200 if (test->dependencies.n.next == NULL)
201 list_head_init(&test->dependencies);
203 //dependent(s) args (if any), last one is NULL
204 while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
205 dchild = malloc(sizeof(*dchild));
206 dchild->dependent = test;
207 /* The thing we depend on might not be initialized yet! */
208 if (depends->dependencies.n.next == NULL)
209 list_head_init(&depends->dependencies);
210 list_add_tail(&depends->dependencies, &dchild->node);
217 * get_next_test - retrieves the next test to be processed
219 static inline struct ccanlint *get_next_test(struct list_head *test)
223 if (list_empty(test))
226 list_for_each(test, i, list) {
227 if (i->num_depends == 0)
230 errx(1, "Can't make process; test dependency cycle");
233 static void init_tests(void)
235 const struct ccanlint *i;
236 struct btree *keys, *names;
239 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__, NULL)
240 #include "generated-normal-tests"
242 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__, NULL)
243 #include "generated-compulsory-tests"
245 /* Self-consistency check: make sure no two tests
246 have the same key or name. */
247 keys = btree_new(btree_strcmp);
248 names = btree_new(btree_strcmp);
249 list_for_each(&compulsory_tests, i, list) {
250 if (!btree_insert(keys, i->key))
251 errx(1, "BUG: Duplicate test key '%s'", i->key);
252 if (!btree_insert(keys, i->name))
253 errx(1, "BUG: Duplicate test name '%s'", i->name);
255 list_for_each(&normal_tests, i, list) {
256 if (!btree_insert(keys, i->key))
257 errx(1, "BUG: Duplicate test key '%s'", i->key);
258 if (!btree_insert(keys, i->name))
259 errx(1, "BUG: Duplicate test name '%s'", i->name);
267 printf("\nCompulsory Tests\n");
268 list_for_each(&compulsory_tests, i, list) {
269 printf("%s depends on %u others\n", i->name, i->num_depends);
270 if (!list_empty(&i->dependencies)) {
271 const struct dependent *d;
272 printf("These depend on us:\n");
273 list_for_each(&i->dependencies, d, node)
274 printf("\t%s\n", d->dependent->name);
278 printf("\nNormal Tests\n");
279 list_for_each(&normal_tests, i, list) {
280 printf("%s depends on %u others\n", i->name, i->num_depends);
281 if (!list_empty(&i->dependencies)) {
282 const struct dependent *d;
283 printf("These depend on us:\n");
284 list_for_each(&i->dependencies, d, node)
285 printf("\t%s\n", d->dependent->name);
290 static struct ccanlint *find_test(const char *key)
294 list_for_each(&compulsory_tests, i, list)
295 if (streq(i->key, key))
298 list_for_each(&normal_tests, i, list)
299 if (streq(i->key, key))
305 static char *keep_test(const char *testname, void *unused)
307 struct ccanlint *i = find_test(testname);
309 errx(1, "No test %s to --keep", testname);
310 i->keep_results = true;
314 static char *skip_test(const char *testname, void *unused)
316 btree_insert(cmdline_exclude, optarg);
320 static void print_tests(struct list_head *tests, const char *type)
324 printf("%s tests:\n", type);
325 /* This makes them print in topological order. */
326 while ((i = get_next_test(tests)) != NULL) {
327 const struct dependent *d;
328 printf(" %-25s %s\n", i->key, i->name);
330 list_for_each(&i->dependencies, d, node)
331 d->dependent->num_depends--;
335 static char *list_tests(void *arg)
337 print_tests(&compulsory_tests, "Compulsory");
338 print_tests(&normal_tests, "Normal");
342 static char *strip(const void *ctx, const char *line)
344 line += strcspn(line, IDENT_CHARS "-");
345 return talloc_strndup(ctx, line, strspn(line, IDENT_CHARS "-"));
348 static void add_info_fails(struct ccan_file *info)
350 struct doc_section *d;
353 list_for_each(get_ccan_file_docs(info), d, list) {
354 if (!streq(d->type, "fails"))
357 for (i = 0; i < d->num_lines; i++)
358 btree_insert(info_exclude, strip(info, d->lines[i]));
363 static bool depends_on(struct ccanlint *i, struct ccanlint *target)
365 const struct dependent *d;
370 list_for_each(&i->dependencies, d, node) {
371 if (depends_on(d->dependent, target))
377 /* O(N^2), who cares? */
378 static void skip_unrelated_tests(struct ccanlint *target)
381 struct list_head *list;
383 foreach_ptr(list, &compulsory_tests, &normal_tests)
384 list_for_each(list, i, list)
385 if (!depends_on(i, target))
386 i->skip = "not relevant to target";
389 int main(int argc, char *argv[])
391 bool summary = false;
392 unsigned int score = 0, total_score = 0;
395 const char *prefix = "";
396 char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
400 cmdline_exclude = btree_new(btree_strcmp);
401 info_exclude = btree_new(btree_strcmp);
403 opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
404 "use this directory");
405 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
406 "do not compile anything");
407 opt_register_noarg("-l|--list-tests", list_tests, NULL,
408 "list tests ccanlint performs (and exit)");
409 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
410 "keep results of <testname> (can be used multiple times)");
411 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
412 "simply give one line summary");
413 opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
414 "verbose mode (up to -vvvv)");
415 opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
416 "exclude <testname> (can be used multiple times)");
417 opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
419 "ignore (terminate) tests that are slower than this");
420 opt_register_arg("--target <testname>", opt_set_charp,
422 "only run one test (and its prerequisites)");
423 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
424 "\nA program for checking and guiding development"
426 "This usage message");
428 opt_parse(&argc, argv, opt_log_stderr_exit);
431 dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
433 prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
435 compile_verbose = true;
437 tools_verbose = true;
439 /* We move into temporary directory, so gcov dumps its files there. */
440 if (chdir(temp_dir(talloc_autofree_context())) != 0)
441 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
443 m = get_manifest(talloc_autofree_context(), dir);
445 /* Create a symlink from temp dir back to src dir's test directory. */
446 if (symlink(talloc_asprintf(m, "%s/test", dir),
447 talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
448 err(1, "Creating test symlink in %s", temp_dir(NULL));
451 struct ccanlint *test;
453 test = find_test(target);
455 err(1, "Unknown test to run '%s'", target);
456 skip_unrelated_tests(test);
459 /* If you don't pass the compulsory tests, you get a score of 0. */
460 while ((i = get_next_test(&compulsory_tests)) != NULL) {
461 if (!run_test(i, summary, &score, &total_score, m)) {
462 printf("%sTotal score: 0/%u\n", prefix, total_score);
463 errx(1, "%s%s failed", prefix, i->name);
467 add_info_fails(m->info_file);
468 while ((i = get_next_test(&normal_tests)) != NULL)
469 run_test(i, summary, &score, &total_score, m);
471 printf("%sTotal score: %u/%u\n", prefix, score, total_score);