ccanlint: compile and run tests.
[ccan] / tools / ccanlint / tests / compile_tests.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 const char *can_build(struct manifest *m)
18 {
19         if (safe_mode)
20                 return "Safe mode enabled";
21         return NULL;
22 }
23
24 static char *obj_list(const struct manifest *m, bool link_with_module)
25 {
26         char *list = talloc_strdup(m, "../tap.o");
27         struct ccan_file *i;
28
29         /* Objects from any other C files. */
30         list_for_each(&m->other_test_c_files, i, list)
31                 list = talloc_asprintf_append(list, " %.*s.o",
32                                               strlen(i->name) - 2, i->name);
33
34         if (link_with_module)
35                 list = talloc_asprintf_append(list, " ../%s.o", m->basename);
36
37         return list;
38 }
39
40 static char *lib_list(const struct manifest *m)
41 {
42         unsigned int i, num;
43         char **libs = get_libs(m, ".", ".", &num);
44         char *ret = talloc_strdup(m, "");
45
46         for (i = 0; i < num; i++)
47                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
48         return ret;
49 }
50
51 static int cleanup_testfile(const char *testfile)
52 {
53         unlink(testfile);
54         return 0;
55 }
56
57 static char *compile(const void *ctx,
58                      struct manifest *m, struct ccan_file *file, bool fail,
59                      bool link_with_module)
60 {
61         file->compiled = talloc_strdup(ctx, tempnam("/tmp", "ccanlint"));
62         talloc_set_destructor(file->compiled, cleanup_testfile);
63
64         return run_command(m, "cc " CFLAGS " %s -o %s %s %s %s",
65                            fail ? "-DFAIL" : "",
66                            file->compiled, file->name,
67                            obj_list(m, link_with_module), lib_list(m));
68 }
69
70 struct compile_tests_result {
71         struct list_node list;
72         const char *filename;
73         const char *description;
74         const char *output;
75 };
76
77 static void *do_compile_tests(struct manifest *m)
78 {
79         struct list_head *list = talloc(m, struct list_head);
80         char *cmdout;
81         struct ccan_file *i;
82         struct compile_tests_result *res;
83
84         list_head_init(list);
85
86         compile_tests.total_score = 0;
87         list_for_each(&m->compile_ok_tests, i, list) {
88                 compile_tests.total_score++;
89                 cmdout = compile(list, m, i, false, false);
90                 if (cmdout) {
91                         res = talloc(list, struct compile_tests_result);
92                         res->filename = i->name;
93                         res->description = "failed to compile";
94                         res->output = talloc_steal(res, cmdout);
95                         list_add_tail(list, &res->list);
96                 }
97         }
98
99         list_for_each(&m->run_tests, i, list) {
100                 compile_tests.total_score++;
101                 cmdout = compile(m, m, i, false, false);
102                 if (cmdout) {
103                         res = talloc(list, struct compile_tests_result);
104                         res->filename = i->name;
105                         res->description = "failed to compile";
106                         res->output = talloc_steal(res, cmdout);
107                         list_add_tail(list, &res->list);
108                 }
109         }
110
111         list_for_each(&m->api_tests, i, list) {
112                 compile_tests.total_score++;
113                 cmdout = compile(m, m, i, false, true);
114                 if (cmdout) {
115                         res = talloc(list, struct compile_tests_result);
116                         res->filename = i->name;
117                         res->description = "failed to compile";
118                         res->output = talloc_steal(res, cmdout);
119                         list_add_tail(list, &res->list);
120                 }
121         }
122
123         list_for_each(&m->compile_fail_tests, i, list) {
124                 compile_tests.total_score++;
125                 cmdout = compile(list, m, i, true, false);
126                 if (cmdout) {
127                         res = talloc(list, struct compile_tests_result);
128                         res->filename = i->name;
129                         res->description = "failed to compile without -DFAIL";
130                         res->output = talloc_steal(res, cmdout);
131                         list_add_tail(list, &res->list);
132                 } else {
133                         cmdout = compile(list, m, i, false, false);
134                         if (!cmdout) {
135                                 res = talloc(list, struct compile_tests_result);
136                                 res->filename = i->name;
137                                 res->description = "compiled successfully"
138                                         " with -DFAIL";
139                                 res->output = "";
140                                 list_add_tail(list, &res->list);
141                         }
142                 }
143         }
144
145         if (list_empty(list)) {
146                 talloc_free(list);
147                 list = NULL;
148         }
149
150         return list;
151 }
152
153 static unsigned int score_compile_tests(struct manifest *m,
154                                         void *check_result)
155 {
156         struct list_head *list = check_result;
157         struct compile_tests_result *i;
158         unsigned int score = compile_tests.total_score;
159
160         list_for_each(list, i, list)
161                 score--;
162         return score;
163 }
164
165 static const char *describe_compile_tests(struct manifest *m,
166                                           void *check_result)
167 {
168         struct list_head *list = check_result;
169         struct compile_tests_result *i;
170         char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
171
172         list_for_each(list, i, list)
173                 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
174                                                  i->filename, i->description,
175                                                  i->output);
176         return descrip;
177 }
178
179 struct ccanlint compile_tests = {
180         .name = "Compile tests succeed",
181         .total_score = 1,
182         .score = score_compile_tests,
183         .check = do_compile_tests,
184         .describe = describe_compile_tests,
185         .can_run = can_build,
186 };
187
188 REGISTER_TEST(compile_tests, &compile_test_helpers, NULL);