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.
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>
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;
44 static void indent_print(const char *string)
47 unsigned int line = strcspn(string, "\n");
48 printf("\t%.*s", line, string);
49 if (string[line] == '\n') {
58 bool ask(const char *question)
62 printf("%s ", question);
65 return fgets(reply, sizeof(reply), stdin) != NULL
66 && toupper(reply[0]) == 'Y';
69 static const char *should_skip(struct manifest *m, struct ccanlint *i)
71 if (btree_lookup(cmdline_exclude, i->key))
72 return "excluded on command line";
74 if (btree_lookup(info_exclude, i->key))
75 return "excluded in _info file";
78 return "dependency failed";
81 return "dependency was skipped";
88 static bool run_test(struct ccanlint *i,
91 unsigned int *total_score,
95 unsigned int this_score, max_score, 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 skip = should_skip(m, i);
109 printf(" %s: skipped (%s)\n", i->name, skip);
111 /* If we're skipping this because a prereq failed, we fail. */
113 *total_score += i->total_score;
116 list_add_tail(&finished_tests, &i->list);
117 list_for_each(&i->dependencies, d, node) {
118 d->dependent->skip = true;
119 d->dependent->skip_fail = i->skip_fail;
124 timeleft = timeout ? timeout : default_timeout_ms;
125 result = i->check(m, i->keep_results, &timeleft);
126 if (timeout && timeleft == 0) {
131 max_score = i->total_score;
136 this_score = max_score;
138 this_score = i->score(m, result);
142 bad = (this_score == 0);
143 good = (this_score >= max_score);
145 if (verbose || (!good && !quiet)) {
146 printf(" %s: %s", i->name,
147 bad ? "FAIL" : good ? "PASS" : "PARTIAL");
149 printf(" (+%u/%u)", this_score, max_score);
153 if (!quiet && result) {
155 if (i->describe && (desc = i->describe(m, result)) != NULL)
156 printf(" %s\n", desc);
158 i->handle(m, result);
161 if (i->total_score) {
162 *score += this_score;
163 *total_score += i->total_score;
167 list_add_tail(&finished_tests, &i->list);
170 /* Skip any tests which depend on this one. */
171 list_for_each(&i->dependencies, d, node) {
172 d->dependent->skip = true;
173 d->dependent->skip_fail = true;
179 static void register_test(struct list_head *h, struct ccanlint *test, ...)
182 struct ccanlint *depends;
183 struct dependent *dchild;
185 list_add(h, &test->list);
188 /* Careful: we might have been initialized by a dependent. */
189 if (test->dependencies.n.next == NULL)
190 list_head_init(&test->dependencies);
192 //dependent(s) args (if any), last one is NULL
193 while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
194 dchild = malloc(sizeof(*dchild));
195 dchild->dependent = test;
196 /* The thing we depend on might not be initialized yet! */
197 if (depends->dependencies.n.next == NULL)
198 list_head_init(&depends->dependencies);
199 list_add_tail(&depends->dependencies, &dchild->node);
206 * get_next_test - retrieves the next test to be processed
208 static inline struct ccanlint *get_next_test(struct list_head *test)
212 if (list_empty(test))
215 list_for_each(test, i, list) {
216 if (i->num_depends == 0)
219 errx(1, "Can't make process; test dependency cycle");
222 static void init_tests(void)
224 const struct ccanlint *i;
225 struct btree *keys, *names;
228 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__, NULL)
229 #include "generated-normal-tests"
231 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__, NULL)
232 #include "generated-compulsory-tests"
234 /* Self-consistency check: make sure no two tests
235 have the same key or name. */
236 keys = btree_new(btree_strcmp);
237 names = btree_new(btree_strcmp);
238 list_for_each(&compulsory_tests, i, list) {
239 if (!btree_insert(keys, i->key))
240 errx(1, "BUG: Duplicate test key '%s'", i->key);
241 if (!btree_insert(keys, i->name))
242 errx(1, "BUG: Duplicate test name '%s'", i->name);
244 list_for_each(&normal_tests, i, list) {
245 if (!btree_insert(keys, i->key))
246 errx(1, "BUG: Duplicate test key '%s'", i->key);
247 if (!btree_insert(keys, i->name))
248 errx(1, "BUG: Duplicate test name '%s'", i->name);
256 printf("\nCompulsory Tests\n");
257 list_for_each(&compulsory_tests, i, list) {
258 printf("%s depends on %u others\n", i->name, i->num_depends);
259 if (!list_empty(&i->dependencies)) {
260 const struct dependent *d;
261 printf("These depend on us:\n");
262 list_for_each(&i->dependencies, d, node)
263 printf("\t%s\n", d->dependent->name);
267 printf("\nNormal Tests\n");
268 list_for_each(&normal_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);
279 static struct ccanlint *find_test(const char *key)
283 list_for_each(&compulsory_tests, i, list)
284 if (streq(i->key, key))
287 list_for_each(&normal_tests, i, list)
288 if (streq(i->key, key))
294 static char *keep_test(const char *testname, void *unused)
296 struct ccanlint *i = find_test(testname);
298 errx(1, "No test %s to --keep", testname);
299 i->keep_results = true;
303 static char *skip_test(const char *testname, void *unused)
305 btree_insert(cmdline_exclude, optarg);
309 static void print_tests(struct list_head *tests, const char *type)
313 printf("%s tests:\n", type);
314 /* This makes them print in topological order. */
315 while ((i = get_next_test(tests)) != NULL) {
316 const struct dependent *d;
317 printf(" %-25s %s\n", i->key, i->name);
319 list_for_each(&i->dependencies, d, node)
320 d->dependent->num_depends--;
324 static char *list_tests(void *arg)
326 print_tests(&compulsory_tests, "Compulsory");
327 print_tests(&normal_tests, "Normal");
331 static char *strip(const void *ctx, const char *line)
333 line += strcspn(line, IDENT_CHARS "-");
334 return talloc_strndup(ctx, line, strspn(line, IDENT_CHARS "-"));
337 static void add_info_fails(struct ccan_file *info)
339 struct doc_section *d;
342 list_for_each(get_ccan_file_docs(info), d, list) {
343 if (!streq(d->type, "fails"))
346 for (i = 0; i < d->num_lines; i++)
347 btree_insert(info_exclude, strip(info, d->lines[i]));
352 int main(int argc, char *argv[])
354 bool summary = false;
355 unsigned int score = 0, total_score = 0;
358 const char *prefix = "";
359 char *dir = talloc_getcwd(NULL), *base_dir = dir;
363 cmdline_exclude = btree_new(btree_strcmp);
364 info_exclude = btree_new(btree_strcmp);
366 opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
367 "use this directory");
368 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
369 "do not compile anything");
370 opt_register_noarg("-l|--list-tests", list_tests, NULL,
371 "list tests ccanlint performs (and exit)");
372 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
373 "keep results of <testname> (can be used multiple times)");
374 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
375 "simply give one line summary");
376 opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
377 "verbose mode (can specify more than once)");
378 opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
379 "exclude <testname> (can be used multiple times)");
380 opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
382 "ignore (terminate) tests that are slower than this");
383 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
384 "\nA program for checking and guiding development"
386 "This usage message");
388 opt_parse(&argc, argv, opt_log_stderr_exit);
391 dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
393 prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
395 compile_verbose = true;
397 tools_verbose = true;
399 /* We move into temporary directory, so gcov dumps its files there. */
400 if (chdir(temp_dir(talloc_autofree_context())) != 0)
401 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
403 m = get_manifest(talloc_autofree_context(), dir);
405 /* Create a symlink from temp dir back to src dir's test directory. */
406 if (symlink(talloc_asprintf(m, "%s/test", dir),
407 talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
408 err(1, "Creating test symlink in %s", temp_dir(NULL));
410 /* If you don't pass the compulsory tests, you don't even get a score */
412 printf("Compulsory tests:\n");
414 while ((i = get_next_test(&compulsory_tests)) != NULL) {
415 if (!run_test(i, summary, &score, &total_score, m)) {
416 errx(1, "%s%s failed", prefix, i->name);
420 add_info_fails(m->info_file);
423 printf("\nNormal tests:\n");
424 while ((i = get_next_test(&normal_tests)) != NULL)
425 run_test(i, summary, &score, &total_score, m);
427 printf("%sTotal score: %u/%u\n", prefix, score, total_score);