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/talloc/talloc.h>
31 static unsigned int verbose = 0;
32 static LIST_HEAD(compulsory_tests);
33 static LIST_HEAD(normal_tests);
34 static LIST_HEAD(finished_tests);
35 bool safe_mode = false;
37 static void usage(const char *name)
39 fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-d <dirname>]\n"
41 " -s: simply give one line summary\n"
42 " -d: use this directory instead of the current one\n"
43 " -n: do not compile anything\n",
49 static void indent_print(const char *string)
52 unsigned int line = strcspn(string, "\n");
53 printf("\t%.*s", line, string);
54 if (string[line] == '\n') {
63 bool ask(const char *question)
67 printf("%s ", question);
70 return fgets(reply, sizeof(reply), stdin) != NULL
71 && toupper(reply[0]) == 'Y';
74 static const char *should_skip(struct manifest *m, struct ccanlint *i)
77 return "dependency failed";
80 return "dependency was skipped";
87 static bool run_test(struct ccanlint *i,
90 unsigned int *total_score,
94 unsigned int this_score;
95 const struct dependent *d;
98 //one less test to run through
99 list_for_each(&i->dependencies, d, node)
100 d->dependent->num_depends--;
102 skip = should_skip(m, i);
105 printf(" %s: skipped (%s)\n", i->name, skip);
107 /* If we're skipping this because a prereq failed, we fail. */
109 *total_score += i->total_score;
112 list_add_tail(&finished_tests, &i->list);
113 list_for_each(&i->dependencies, d, node) {
114 d->dependent->skip = true;
115 d->dependent->skip_fail = i->skip_fail;
120 result = i->check(m);
123 printf(" %s: OK\n", i->name);
124 if (i->total_score) {
125 *score += i->total_score;
126 *total_score += i->total_score;
130 list_add_tail(&finished_tests, &i->list);
135 this_score = i->score(m, result);
140 list_add_tail(&finished_tests, &i->list);
142 *total_score += i->total_score;
143 *score += this_score;
145 printf("%s\n", i->describe(m, result));
148 i->handle(m, result);
151 /* Skip any tests which depend on this one. */
152 list_for_each(&i->dependencies, d, node) {
153 d->dependent->skip = true;
154 d->dependent->skip_fail = true;
160 static void register_test(struct list_head *h, struct ccanlint *test, ...)
163 struct ccanlint *depends;
164 struct dependent *dchild;
166 list_add(h, &test->list);
169 /* Careful: we might have been initialized by a dependent. */
170 if (test->dependencies.n.next == NULL)
171 list_head_init(&test->dependencies);
173 //dependent(s) args (if any), last one is NULL
174 while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
175 dchild = malloc(sizeof(*dchild));
176 dchild->dependent = test;
177 /* The thing we depend on might not be initialized yet! */
178 if (depends->dependencies.n.next == NULL)
179 list_head_init(&depends->dependencies);
180 list_add_tail(&depends->dependencies, &dchild->node);
187 * get_next_test - retrieves the next test to be processed
189 static inline struct ccanlint *get_next_test(struct list_head *test)
193 if (list_empty(test))
196 list_for_each(test, i, list) {
197 if (i->num_depends == 0)
200 errx(1, "Can't make process; test dependency cycle");
203 static void init_tests(void)
205 const struct ccanlint *i;
208 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
209 #include "generated-normal-tests"
211 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
212 #include "generated-compulsory-tests"
217 printf("\nCompulsory Tests\n");
218 list_for_each(&compulsory_tests, i, list) {
219 printf("%s depends on %u others\n", i->name, i->num_depends);
220 if (!list_empty(&i->dependencies)) {
221 const struct dependent *d;
222 printf("These depend on us:\n");
223 list_for_each(&i->dependencies, d, node)
224 printf("\t%s\n", d->dependent->name);
228 printf("\nNormal Tests\n");
229 list_for_each(&normal_tests, i, list) {
230 printf("%s depends on %u others\n", i->name, i->num_depends);
231 if (!list_empty(&i->dependencies)) {
232 const struct dependent *d;
233 printf("These depend on us:\n");
234 list_for_each(&i->dependencies, d, node)
235 printf("\t%s\n", d->dependent->name);
240 int main(int argc, char *argv[])
243 bool summary = false;
244 unsigned int score = 0, total_score = 0;
247 const char *prefix = "", *dir = ".";
249 /* I'd love to use long options, but that's not standard. */
250 /* FIXME: getopt_long ccan package? */
251 while ((c = getopt(argc, argv, "sd:vn")) != -1) {
255 prefix = talloc_append_string(talloc_basename(NULL,
258 if (chdir(optarg) != 0)
259 err(1, "Changing into directory '%s'", optarg);
278 m = get_manifest(talloc_autofree_context(), dir);
282 /* If you don't pass the compulsory tests, you don't even get a score */
284 printf("Compulsory tests:\n");
286 while ((i = get_next_test(&compulsory_tests)) != NULL) {
287 if (!run_test(i, summary, &score, &total_score, m)) {
288 errx(1, "%s%s failed", prefix, i->name);
293 printf("\nNormal tests:\n");
294 while ((i = get_next_test(&normal_tests)) != NULL)
295 run_test(i, summary, &score, &total_score, m);
297 printf("%sTotal score: %u/%u\n", prefix, score, total_score);