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 *cmdline_exclude;
40 static struct btree *info_exclude;
41 static unsigned int timeout;
43 static void usage(const char *name)
45 fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-t <ms>] [-d <dirname>] [-x <test>]* [-k <test>]*\n"
46 " -v: verbose mode (can specify more than once)\n"
47 " -s: simply give one line summary\n"
48 " -d: use this directory instead of the current one\n"
49 " -n: do not compile anything\n"
50 " -l: list tests ccanlint performs\n"
51 " -k: keep results of this test (can be used multiple times)\n"
52 " -x: exclude tests (can be used multiple times)\n"
53 " -t: ignore (terminate) tests that are slower than this\n",
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 static const char *should_skip(struct manifest *m, struct ccanlint *i)
86 if (btree_lookup(cmdline_exclude, i->key))
87 return "excluded on command line";
89 if (btree_lookup(info_exclude, i->key))
90 return "excluded in _info file";
93 return "dependency failed";
96 return "dependency was skipped";
103 static bool run_test(struct ccanlint *i,
106 unsigned int *total_score,
110 unsigned int this_score, timeleft;
111 const struct dependent *d;
115 //one less test to run through
116 list_for_each(&i->dependencies, d, node)
117 d->dependent->num_depends--;
119 skip = should_skip(m, i);
124 printf(" %s: skipped (%s)\n", i->name, skip);
126 /* If we're skipping this because a prereq failed, we fail. */
128 *total_score += i->total_score;
131 list_add_tail(&finished_tests, &i->list);
132 list_for_each(&i->dependencies, d, node) {
133 d->dependent->skip = true;
134 d->dependent->skip_fail = i->skip_fail;
139 timeleft = timeout ? timeout : default_timeout_ms;
140 result = i->check(m, i->keep_results, &timeleft);
141 if (timeout && timeleft == 0) {
147 this_score = i->total_score ? i->total_score : 1;
149 this_score = i->score(m, result);
153 bad = (this_score == 0);
154 good = (this_score >= i->total_score);
156 if (verbose || (bad && !quiet)) {
157 printf(" %s: %s", i->name,
158 bad ? "FAIL" : good ? "PASS" : "PARTIAL");
161 this_score, i->total_score);
165 if (!quiet && result) {
167 if (i->describe && (desc = i->describe(m, result)) != NULL)
168 printf(" %s\n", desc);
170 i->handle(m, result);
173 if (i->total_score) {
174 *score += this_score;
175 *total_score += i->total_score;
179 list_add_tail(&finished_tests, &i->list);
182 /* Skip any tests which depend on this one. */
183 list_for_each(&i->dependencies, d, node) {
184 d->dependent->skip = true;
185 d->dependent->skip_fail = true;
191 static void register_test(struct list_head *h, struct ccanlint *test, ...)
194 struct ccanlint *depends;
195 struct dependent *dchild;
197 list_add(h, &test->list);
200 /* Careful: we might have been initialized by a dependent. */
201 if (test->dependencies.n.next == NULL)
202 list_head_init(&test->dependencies);
204 //dependent(s) args (if any), last one is NULL
205 while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
206 dchild = malloc(sizeof(*dchild));
207 dchild->dependent = test;
208 /* The thing we depend on might not be initialized yet! */
209 if (depends->dependencies.n.next == NULL)
210 list_head_init(&depends->dependencies);
211 list_add_tail(&depends->dependencies, &dchild->node);
218 * get_next_test - retrieves the next test to be processed
220 static inline struct ccanlint *get_next_test(struct list_head *test)
224 if (list_empty(test))
227 list_for_each(test, i, list) {
228 if (i->num_depends == 0)
231 errx(1, "Can't make process; test dependency cycle");
234 static void init_tests(void)
236 const struct ccanlint *i;
237 struct btree *keys, *names;
240 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
241 #include "generated-normal-tests"
243 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
244 #include "generated-compulsory-tests"
246 /* Self-consistency check: make sure no two tests
247 have the same key or name. */
248 keys = btree_new(btree_strcmp);
249 names = btree_new(btree_strcmp);
250 list_for_each(&compulsory_tests, i, list) {
251 if (!btree_insert(keys, i->key))
252 errx(1, "BUG: Duplicate test key '%s'", i->key);
253 if (!btree_insert(keys, i->name))
254 errx(1, "BUG: Duplicate test name '%s'", i->name);
256 list_for_each(&normal_tests, i, list) {
257 if (!btree_insert(keys, i->key))
258 errx(1, "BUG: Duplicate test key '%s'", i->key);
259 if (!btree_insert(keys, i->name))
260 errx(1, "BUG: Duplicate test name '%s'", i->name);
268 printf("\nCompulsory Tests\n");
269 list_for_each(&compulsory_tests, i, list) {
270 printf("%s depends on %u others\n", i->name, i->num_depends);
271 if (!list_empty(&i->dependencies)) {
272 const struct dependent *d;
273 printf("These depend on us:\n");
274 list_for_each(&i->dependencies, d, node)
275 printf("\t%s\n", d->dependent->name);
279 printf("\nNormal Tests\n");
280 list_for_each(&normal_tests, i, list) {
281 printf("%s depends on %u others\n", i->name, i->num_depends);
282 if (!list_empty(&i->dependencies)) {
283 const struct dependent *d;
284 printf("These depend on us:\n");
285 list_for_each(&i->dependencies, d, node)
286 printf("\t%s\n", d->dependent->name);
291 static struct ccanlint *find_test(const char *key)
295 list_for_each(&compulsory_tests, i, list)
296 if (streq(i->key, key))
299 list_for_each(&normal_tests, i, list)
300 if (streq(i->key, key))
306 static void keep_test(const char *testname)
308 struct ccanlint *i = find_test(testname);
310 errx(1, "No test %s to --keep", testname);
311 i->keep_results = true;
314 static void print_tests(struct list_head *tests, const char *type)
318 printf("%s tests:\n", type);
319 /* This makes them print in topological order. */
320 while ((i = get_next_test(tests)) != NULL) {
321 const struct dependent *d;
322 printf(" %-25s %s\n", i->key, i->name);
324 list_for_each(&i->dependencies, d, node)
325 d->dependent->num_depends--;
329 static void list_tests(void)
331 print_tests(&compulsory_tests, "Compulsory");
332 print_tests(&normal_tests, "Normal");
336 static char *strip(const void *ctx, const char *line)
338 line += strcspn(line, IDENT_CHARS "-");
339 return talloc_strndup(ctx, line, strspn(line, IDENT_CHARS "-"));
342 static void add_info_fails(struct ccan_file *info)
344 struct doc_section *d;
347 list_for_each(get_ccan_file_docs(info), d, list) {
348 if (!streq(d->type, "fails"))
351 for (i = 0; i < d->num_lines; i++)
352 btree_insert(info_exclude, strip(info, d->lines[i]));
357 int main(int argc, char *argv[])
360 bool summary = false;
361 unsigned int score = 0, total_score = 0;
364 const char *prefix = "", *dir = talloc_getcwd(NULL);
368 cmdline_exclude = btree_new(btree_strcmp);
369 info_exclude = btree_new(btree_strcmp);
371 /* I'd love to use long options, but that's not standard. */
372 /* FIXME: popt ccan package? */
373 while ((c = getopt(argc, argv, "sd:vnlx:t:k:")) != -1) {
376 if (optarg[0] != '/')
377 dir = talloc_asprintf_append(NULL, "%s/%s",
381 prefix = talloc_append_string(talloc_basename(NULL,
400 btree_insert(cmdline_exclude, optarg);
403 timeout = atoi(optarg);
405 errx(1, "Invalid timeout %s: 1 ms minumum",
417 compile_verbose = true;
419 tools_verbose = true;
421 /* We move into temporary directory, so gcov dumps its files there. */
422 if (chdir(temp_dir(talloc_autofree_context())) != 0)
423 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
425 m = get_manifest(talloc_autofree_context(), dir);
427 /* Create a symlink from temp dir back to src dir's test directory. */
428 if (symlink(talloc_asprintf(m, "%s/test", dir),
429 talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
430 err(1, "Creating test symlink in %s", temp_dir(NULL));
432 /* If you don't pass the compulsory tests, you don't even get a score */
434 printf("Compulsory tests:\n");
436 while ((i = get_next_test(&compulsory_tests)) != NULL) {
437 if (!run_test(i, summary, &score, &total_score, m)) {
438 errx(1, "%s%s failed", prefix, i->name);
442 add_info_fails(m->info_file);
445 printf("\nNormal tests:\n");
446 while ((i = get_next_test(&normal_tests)) != NULL)
447 run_test(i, summary, &score, &total_score, m);
449 printf("%sTotal score: %u/%u\n", prefix, score, total_score);