ccanlint: handle when _info doesn't compile.
[ccan] / tools / ccanlint / tests / depends_exist.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/tal/path/path.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <err.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 static bool have_dep(struct manifest *m, const char *dep)
18 {
19         struct manifest *i;
20
21         list_for_each(&m->deps, i, list)
22                 if (streq(i->modname, dep + strlen("ccan/")))
23                         return true;
24
25         return false;
26 }
27
28 static bool add_dep(struct manifest *m,
29                     struct list_head *deplist,
30                     const char *dep, struct score *score)
31 {
32         struct stat st;
33         struct manifest *subm;
34         char *dir = path_join(m, ccan_dir, dep);
35
36         /* FIXME: get_manifest has a tendency to exit. */
37         if (stat(dir, &st) != 0) {
38                 score->error = tal_fmt(m, "Could not stat dependency %s: %s",
39                                        dir, strerror(errno));
40                 return false;
41         }
42         subm = get_manifest(m, dir);
43         list_add_tail(deplist, &subm->list);
44         return true;
45 }
46
47 /* FIXME: check this is still true once we reduce features. */
48 static void check_depends_exist(struct manifest *m,
49                                 unsigned int *timeleft, struct score *score)
50 {
51         unsigned int i;
52         char **deps;
53
54         if (safe_mode)
55                 deps = get_safe_ccan_deps(m, m->dir, "depends", true);
56         else
57                 deps = get_deps(m, m->dir, "depends", true,
58                                 get_or_compile_info);
59
60         if (!deps) {
61                 score->error = tal_fmt(m, "Could not extract dependencies");
62                 return;
63         }
64
65         for (i = 0; deps[i]; i++) {
66                 if (!strstarts(deps[i], "ccan/"))
67                         continue;
68
69                 if (!add_dep(m, &m->deps, deps[i], score))
70                         return;
71         }
72
73         score->pass = true;
74         score->score = score->total;
75 }
76
77 static void check_test_depends_exist(struct manifest *m,
78                                      unsigned int *timeleft,
79                                      struct score *score)
80 {
81         unsigned int i;
82         char **deps;
83         bool needs_tap;
84
85         /* We may need libtap for testing, unless we're "tap" */
86         if (streq(m->modname, "tap")) {
87                 needs_tap = false;
88         } else if (list_empty(&m->run_tests) && list_empty(&m->api_tests)) {
89                 needs_tap = false;
90         } else {
91                 needs_tap = true;
92         }
93
94         if (safe_mode)
95                 deps = get_safe_ccan_deps(m, m->dir, "testdepends", true);
96         else
97                 deps = get_deps(m, m->dir, "testdepends", true,
98                                 get_or_compile_info);
99
100         for (i = 0; deps[i]; i++) {
101                 if (!strstarts(deps[i], "ccan/"))
102                         continue;
103
104                 /* Don't add dependency twice: we can only be on one list! */
105                 if (!have_dep(m, deps[i])
106                     && !add_dep(m, &m->test_deps, deps[i], score))
107                         return;
108
109                 if (streq(deps[i], "ccan/tap")) {
110                         needs_tap = false;
111                 }
112         }
113
114         if (needs_tap && !have_dep(m, "ccan/tap")
115             && !add_dep(m, &m->test_deps, "ccan/tap", score)) {
116                 return;
117         }
118
119         score->pass = true;
120         score->score = score->total;
121 }
122
123 struct ccanlint depends_exist = {
124         .key = "depends_exist",
125         .name = "Module's CCAN dependencies can be found",
126         .compulsory = true,
127         .check = check_depends_exist,
128         .needs = "info_exists"
129 };
130
131 REGISTER_TEST(depends_exist);
132
133 struct ccanlint test_depends_exist = {
134         .key = "test_depends_exist",
135         .name = "Module's CCAN test dependencies can be found",
136         .compulsory = false,
137         .check = check_test_depends_exist,
138         .needs = "depends_exist"
139 };
140
141 REGISTER_TEST(test_depends_exist);