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 const char *should_skip(struct manifest *m, struct ccanlint *i)
74 return "dependency failed";
77 return "dependency was skipped";
84 static bool run_test(struct ccanlint *i,
87 unsigned int *total_score,
91 unsigned int this_score;
92 const struct dependent *d;
95 //one less test to run through
96 list_for_each(&i->dependencies, d, node)
97 d->dependent->num_depends--;
99 skip = should_skip(m, i);
102 printf(" %s: skipped (%s)\n", i->name, skip);
104 /* If we're skipping this because a prereq failed, we fail. */
106 *total_score += i->total_score;
109 list_add_tail(&finished_tests, &i->list);
110 list_for_each(&i->dependencies, d, node) {
111 d->dependent->skip = true;
112 d->dependent->skip_fail = i->skip_fail;
117 result = i->check(m);
120 printf(" %s: OK\n", i->name);
121 if (i->total_score) {
122 *score += i->total_score;
123 *total_score += i->total_score;
127 list_add_tail(&finished_tests, &i->list);
132 this_score = i->score(m, result);
137 list_add_tail(&finished_tests, &i->list);
139 *total_score += i->total_score;
140 *score += this_score;
142 printf("%s FAILED (%u/%u)\n",
143 i->name, this_score, i->total_score);
146 indent_print(i->describe(m, result));
148 printf("%s\n", i->describe(m, result));
151 i->handle(m, result);
154 /* Skip any tests which depend on this one. */
155 list_for_each(&i->dependencies, d, node) {
156 d->dependent->skip = true;
157 d->dependent->skip_fail = true;
163 static void register_test(struct ccanlint *test, ...)
166 struct ccanlint *depends;
167 struct dependent *dchild;
169 if (!test->total_score)
170 list_add(&compulsory_tests, &test->list);
172 list_add(&normal_tests, &test->list);
175 /* Careful: we might have been initialized by a dependent. */
176 if (test->dependencies.n.next == NULL)
177 list_head_init(&test->dependencies);
179 //dependent(s) args (if any), last one is NULL
180 while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
181 dchild = malloc(sizeof(*dchild));
182 dchild->dependent = test;
183 /* The thing we depend on might not be initialized yet! */
184 if (depends->dependencies.n.next == NULL)
185 list_head_init(&depends->dependencies);
186 list_add_tail(&depends->dependencies, &dchild->node);
193 * get_next_test - retrieves the next test to be processed
195 static inline struct ccanlint *get_next_test(struct list_head *test)
199 if (list_empty(test))
202 list_for_each(test, i, list) {
203 if (i->num_depends == 0)
206 errx(1, "Can't make process; test dependency cycle");
209 static void init_tests(void)
211 const struct ccanlint *i;
214 #define REGISTER_TEST(name, ...) register_test(&name, __VA_ARGS__)
215 #include "generated-init-tests"
220 printf("\nCompulsory Tests\n");
221 list_for_each(&compulsory_tests, i, list) {
222 printf("%s depends on %u others\n", i->name, i->num_depends);
223 if (!list_empty(&i->dependencies)) {
224 const struct dependent *d;
225 printf("These depend on us:\n");
226 list_for_each(&i->dependencies, d, node)
227 printf("\t%s\n", d->dependent->name);
231 printf("\nNormal Tests\n");
232 list_for_each(&normal_tests, i, list) {
233 printf("%s depends on %u others\n", i->name, i->num_depends);
234 if (!list_empty(&i->dependencies)) {
235 const struct dependent *d;
236 printf("These depend on us:\n");
237 list_for_each(&i->dependencies, d, node)
238 printf("\t%s\n", d->dependent->name);
243 int main(int argc, char *argv[])
246 bool summary = false;
247 unsigned int score, total_score;
251 /* I'd love to use long options, but that's not standard. */
252 /* FIXME: getopt_long ccan package? */
253 while ((c = getopt(argc, argv, "sd:vn")) != -1) {
256 if (chdir(optarg) != 0)
257 err(1, "Changing into directory '%s'", optarg);
276 m = get_manifest(talloc_autofree_context());
280 /* If you don't pass the compulsory tests, you don't even get a score */
282 printf("Compulsory tests:\n");
284 while ((i = get_next_test(&compulsory_tests)) != NULL) {
285 if (!run_test(i, summary, &score, &total_score, m))
290 printf("\nNormal tests:\n");
291 score = total_score = 0;
292 while ((i = get_next_test(&normal_tests)) != NULL) {
294 run_test(i, summary, &score, &total_score, m);
296 printf("Total score: %u/%u\n", score, total_score);