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 static unsigned int verbose = 0;
30 static LIST_HEAD(compulsory_tests);
31 static LIST_HEAD(normal_tests);
32 static LIST_HEAD(finished_tests);
34 static void usage(const char *name)
36 fprintf(stderr, "Usage: %s [-s] [-v] [-d <dirname>]\n"
38 " -s: simply give one line per FAIL and total score\n"
39 " -d: use this directory instead of the current one\n",
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') {
57 bool ask(const char *question)
61 printf("%s ", question);
64 return fgets(reply, sizeof(reply), stdin) != NULL
65 && toupper(reply[0]) == 'Y';
68 static bool run_test(struct ccanlint *i,
71 unsigned int *total_score,
75 unsigned int this_score;
76 const struct dependent *d;
79 *total_score += i->total_score;
80 //one less test to run through
81 list_for_each(&i->dependencies, d, node)
82 d->dependent->num_depends--;
86 printf(" %s: OK\n", i->name);
88 *score += i->total_score;
91 list_add_tail(&finished_tests, &i->list);
96 this_score = i->score(m, result);
100 *score += this_score;
102 printf("%s FAILED (%u/%u)\n",
103 i->name, this_score, i->total_score);
106 indent_print(i->describe(m, result));
108 list_add_tail(&finished_tests, &i->list);
112 printf("%s\n", i->describe(m, result));
115 i->handle(m, result);
117 list_add_tail(&finished_tests, &i->list);
121 static void register_test(struct ccanlint *test, ...)
124 struct ccanlint *depends;
125 struct dependent *dchild;
127 if (!test->total_score)
128 list_add(&compulsory_tests, &test->list);
130 list_add(&normal_tests, &test->list);
133 /* Careful: we might have been initialized by a dependent. */
134 if (test->dependencies.n.next == NULL)
135 list_head_init(&test->dependencies);
137 //dependent(s) args (if any), last one is NULL
138 while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
139 dchild = malloc(sizeof(*dchild));
140 dchild->dependent = test;
141 /* The thing we depend on might not be initialized yet! */
142 if (depends->dependencies.n.next == NULL)
143 list_head_init(&depends->dependencies);
144 list_add_tail(&depends->dependencies, &dchild->node);
151 * get_next_test - retrieves the next test to be processed
153 static inline struct ccanlint *get_next_test(struct list_head *test)
157 if (list_empty(test))
160 list_for_each(test, i, list) {
161 if (i->num_depends == 0)
164 errx(1, "Can't make process; test dependency cycle");
167 static void init_tests(void)
169 const struct ccanlint *i;
172 #define REGISTER_TEST(name, ...) register_test(&name, __VA_ARGS__)
173 #include "generated-init-tests"
178 printf("\nCompulsory Tests\n");
179 list_for_each(&compulsory_tests, i, list) {
180 printf("%s depends on %u others\n", i->name, i->num_depends);
181 if (!list_empty(&i->dependencies)) {
182 const struct dependent *d;
183 printf("These depend on us:\n");
184 list_for_each(&i->dependencies, d, node)
185 printf("\t%s\n", d->dependent->name);
189 printf("\nNormal Tests\n");
190 list_for_each(&normal_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);
201 int main(int argc, char *argv[])
204 bool summary = false;
205 unsigned int score, total_score;
209 /* I'd love to use long options, but that's not standard. */
210 /* FIXME: getopt_long ccan package? */
211 while ((c = getopt(argc, argv, "sd:v")) != -1) {
214 if (chdir(optarg) != 0)
215 err(1, "Changing into directory '%s'", optarg);
235 /* If you don't pass the compulsory tests, you don't even get a score */
237 printf("Compulsory tests:\n");
239 while ((i = get_next_test(&compulsory_tests)) != NULL) {
240 if (!run_test(i, summary, &score, &total_score, m))
245 printf("\nNormal tests:\n");
246 score = total_score = 0;
247 while ((i = get_next_test(&normal_tests)) != NULL) {
249 run_test(i, summary, &score, &total_score, m);
251 printf("Total score: %u/%u\n", score, total_score);