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);
103 *score += this_score;
105 printf("%s FAILED (%u/%u)\n",
106 i->name, this_score, i->total_score);
109 indent_print(i->describe(m, result));
111 list_add_tail(&finished_tests, &i->list);
115 printf("%s\n", i->describe(m, result));
118 i->handle(m, result);
120 list_add_tail(&finished_tests, &i->list);
124 static void register_test(struct ccanlint *test, ...)
127 struct ccanlint *depends;
128 struct dependent *dchild;
130 if (!test->total_score)
131 list_add(&compulsory_tests, &test->list);
133 list_add(&normal_tests, &test->list);
136 /* Careful: we might have been initialized by a dependent. */
137 if (test->dependencies.n.next == NULL)
138 list_head_init(&test->dependencies);
140 //dependent(s) args (if any), last one is NULL
141 while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
142 dchild = malloc(sizeof(*dchild));
143 dchild->dependent = test;
144 /* The thing we depend on might not be initialized yet! */
145 if (depends->dependencies.n.next == NULL)
146 list_head_init(&depends->dependencies);
147 list_add_tail(&depends->dependencies, &dchild->node);
154 * get_next_test - retrieves the next test to be processed
156 static inline struct ccanlint *get_next_test(struct list_head *test)
160 if (list_empty(test))
163 list_for_each(test, i, list) {
164 if (i->num_depends == 0)
167 errx(1, "Can't make process; test dependency cycle");
170 static void init_tests(void)
172 const struct ccanlint *i;
175 #define REGISTER_TEST(name, ...) register_test(&name, __VA_ARGS__)
176 #include "generated-init-tests"
181 printf("\nCompulsory Tests\n");
182 list_for_each(&compulsory_tests, i, list) {
183 printf("%s depends on %u others\n", i->name, i->num_depends);
184 if (!list_empty(&i->dependencies)) {
185 const struct dependent *d;
186 printf("These depend on us:\n");
187 list_for_each(&i->dependencies, d, node)
188 printf("\t%s\n", d->dependent->name);
192 printf("\nNormal Tests\n");
193 list_for_each(&normal_tests, i, list) {
194 printf("%s depends on %u others\n", i->name, i->num_depends);
195 if (!list_empty(&i->dependencies)) {
196 const struct dependent *d;
197 printf("These depend on us:\n");
198 list_for_each(&i->dependencies, d, node)
199 printf("\t%s\n", d->dependent->name);
204 int main(int argc, char *argv[])
207 bool summary = false;
208 unsigned int score, total_score;
212 /* I'd love to use long options, but that's not standard. */
213 /* FIXME: getopt_long ccan package? */
214 while ((c = getopt(argc, argv, "sd:vn")) != -1) {
217 if (chdir(optarg) != 0)
218 err(1, "Changing into directory '%s'", optarg);
237 m = get_manifest(talloc_autofree_context());
241 /* If you don't pass the compulsory tests, you don't even get a score */
243 printf("Compulsory tests:\n");
245 while ((i = get_next_test(&compulsory_tests)) != NULL) {
246 if (!run_test(i, summary, &score, &total_score, m))
251 printf("\nNormal tests:\n");
252 score = total_score = 0;
253 while ((i = get_next_test(&normal_tests)) != NULL) {
255 run_test(i, summary, &score, &total_score, m);
257 printf("Total score: %u/%u\n", score, total_score);