2 * ccanlint: assorted checks and advice for a ccan package
3 * Copyright (C) 2008 Rusty Russell
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 static unsigned int verbose = 0;
30 static LIST_HEAD(tests);
31 static LIST_HEAD(finished_tests);
33 static void usage(const char *name)
35 fprintf(stderr, "Usage: %s [-s] [-v] [-d <dirname>]\n"
37 " -s: simply give one line per FAIL and total score\n"
38 " -d: use this directory instead of the current one\n",
43 static void indent_print(const char *string)
46 unsigned int line = strcspn(string, "\n");
47 printf("\t%.*s", line, string);
48 if (string[line] == '\n') {
56 bool ask(const char *question)
60 printf("%s ", question);
63 return fgets(reply, sizeof(reply), stdin) != NULL
64 && toupper(reply[0]) == 'Y';
67 static bool run_test(const struct ccanlint *i,
70 unsigned int *total_score,
74 unsigned int this_score;
77 *total_score += i->total_score;
82 printf(" %s: OK\n", i->name);
84 *score += i->total_score;
89 this_score = i->score(m, result);
95 printf("%s FAILED (%u/%u)\n",
96 i->name, this_score, i->total_score);
99 indent_print(i->describe(m, result));
103 printf("%s\n", i->describe(m, result));
106 i->handle(m, result);
111 static void register_test(struct ccanlint *test, ...)
114 struct ccanlint *depends;
115 struct dependent *dchild;
117 list_add(&tests, &test->list);
119 /* Careful: we might have been initialized by a dependent. */
120 if (test->dependencies.n.next == NULL)
121 list_head_init(&test->dependencies);
123 //dependant(s) args (if any), last one is NULL
124 while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
125 dchild = malloc(sizeof(*dchild));
126 dchild->dependent = test;
127 /* The thing we depend on might not be initialized yet! */
128 if (depends->dependencies.n.next == NULL)
129 list_head_init(&depends->dependencies);
130 list_add_tail(&depends->dependencies, &dchild->node);
136 static void init_tests(void)
138 const struct ccanlint *i;
141 #define REGISTER_TEST(name, ...) register_test(&name, __VA_ARGS__)
142 #include "generated-init-tests"
147 list_for_each(&tests, i, list) {
148 printf("%s depends on %u others\n", i->name, i->num_depends);
149 if (!list_empty(&i->dependencies)) {
150 const struct dependent *d;
151 printf("These depend on us:\n");
152 list_for_each(&i->dependencies, d, node)
153 printf("\t%s\n", d->dependent->name);
158 int main(int argc, char *argv[])
161 bool summary = false;
162 unsigned int score, total_score;
164 const struct ccanlint *i;
166 /* I'd love to use long options, but that's not standard. */
167 /* FIXME: getopt_long ccan package? */
168 while ((c = getopt(argc, argv, "sd:v")) != -1) {
171 if (chdir(optarg) != 0)
172 err(1, "Changing into directory '%s'", optarg);
192 /* If you don't pass the compulsory tests, you don't even get a score */
194 printf("Compulsory tests:\n");
195 list_for_each(&tests, i, list)
197 if (!run_test(i, summary, &score, &total_score, m))
201 printf("\nNormal tests:\n");
202 score = total_score = 0;
203 list_for_each(&tests, i, list)
205 run_test(i, summary, &score, &total_score, m);
207 printf("Total score: %u/%u\n", score, total_score);