ccanlint: add testdepends support.
[ccan] / tools / ccanlint / tests / depends_exist.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.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->basename, 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 = talloc_asprintf(m, "%s/%s", ccan_dir, dep);
35
36         /* FIXME: get_manifest has a tendency to exit. */
37         if (stat(dir, &st) != 0) {
38                 score->error
39                         = talloc_asprintf(m,
40                                           "Could not stat dependency %s: %s",
41                                           dir, strerror(errno));
42                 return false;
43         }
44         subm = get_manifest(m, dir);
45         list_add_tail(deplist, &subm->list);
46         return true;
47 }
48
49 /* FIXME: check this is still true once we reduce features. */
50 static void check_depends_exist(struct manifest *m,
51                                 unsigned int *timeleft, struct score *score)
52 {
53         unsigned int i;
54         char **deps;
55
56         if (safe_mode)
57                 deps = get_safe_ccan_deps(m, m->dir, "depends", true);
58         else
59                 deps = get_deps(m, m->dir, "depends", true,
60                                 get_or_compile_info);
61
62         for (i = 0; deps[i]; i++) {
63                 if (!strstarts(deps[i], "ccan/"))
64                         continue;
65
66                 if (!add_dep(m, &m->deps, deps[i], score))
67                         return;
68         }
69
70         score->pass = true;
71         score->score = score->total;
72 }
73
74 static void check_test_depends_exist(struct manifest *m,
75                                      unsigned int *timeleft,
76                                      struct score *score)
77 {
78         unsigned int i;
79         char **deps;
80         bool needs_tap;
81
82         /* We may need libtap for testing, unless we're "tap" */
83         if (streq(m->basename, "tap")) {
84                 needs_tap = false;
85         } else if (list_empty(&m->run_tests) && list_empty(&m->api_tests)) {
86                 needs_tap = false;
87         } else {
88                 needs_tap = true;
89         }
90
91         if (safe_mode)
92                 deps = get_safe_ccan_deps(m, m->dir, "testdepends", true);
93         else
94                 deps = get_deps(m, m->dir, "testdepends", true,
95                                 get_or_compile_info);
96
97         for (i = 0; deps[i]; i++) {
98                 if (!strstarts(deps[i], "ccan/"))
99                         continue;
100
101                 /* Don't add dependency twice: we can only be on one list! */
102                 if (!have_dep(m, deps[i])
103                     && !add_dep(m, &m->test_deps, deps[i], score))
104                         return;
105
106                 if (streq(deps[i], "ccan/tap")) {
107                         needs_tap = false;
108                 }
109         }
110
111         if (needs_tap && !have_dep(m, "ccan/tap")
112             && !add_dep(m, &m->test_deps, "ccan/tap", score)) {
113                 return;
114         }
115
116         score->pass = true;
117         score->score = score->total;
118 }
119
120 struct ccanlint depends_exist = {
121         .key = "depends_exist",
122         .name = "Module's CCAN dependencies can be found",
123         .compulsory = true,
124         .check = check_depends_exist,
125         .needs = "info_exists"
126 };
127
128 REGISTER_TEST(depends_exist);
129
130 struct ccanlint test_depends_exist = {
131         .key = "test_depends_exist",
132         .name = "Module's CCAN test dependencies can be found",
133         .compulsory = false,
134         .check = check_test_depends_exist,
135         .needs = "depends_exist"
136 };
137
138 REGISTER_TEST(test_depends_exist);