ccanlint.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * ccanlint: assorted checks and advice for a ccan package
  3. * Copyright (C) 2008 Rusty Russell, Idris Soule
  4. * Copyright (C) 2010 Rusty Russell, Idris Soule
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "ccanlint.h"
  21. #include "../tools.h"
  22. #include <unistd.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <err.h>
  27. #include <ctype.h>
  28. #include <ccan/btree/btree.h>
  29. #include <ccan/str/str.h>
  30. #include <ccan/str_talloc/str_talloc.h>
  31. #include <ccan/talloc/talloc.h>
  32. #include <ccan/opt/opt.h>
  33. #include <ccan/foreach/foreach.h>
  34. #include <ccan/grab_file/grab_file.h>
  35. #include <ccan/cast/cast.h>
  36. int verbose = 0;
  37. static struct list_head compulsory_tests;
  38. static struct list_head normal_tests;
  39. bool safe_mode = false;
  40. static struct btree *cmdline_exclude;
  41. static struct btree *info_exclude;
  42. static unsigned int timeout;
  43. /* These are overridden at runtime if we can find config.h */
  44. const char *compiler = NULL;
  45. const char *cflags = NULL;
  46. const char *config_header;
  47. #if 0
  48. static void indent_print(const char *string)
  49. {
  50. while (*string) {
  51. unsigned int line = strcspn(string, "\n");
  52. printf("\t%.*s", line, string);
  53. if (string[line] == '\n') {
  54. printf("\n");
  55. line++;
  56. }
  57. string += line;
  58. }
  59. }
  60. #endif
  61. bool ask(const char *question)
  62. {
  63. char reply[80];
  64. printf("%s ", question);
  65. fflush(stdout);
  66. return fgets(reply, sizeof(reply), stdin) != NULL
  67. && toupper(reply[0]) == 'Y';
  68. }
  69. static const char *should_skip(struct manifest *m, struct ccanlint *i)
  70. {
  71. if (btree_lookup(cmdline_exclude, i->key))
  72. return "excluded on command line";
  73. if (btree_lookup(info_exclude, i->key))
  74. return "excluded in _info file";
  75. if (i->skip)
  76. return i->skip;
  77. if (i->skip_fail)
  78. return "dependency failed";
  79. if (i->can_run)
  80. return i->can_run(m);
  81. return NULL;
  82. }
  83. static bool run_test(struct ccanlint *i,
  84. bool quiet,
  85. unsigned int *running_score,
  86. unsigned int *running_total,
  87. struct manifest *m,
  88. const char *prefix)
  89. {
  90. unsigned int timeleft;
  91. const struct dependent *d;
  92. const char *skip;
  93. struct score *score;
  94. //one less test to run through
  95. list_for_each(&i->dependencies, d, node)
  96. d->dependent->num_depends--;
  97. score = talloc(m, struct score);
  98. list_head_init(&score->per_file_errors);
  99. score->error = NULL;
  100. score->pass = false;
  101. score->score = 0;
  102. score->total = 1;
  103. skip = should_skip(m, i);
  104. if (skip) {
  105. skip:
  106. if (verbose && !streq(skip, "not relevant to target"))
  107. printf("%s%s: skipped (%s)\n", prefix, i->name, skip);
  108. /* If we're skipping this because a prereq failed, we fail:
  109. * count it as a score of 1. */
  110. if (i->skip_fail)
  111. (*running_total)++;
  112. list_del(&i->list);
  113. list_for_each(&i->dependencies, d, node) {
  114. if (d->dependent->skip)
  115. continue;
  116. d->dependent->skip = "dependency was skipped";
  117. d->dependent->skip_fail = i->skip_fail;
  118. }
  119. return i->skip_fail ? false : true;
  120. }
  121. timeleft = timeout ? timeout : default_timeout_ms;
  122. i->check(m, i->keep_results, &timeleft, score);
  123. if (timeout && timeleft == 0) {
  124. skip = "timeout";
  125. goto skip;
  126. }
  127. assert(score->score <= score->total);
  128. if ((!score->pass && !quiet)
  129. || (score->score < score->total && verbose)
  130. || verbose > 1) {
  131. printf("%s%s (%s): %s",
  132. prefix, i->name, i->key, score->pass ? "PASS" : "FAIL");
  133. if (score->total > 1)
  134. printf(" (+%u/%u)", score->score, score->total);
  135. printf("\n");
  136. }
  137. if ((!quiet && !score->pass) || verbose) {
  138. if (score->error) {
  139. printf("%s%s", score->error,
  140. strends(score->error, "\n") ? "" : "\n");
  141. }
  142. }
  143. if (!quiet && score->score < score->total && i->handle)
  144. i->handle(m, score);
  145. *running_score += score->score;
  146. *running_total += score->total;
  147. list_del(&i->list);
  148. if (!score->pass) {
  149. /* Skip any tests which depend on this one. */
  150. list_for_each(&i->dependencies, d, node) {
  151. if (d->dependent->skip)
  152. continue;
  153. d->dependent->skip = "dependency failed";
  154. d->dependent->skip_fail = true;
  155. }
  156. }
  157. return score->pass;
  158. }
  159. static void register_test(struct list_head *h, struct ccanlint *test)
  160. {
  161. list_add(h, &test->list);
  162. test->options = talloc_array(NULL, char *, 1);
  163. test->options[0] = NULL;
  164. test->skip = NULL;
  165. test->skip_fail = false;
  166. }
  167. /**
  168. * get_next_test - retrieves the next test to be processed
  169. **/
  170. static inline struct ccanlint *get_next_test(struct list_head *test)
  171. {
  172. struct ccanlint *i;
  173. if (list_empty(test))
  174. return NULL;
  175. list_for_each(test, i, list) {
  176. if (i->num_depends == 0)
  177. return i;
  178. }
  179. errx(1, "Can't make process; test dependency cycle");
  180. }
  181. static struct ccanlint *find_test(const char *key)
  182. {
  183. struct ccanlint *i;
  184. list_for_each(&compulsory_tests, i, list)
  185. if (streq(i->key, key))
  186. return i;
  187. list_for_each(&normal_tests, i, list)
  188. if (streq(i->key, key))
  189. return i;
  190. return NULL;
  191. }
  192. bool is_excluded(const char *name)
  193. {
  194. return btree_lookup(cmdline_exclude, name) != NULL
  195. || btree_lookup(info_exclude, name) != NULL
  196. || find_test(name)->skip != NULL;
  197. }
  198. #undef REGISTER_TEST
  199. #define REGISTER_TEST(name, ...) extern struct ccanlint name
  200. #include "generated-normal-tests"
  201. #include "generated-compulsory-tests"
  202. static void init_tests(void)
  203. {
  204. struct ccanlint *c;
  205. struct btree *keys, *names;
  206. struct list_head *list;
  207. list_head_init(&normal_tests);
  208. list_head_init(&compulsory_tests);
  209. #undef REGISTER_TEST
  210. #define REGISTER_TEST(name) register_test(&normal_tests, &name)
  211. #include "generated-normal-tests"
  212. #undef REGISTER_TEST
  213. #define REGISTER_TEST(name) register_test(&compulsory_tests, &name)
  214. #include "generated-compulsory-tests"
  215. /* Initialize dependency lists. */
  216. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  217. list_for_each(list, c, list) {
  218. list_head_init(&c->dependencies);
  219. c->num_depends = 0;
  220. }
  221. }
  222. /* Resolve dependencies. */
  223. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  224. list_for_each(list, c, list) {
  225. char **deps = strsplit(NULL, c->needs, " ");
  226. unsigned int i;
  227. for (i = 0; deps[i]; i++) {
  228. struct ccanlint *dep;
  229. struct dependent *dchild;
  230. dep = find_test(deps[i]);
  231. if (!dep)
  232. errx(1, "BUG: unknown dep '%s' for %s",
  233. deps[i], c->key);
  234. dchild = talloc(NULL, struct dependent);
  235. dchild->dependent = c;
  236. list_add_tail(&dep->dependencies,
  237. &dchild->node);
  238. c->num_depends++;
  239. }
  240. talloc_free(deps);
  241. }
  242. }
  243. /* Self-consistency check: make sure no two tests
  244. have the same key or name. */
  245. keys = btree_new(btree_strcmp);
  246. names = btree_new(btree_strcmp);
  247. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  248. list_for_each(list, c, list) {
  249. if (!btree_insert(keys, c->key))
  250. errx(1, "BUG: Duplicate test key '%s'",
  251. c->key);
  252. if (!btree_insert(names, c->name))
  253. errx(1, "BUG: Duplicate test name '%s'",
  254. c->name);
  255. }
  256. }
  257. btree_delete(keys);
  258. btree_delete(names);
  259. }
  260. static void print_test_depends(void)
  261. {
  262. struct list_head *list;
  263. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  264. struct ccanlint *c;
  265. printf("\%s Tests\n",
  266. list == &compulsory_tests ? "Compulsory" : "Normal");
  267. list_for_each(list, c, list) {
  268. if (!list_empty(&c->dependencies)) {
  269. const struct dependent *d;
  270. printf("These depend on %s:\n", c->key);
  271. list_for_each(&c->dependencies, d, node)
  272. printf("\t%s\n", d->dependent->key);
  273. }
  274. }
  275. }
  276. }
  277. static int show_tmpdir(const char *dir)
  278. {
  279. printf("You can find ccanlint working files in '%s'\n", dir);
  280. return 0;
  281. }
  282. static char *keep_test(const char *testname, void *unused)
  283. {
  284. struct ccanlint *i;
  285. if (streq(testname, "all")) {
  286. struct list_head *list;
  287. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  288. list_for_each(list, i, list)
  289. i->keep_results = true;
  290. }
  291. } else {
  292. i = find_test(testname);
  293. if (!i)
  294. errx(1, "No test %s to --keep", testname);
  295. i->keep_results = true;
  296. }
  297. /* Don't automatically destroy temporary dir. */
  298. talloc_set_destructor(temp_dir(NULL), show_tmpdir);
  299. return NULL;
  300. }
  301. static char *skip_test(const char *testname, void *unused)
  302. {
  303. btree_insert(cmdline_exclude, testname);
  304. return NULL;
  305. }
  306. static void print_tests(struct list_head *tests, const char *type)
  307. {
  308. struct ccanlint *i;
  309. printf("%s tests:\n", type);
  310. /* This makes them print in topological order. */
  311. while ((i = get_next_test(tests)) != NULL) {
  312. const struct dependent *d;
  313. printf(" %-25s %s\n", i->key, i->name);
  314. list_del(&i->list);
  315. list_for_each(&i->dependencies, d, node)
  316. d->dependent->num_depends--;
  317. }
  318. }
  319. static char *list_tests(void *arg)
  320. {
  321. init_tests();
  322. print_tests(&compulsory_tests, "Compulsory");
  323. print_tests(&normal_tests, "Normal");
  324. exit(0);
  325. }
  326. static void test_dgraph_vertices(struct list_head *tests, const char *style)
  327. {
  328. const struct ccanlint *i;
  329. list_for_each(tests, i, list) {
  330. /*
  331. * todo: escape labels in case ccanlint test keys have
  332. * characters interpreted as GraphViz syntax.
  333. */
  334. printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
  335. }
  336. }
  337. static void test_dgraph_edges(struct list_head *tests)
  338. {
  339. const struct ccanlint *i;
  340. const struct dependent *d;
  341. list_for_each(tests, i, list)
  342. list_for_each(&i->dependencies, d, node)
  343. printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
  344. }
  345. static char *test_dependency_graph(void *arg)
  346. {
  347. init_tests();
  348. puts("digraph G {");
  349. test_dgraph_vertices(&compulsory_tests, ", style=filled, fillcolor=yellow");
  350. test_dgraph_vertices(&normal_tests, "");
  351. test_dgraph_edges(&compulsory_tests);
  352. test_dgraph_edges(&normal_tests);
  353. puts("}");
  354. exit(0);
  355. }
  356. /* Remove empty lines. */
  357. static char **collapse(char **lines, unsigned int *nump)
  358. {
  359. unsigned int i, j;
  360. for (i = j = 0; lines[i]; i++) {
  361. if (lines[i][0])
  362. lines[j++] = lines[i];
  363. }
  364. lines[j] = NULL;
  365. if (nump)
  366. *nump = j;
  367. return lines;
  368. }
  369. static void add_options(struct ccanlint *test, char **options,
  370. unsigned int num_options)
  371. {
  372. unsigned int num;
  373. if (!test->options)
  374. num = 0;
  375. else
  376. /* -1, because last one is NULL. */
  377. num = talloc_array_length(test->options) - 1;
  378. test->options = talloc_realloc(NULL, test->options,
  379. char *,
  380. num + num_options + 1);
  381. memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
  382. }
  383. static void add_info_options(struct ccan_file *info, bool mark_fails)
  384. {
  385. struct doc_section *d;
  386. unsigned int i;
  387. struct ccanlint *test;
  388. list_for_each(get_ccan_file_docs(info), d, list) {
  389. if (!streq(d->type, "ccanlint"))
  390. continue;
  391. for (i = 0; i < d->num_lines; i++) {
  392. unsigned int num_words;
  393. char **words = collapse(strsplit(d, d->lines[i], " \t"),
  394. &num_words);
  395. if (num_words == 0)
  396. continue;
  397. if (strncmp(words[0], "//", 2) == 0)
  398. continue;
  399. test = find_test(words[0]);
  400. if (!test) {
  401. warnx("%s: unknown ccanlint test '%s'",
  402. info->fullname, words[0]);
  403. continue;
  404. }
  405. if (!words[1]) {
  406. warnx("%s: no argument to test '%s'",
  407. info->fullname, words[0]);
  408. continue;
  409. }
  410. /* Known failure? */
  411. if (strcasecmp(words[1], "FAIL") == 0) {
  412. if (mark_fails)
  413. btree_insert(info_exclude, words[0]);
  414. } else {
  415. if (!test->takes_options)
  416. warnx("%s: %s doesn't take options",
  417. info->fullname, words[0]);
  418. add_options(test, words+1, num_words-1);
  419. }
  420. }
  421. }
  422. }
  423. /* If options are of form "filename:<option>" they only apply to that file */
  424. char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
  425. {
  426. char **ret;
  427. unsigned int i, j = 0;
  428. /* Fast path. */
  429. if (!test->options[0])
  430. return test->options;
  431. ret = talloc_array(f, char *, talloc_array_length(test->options));
  432. for (i = 0; test->options[i]; i++) {
  433. char *optname;
  434. if (!test->options[i] || !strchr(test->options[i], ':')) {
  435. optname = test->options[i];
  436. } else if (strstarts(test->options[i], f->name)
  437. && test->options[i][strlen(f->name)] == ':') {
  438. optname = test->options[i] + strlen(f->name) + 1;
  439. } else
  440. continue;
  441. /* FAIL overrides anything else. */
  442. if (streq(optname, "FAIL")) {
  443. ret = talloc_array(f, char *, 2);
  444. ret[0] = (char *)"FAIL";
  445. ret[1] = NULL;
  446. return ret;
  447. }
  448. ret[j++] = optname;
  449. }
  450. ret[j] = NULL;
  451. /* Shrink it to size so talloc_array_length() works as expected. */
  452. return talloc_realloc(NULL, ret, char *, j + 1);
  453. }
  454. static bool depends_on(struct ccanlint *i, struct ccanlint *target)
  455. {
  456. const struct dependent *d;
  457. if (i == target)
  458. return true;
  459. list_for_each(&i->dependencies, d, node) {
  460. if (depends_on(d->dependent, target))
  461. return true;
  462. }
  463. return false;
  464. }
  465. /* O(N^2), who cares? */
  466. static void skip_unrelated_tests(struct ccanlint *target)
  467. {
  468. struct ccanlint *i;
  469. struct list_head *list;
  470. foreach_ptr(list, &compulsory_tests, &normal_tests)
  471. list_for_each(list, i, list)
  472. if (!depends_on(i, target))
  473. i->skip = "not relevant to target";
  474. }
  475. static char *demangle_string(char *string)
  476. {
  477. unsigned int i;
  478. const char mapfrom[] = "abfnrtv";
  479. const char mapto[] = "\a\b\f\n\r\t\v";
  480. if (!strchr(string, '"'))
  481. return NULL;
  482. string = strchr(string, '"') + 1;
  483. if (!strrchr(string, '"'))
  484. return NULL;
  485. *strrchr(string, '"') = '\0';
  486. for (i = 0; i < strlen(string); i++) {
  487. if (string[i] == '\\') {
  488. char repl;
  489. unsigned len = 0;
  490. const char *p = strchr(mapfrom, string[i+1]);
  491. if (p) {
  492. repl = mapto[p - mapfrom];
  493. len = 1;
  494. } else if (strlen(string+i+1) >= 3) {
  495. if (string[i+1] == 'x') {
  496. repl = (string[i+2]-'0')*16
  497. + string[i+3]-'0';
  498. len = 3;
  499. } else if (cisdigit(string[i+1])) {
  500. repl = (string[i+2]-'0')*8*8
  501. + (string[i+3]-'0')*8
  502. + (string[i+4]-'0');
  503. len = 3;
  504. }
  505. }
  506. if (len == 0) {
  507. repl = string[i+1];
  508. len = 1;
  509. }
  510. string[i] = repl;
  511. memmove(string + i + 1, string + i + len + 1,
  512. strlen(string + i + len + 1) + 1);
  513. }
  514. }
  515. return string;
  516. }
  517. static void read_config_header(void)
  518. {
  519. char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
  520. char **lines;
  521. unsigned int i;
  522. config_header = grab_file(NULL, fname, NULL);
  523. if (!config_header) {
  524. talloc_free(fname);
  525. return;
  526. }
  527. lines = strsplit(config_header, config_header, "\n");
  528. for (i = 0; i < talloc_array_length(lines) - 1; i++) {
  529. char *sym;
  530. const char **line = (const char **)&lines[i];
  531. if (!get_token(line, "#"))
  532. continue;
  533. if (!get_token(line, "define"))
  534. continue;
  535. sym = get_symbol_token(lines, line);
  536. if (streq(sym, "CCAN_COMPILER") && !compiler) {
  537. compiler = demangle_string(lines[i]);
  538. if (!compiler)
  539. errx(1, "%s:%u:could not parse CCAN_COMPILER",
  540. fname, i+1);
  541. if (verbose > 1)
  542. printf("%s: compiler set to '%s'\n",
  543. fname, compiler);
  544. } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
  545. cflags = demangle_string(lines[i]);
  546. if (!cflags)
  547. errx(1, "%s:%u:could not parse CCAN_CFLAGS",
  548. fname, i+1);
  549. if (verbose > 1)
  550. printf("%s: compiler flags set to '%s'\n",
  551. fname, cflags);
  552. }
  553. }
  554. if (!compiler)
  555. compiler = CCAN_COMPILER;
  556. if (!cflags)
  557. compiler = CCAN_CFLAGS;
  558. }
  559. static char *opt_set_const_charp(const char *arg, const char **p)
  560. {
  561. return opt_set_charp(arg, cast_const2(char **, p));
  562. }
  563. int main(int argc, char *argv[])
  564. {
  565. bool summary = false, pass = true;
  566. unsigned int i;
  567. struct manifest *m;
  568. struct ccanlint *t;
  569. const char *prefix = "";
  570. char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL,
  571. *testlink;
  572. cmdline_exclude = btree_new(btree_strcmp);
  573. info_exclude = btree_new(btree_strcmp);
  574. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  575. "do not compile anything");
  576. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  577. "list tests ccanlint performs (and exit)");
  578. opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
  579. "print dependency graph of tests in Graphviz .dot format");
  580. opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
  581. "keep results of <testname>"
  582. " (can be used multiple times, or 'all')");
  583. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  584. "simply give one line summary");
  585. opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
  586. "verbose mode (up to -vvvv)");
  587. opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
  588. "exclude <testname> (can be used multiple times)");
  589. opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
  590. NULL, &timeout,
  591. "ignore (terminate) tests that are slower than this");
  592. opt_register_arg("--target <testname>", opt_set_charp,
  593. NULL, &target,
  594. "only run one test (and its prerequisites)");
  595. opt_register_arg("--compiler <compiler>", opt_set_const_charp,
  596. NULL, &compiler, "set the compiler");
  597. opt_register_arg("--cflags <flags>", opt_set_const_charp,
  598. NULL, &cflags, "set the compiler flags");
  599. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  600. "\nA program for checking and guiding development"
  601. " of CCAN modules.",
  602. "This usage message");
  603. /* We move into temporary directory, so gcov dumps its files there. */
  604. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  605. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  606. opt_parse(&argc, argv, opt_log_stderr_exit);
  607. if (verbose >= 3) {
  608. compile_verbose = true;
  609. print_test_depends();
  610. }
  611. if (verbose >= 4)
  612. tools_verbose = true;
  613. /* This links back to the module's test dir. */
  614. testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
  615. /* Defaults to pwd. */
  616. if (argc == 1) {
  617. i = 1;
  618. goto got_dir;
  619. }
  620. for (i = 1; i < argc; i++) {
  621. unsigned int score, total_score;
  622. dir = argv[i];
  623. if (dir[0] != '/')
  624. dir = talloc_asprintf_append(NULL, "%s/%s",
  625. base_dir, dir);
  626. while (strends(dir, "/"))
  627. dir[strlen(dir)-1] = '\0';
  628. got_dir:
  629. if (dir != base_dir)
  630. prefix = talloc_append_string(talloc_basename(NULL,dir),
  631. ": ");
  632. init_tests();
  633. if (target) {
  634. struct ccanlint *test;
  635. test = find_test(target);
  636. if (!test)
  637. errx(1, "Unknown test to run '%s'", target);
  638. skip_unrelated_tests(test);
  639. }
  640. m = get_manifest(talloc_autofree_context(), dir);
  641. /* FIXME: This has to come after we've got manifest. */
  642. if (i == 1)
  643. read_config_header();
  644. /* Create a symlink from temp dir back to src dir's
  645. * test directory. */
  646. unlink(testlink);
  647. if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
  648. err(1, "Creating test symlink in %s", temp_dir(NULL));
  649. /* If you don't pass the compulsory tests, score is 0. */
  650. score = total_score = 0;
  651. while ((t = get_next_test(&compulsory_tests)) != NULL) {
  652. if (!run_test(t, summary, &score, &total_score, m,
  653. prefix)) {
  654. warnx("%s%s failed", prefix, t->name);
  655. printf("%sTotal score: 0/%u\n",
  656. prefix, total_score);
  657. pass = false;
  658. goto next;
  659. }
  660. }
  661. /* --target overrides known FAIL from _info */
  662. if (m->info_file)
  663. add_info_options(m->info_file, !target);
  664. while ((t = get_next_test(&normal_tests)) != NULL)
  665. pass &= run_test(t, summary, &score, &total_score, m,
  666. prefix);
  667. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  668. next: ;
  669. }
  670. return pass ? 0 : 1;
  671. }