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/talloc/talloc.h>
30 static unsigned int verbose = 0;
31 static LIST_HEAD(compulsory_tests);
32 static LIST_HEAD(normal_tests);
33 static LIST_HEAD(finished_tests);
34 bool safe_mode = false;
36 static void usage(const char *name)
38 fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-d <dirname>]\n"
40 " -s: simply give one line per FAIL and total score\n"
41 " -d: use this directory instead of the current one\n"
42 " -n: do not compile anything\n",
47 static void indent_print(const char *string)
50 unsigned int line = strcspn(string, "\n");
51 printf("\t%.*s", line, string);
52 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 bool run_test(struct ccanlint *i,
74 unsigned int *total_score,
78 unsigned int this_score;
79 const struct dependent *d;
82 *total_score += i->total_score;
83 //one less test to run through
84 list_for_each(&i->dependencies, d, node)
85 d->dependent->num_depends--;
89 printf(" %s: OK\n", i->name);
91 *score += i->total_score;
94 list_add_tail(&finished_tests, &i->list);
99 this_score = i->score(m, result);
104 list_add_tail(&finished_tests, &i->list);
106 *score += this_score;
108 printf("%s FAILED (%u/%u)\n",
109 i->name, this_score, i->total_score);
112 indent_print(i->describe(m, result));
114 printf("%s\n", i->describe(m, result));
117 i->handle(m, result);
120 /* Skip any tests which depend on this one. */
121 list_for_each(&i->dependencies, d, node) {
122 list_del(&d->dependent->list);
123 list_add(&finished_tests, &d->dependent->list);
125 printf(" -> skipping %s\n", d->dependent->name);
126 *total_score += d->dependent->total_score;
132 static void register_test(struct ccanlint *test, ...)
135 struct ccanlint *depends;
136 struct dependent *dchild;
138 if (!test->total_score)
139 list_add(&compulsory_tests, &test->list);
141 list_add(&normal_tests, &test->list);
144 /* Careful: we might have been initialized by a dependent. */
145 if (test->dependencies.n.next == NULL)
146 list_head_init(&test->dependencies);
148 //dependent(s) args (if any), last one is NULL
149 while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
150 dchild = malloc(sizeof(*dchild));
151 dchild->dependent = test;
152 /* The thing we depend on might not be initialized yet! */
153 if (depends->dependencies.n.next == NULL)
154 list_head_init(&depends->dependencies);
155 list_add_tail(&depends->dependencies, &dchild->node);
162 * get_next_test - retrieves the next test to be processed
164 static inline struct ccanlint *get_next_test(struct list_head *test)
168 if (list_empty(test))
171 list_for_each(test, i, list) {
172 if (i->num_depends == 0)
175 errx(1, "Can't make process; test dependency cycle");
178 static void init_tests(void)
180 const struct ccanlint *i;
183 #define REGISTER_TEST(name, ...) register_test(&name, __VA_ARGS__)
184 #include "generated-init-tests"
189 printf("\nCompulsory Tests\n");
190 list_for_each(&compulsory_tests, i, list) {
191 printf("%s depends on %u others\n", i->name, i->num_depends);
192 if (!list_empty(&i->dependencies)) {
193 const struct dependent *d;
194 printf("These depend on us:\n");
195 list_for_each(&i->dependencies, d, node)
196 printf("\t%s\n", d->dependent->name);
200 printf("\nNormal Tests\n");
201 list_for_each(&normal_tests, i, list) {
202 printf("%s depends on %u others\n", i->name, i->num_depends);
203 if (!list_empty(&i->dependencies)) {
204 const struct dependent *d;
205 printf("These depend on us:\n");
206 list_for_each(&i->dependencies, d, node)
207 printf("\t%s\n", d->dependent->name);
212 int main(int argc, char *argv[])
215 bool summary = false;
216 unsigned int score, total_score;
220 /* I'd love to use long options, but that's not standard. */
221 /* FIXME: getopt_long ccan package? */
222 while ((c = getopt(argc, argv, "sd:vn")) != -1) {
225 if (chdir(optarg) != 0)
226 err(1, "Changing into directory '%s'", optarg);
245 m = get_manifest(talloc_autofree_context());
249 /* If you don't pass the compulsory tests, you don't even get a score */
251 printf("Compulsory tests:\n");
253 while ((i = get_next_test(&compulsory_tests)) != NULL) {
254 if (!run_test(i, summary, &score, &total_score, m))
259 printf("\nNormal tests:\n");
260 score = total_score = 0;
261 while ((i = get_next_test(&normal_tests)) != NULL) {
263 run_test(i, summary, &score, &total_score, m);
265 printf("Total score: %u/%u\n", score, total_score);