ccanlint.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * ccanlint: assorted checks and advice for a ccan package
  3. * Copyright (C) 2008 Rusty Russell, Idris Soule
  4. *
  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)
  8. * any later version.
  9. *
  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
  13. * more details.
  14. *
  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.
  18. */
  19. #include "ccanlint.h"
  20. #include "../tools.h"
  21. #include <unistd.h>
  22. #include <getopt.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <err.h>
  28. #include <ctype.h>
  29. #include <ccan/btree/btree.h>
  30. #include <ccan/str/str.h>
  31. #include <ccan/str_talloc/str_talloc.h>
  32. #include <ccan/talloc/talloc.h>
  33. static unsigned int verbose = 0;
  34. static LIST_HEAD(compulsory_tests);
  35. static LIST_HEAD(normal_tests);
  36. static LIST_HEAD(finished_tests);
  37. bool safe_mode = false;
  38. static struct btree *exclude;
  39. static unsigned int timeout;
  40. static void usage(const char *name)
  41. {
  42. fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-t <ms>] [-d <dirname>] [-x <tests>] [-k <test>]*\n"
  43. " -v: verbose mode\n"
  44. " -s: simply give one line summary\n"
  45. " -d: use this directory instead of the current one\n"
  46. " -n: do not compile anything\n"
  47. " -l: list tests ccanlint performs\n"
  48. " -k: keep results of this test (can be used multiple times)\n"
  49. " -x: exclude tests (e.g. -x trailing_whitespace,valgrind)\n"
  50. " -t: ignore (terminate) tests that are slower than this\n",
  51. name);
  52. exit(1);
  53. }
  54. #if 0
  55. static void indent_print(const char *string)
  56. {
  57. while (*string) {
  58. unsigned int line = strcspn(string, "\n");
  59. printf("\t%.*s", line, string);
  60. if (string[line] == '\n') {
  61. printf("\n");
  62. line++;
  63. }
  64. string += line;
  65. }
  66. }
  67. #endif
  68. bool ask(const char *question)
  69. {
  70. char reply[2];
  71. printf("%s ", question);
  72. fflush(stdout);
  73. return fgets(reply, sizeof(reply), stdin) != NULL
  74. && toupper(reply[0]) == 'Y';
  75. }
  76. static const char *should_skip(struct manifest *m, struct ccanlint *i)
  77. {
  78. if (btree_lookup(exclude, i->key))
  79. return "excluded on command line";
  80. if (i->skip_fail)
  81. return "dependency failed";
  82. if (i->skip)
  83. return "dependency was skipped";
  84. if (i->can_run)
  85. return i->can_run(m);
  86. return NULL;
  87. }
  88. static bool run_test(struct ccanlint *i,
  89. bool quiet,
  90. unsigned int *score,
  91. unsigned int *total_score,
  92. struct manifest *m)
  93. {
  94. void *result;
  95. unsigned int this_score, timeleft;
  96. const struct dependent *d;
  97. const char *skip;
  98. //one less test to run through
  99. list_for_each(&i->dependencies, d, node)
  100. d->dependent->num_depends--;
  101. skip = should_skip(m, i);
  102. if (skip) {
  103. skip:
  104. if (verbose)
  105. printf(" %s: skipped (%s)\n", i->name, skip);
  106. /* If we're skipping this because a prereq failed, we fail. */
  107. if (i->skip_fail)
  108. *total_score += i->total_score;
  109. list_del(&i->list);
  110. list_add_tail(&finished_tests, &i->list);
  111. list_for_each(&i->dependencies, d, node) {
  112. d->dependent->skip = true;
  113. d->dependent->skip_fail = i->skip_fail;
  114. }
  115. return true;
  116. }
  117. timeleft = timeout ? timeout : default_timeout_ms;
  118. result = i->check(m, i->keep_results, &timeleft);
  119. if (timeout && timeleft == 0) {
  120. skip = "timeout";
  121. goto skip;
  122. }
  123. if (!result) {
  124. if (verbose) {
  125. printf(" %s: OK", i->name);
  126. if (i->total_score)
  127. printf(" (+%u/%u)",
  128. i->total_score, i->total_score);
  129. printf("\n");
  130. }
  131. if (i->total_score) {
  132. *score += i->total_score;
  133. *total_score += i->total_score;
  134. }
  135. list_del(&i->list);
  136. list_add_tail(&finished_tests, &i->list);
  137. return true;
  138. }
  139. if (i->score)
  140. this_score = i->score(m, result);
  141. else
  142. this_score = 0;
  143. list_del(&i->list);
  144. list_add_tail(&finished_tests, &i->list);
  145. *total_score += i->total_score;
  146. *score += this_score;
  147. if (verbose) {
  148. printf(" %s: FAIL (+%u/%u)\n",
  149. i->name, this_score, i->total_score);
  150. }
  151. if (!quiet) {
  152. printf("%s\n", i->describe(m, result));
  153. if (i->handle)
  154. i->handle(m, result);
  155. }
  156. /* Skip any tests which depend on this one. */
  157. list_for_each(&i->dependencies, d, node) {
  158. d->dependent->skip = true;
  159. d->dependent->skip_fail = true;
  160. }
  161. return false;
  162. }
  163. static void register_test(struct list_head *h, struct ccanlint *test, ...)
  164. {
  165. va_list ap;
  166. struct ccanlint *depends;
  167. struct dependent *dchild;
  168. list_add(h, &test->list);
  169. va_start(ap, test);
  170. /* Careful: we might have been initialized by a dependent. */
  171. if (test->dependencies.n.next == NULL)
  172. list_head_init(&test->dependencies);
  173. //dependent(s) args (if any), last one is NULL
  174. while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
  175. dchild = malloc(sizeof(*dchild));
  176. dchild->dependent = test;
  177. /* The thing we depend on might not be initialized yet! */
  178. if (depends->dependencies.n.next == NULL)
  179. list_head_init(&depends->dependencies);
  180. list_add_tail(&depends->dependencies, &dchild->node);
  181. test->num_depends++;
  182. }
  183. va_end(ap);
  184. }
  185. /**
  186. * get_next_test - retrieves the next test to be processed
  187. **/
  188. static inline struct ccanlint *get_next_test(struct list_head *test)
  189. {
  190. struct ccanlint *i;
  191. if (list_empty(test))
  192. return NULL;
  193. list_for_each(test, i, list) {
  194. if (i->num_depends == 0)
  195. return i;
  196. }
  197. errx(1, "Can't make process; test dependency cycle");
  198. }
  199. static void init_tests(void)
  200. {
  201. const struct ccanlint *i;
  202. struct btree *keys, *names;
  203. #undef REGISTER_TEST
  204. #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
  205. #include "generated-normal-tests"
  206. #undef REGISTER_TEST
  207. #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
  208. #include "generated-compulsory-tests"
  209. /* Self-consistency check: make sure no two tests
  210. have the same key or name. */
  211. keys = btree_new(btree_strcmp);
  212. names = btree_new(btree_strcmp);
  213. list_for_each(&compulsory_tests, i, list) {
  214. if (!btree_insert(keys, i->key))
  215. errx(1, "BUG: Duplicate test key '%s'", i->key);
  216. if (!btree_insert(keys, i->name))
  217. errx(1, "BUG: Duplicate test name '%s'", i->name);
  218. }
  219. list_for_each(&normal_tests, i, list) {
  220. if (!btree_insert(keys, i->key))
  221. errx(1, "BUG: Duplicate test key '%s'", i->key);
  222. if (!btree_insert(keys, i->name))
  223. errx(1, "BUG: Duplicate test name '%s'", i->name);
  224. }
  225. btree_delete(keys);
  226. btree_delete(names);
  227. if (!verbose)
  228. return;
  229. printf("\nCompulsory Tests\n");
  230. list_for_each(&compulsory_tests, i, list) {
  231. printf("%s depends on %u others\n", i->name, i->num_depends);
  232. if (!list_empty(&i->dependencies)) {
  233. const struct dependent *d;
  234. printf("These depend on us:\n");
  235. list_for_each(&i->dependencies, d, node)
  236. printf("\t%s\n", d->dependent->name);
  237. }
  238. }
  239. printf("\nNormal Tests\n");
  240. list_for_each(&normal_tests, i, list) {
  241. printf("%s depends on %u others\n", i->name, i->num_depends);
  242. if (!list_empty(&i->dependencies)) {
  243. const struct dependent *d;
  244. printf("These depend on us:\n");
  245. list_for_each(&i->dependencies, d, node)
  246. printf("\t%s\n", d->dependent->name);
  247. }
  248. }
  249. }
  250. static struct ccanlint *find_test(const char *key)
  251. {
  252. struct ccanlint *i;
  253. list_for_each(&compulsory_tests, i, list)
  254. if (streq(i->key, key))
  255. return i;
  256. list_for_each(&normal_tests, i, list)
  257. if (streq(i->key, key))
  258. return i;
  259. return NULL;
  260. }
  261. static void keep_test(const char *testname)
  262. {
  263. struct ccanlint *i = find_test(testname);
  264. if (!i)
  265. errx(1, "No test %s to --keep", testname);
  266. i->keep_results = true;
  267. }
  268. static void print_tests(struct list_head *tests, const char *type)
  269. {
  270. struct ccanlint *i;
  271. printf("%s tests:\n", type);
  272. /* This makes them print in topological order. */
  273. while ((i = get_next_test(tests)) != NULL) {
  274. const struct dependent *d;
  275. printf(" %-25s %s\n", i->key, i->name);
  276. list_del(&i->list);
  277. list_for_each(&i->dependencies, d, node)
  278. d->dependent->num_depends--;
  279. }
  280. }
  281. static void list_tests(void)
  282. {
  283. print_tests(&compulsory_tests, "Compulsory");
  284. print_tests(&normal_tests, "Normal");
  285. exit(0);
  286. }
  287. int main(int argc, char *argv[])
  288. {
  289. int c;
  290. bool summary = false;
  291. unsigned int score = 0, total_score = 0;
  292. struct manifest *m;
  293. struct ccanlint *i;
  294. const char *prefix = "", *dir = ".";
  295. init_tests();
  296. exclude = btree_new(btree_strcmp);
  297. /* I'd love to use long options, but that's not standard. */
  298. /* FIXME: getopt_long ccan package? */
  299. while ((c = getopt(argc, argv, "sd:vnlx:t:k:")) != -1) {
  300. switch (c) {
  301. case 'd':
  302. dir = optarg;
  303. prefix = talloc_append_string(talloc_basename(NULL,
  304. optarg),
  305. ": ");
  306. break;
  307. case 'l':
  308. list_tests();
  309. case 's':
  310. summary = true;
  311. break;
  312. case 'v':
  313. verbose++;
  314. break;
  315. case 'n':
  316. safe_mode = true;
  317. break;
  318. case 'k':
  319. keep_test(optarg);
  320. break;
  321. case 'x': {
  322. char **exclude_strs = strsplit(NULL, optarg, ",", NULL);
  323. size_t i;
  324. for (i = 0; exclude_strs[i]; i++)
  325. btree_insert(exclude, exclude_strs[i]);
  326. } break;
  327. case 't':
  328. timeout = atoi(optarg);
  329. if (!timeout)
  330. errx(1, "Invalid timeout %s: 1 ms minumum",
  331. optarg);
  332. break;
  333. default:
  334. usage(argv[0]);
  335. }
  336. }
  337. if (optind < argc)
  338. usage(argv[0]);
  339. m = get_manifest(talloc_autofree_context(), dir);
  340. /* If you don't pass the compulsory tests, you don't even get a score */
  341. if (verbose)
  342. printf("Compulsory tests:\n");
  343. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  344. if (!run_test(i, summary, &score, &total_score, m)) {
  345. errx(1, "%s%s failed", prefix, i->name);
  346. }
  347. }
  348. if (verbose)
  349. printf("\nNormal tests:\n");
  350. while ((i = get_next_test(&normal_tests)) != NULL)
  351. run_test(i, summary, &score, &total_score, m);
  352. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  353. return 0;
  354. }