]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/examples_run.c
ccan_tokenizer: update to be compatible with darray.
[ccan] / tools / ccanlint / tests / examples_run.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/foreach/foreach.h>
5 #include <ccan/str/str.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <stdint.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <ctype.h>
13 #include <assert.h>
14
15 static const char *can_run(struct manifest *m)
16 {
17         struct list_head *list;
18
19         if (safe_mode)
20                 return "Safe mode enabled";
21         foreach_ptr(list, &m->examples, &m->mangled_examples)
22                 if (!list_empty(list))
23                         return NULL;
24         return "No examples";
25 }
26
27 /* Very dumb scanner, allocates %s-strings. */
28 static bool scan_forv(const void *ctx,
29                       const char *input, const char *fmt, va_list *args)
30 {
31         va_list ap;
32         bool ret;
33
34         if (input[0] == '\0' || fmt[0] == '\0')
35                 return input[0] == fmt[0];
36
37         va_copy(ap, *args);
38
39         if (isspace(fmt[0])) {
40                 /* One format space can swallow many input spaces */
41                 ret = false;
42                 while (isspace(input[0])) {
43                         if (scan_forv(ctx, ++input, fmt+1, &ap)) {
44                                 ret = true;
45                                 break;
46                         }
47                 }
48         } else if (fmt[0] != '%') {
49                 if (toupper(input[0]) != toupper(fmt[0]))
50                         ret = false;
51                 else
52                         ret = scan_forv(ctx, input+1, fmt+1, &ap);
53         } else {
54                 char **p = va_arg(ap, char **);
55                 unsigned int len;
56
57                 ret = false;
58                 assert(fmt[1] == 's');
59                 for (len = 1; input[len-1]; len++) {
60                         ret = scan_forv(ctx, input + len, fmt+2, &ap);
61                         if (ret) {
62                                 *p = talloc_strndup(ctx, input, len);
63                                 ret = true;
64                                 break;
65                         }
66                 }
67         }
68         va_end(ap);
69         return ret;
70 }
71
72 static bool scan_for(const void *ctx, const char *input, const char *fmt, ...)
73 {
74         bool ret;
75         va_list ap;
76
77         va_start(ap, fmt);
78         ret = scan_forv(ctx, input, fmt, &ap);
79         va_end(ap);
80         return ret;
81 }
82
83 static char *find_expect(struct ccan_file *file,
84                          char **lines, char **input, bool *exact,
85                          unsigned *line)
86 {
87         char *expect;
88         const char *fmt;
89
90         for (; lines[*line]; (*line)++) {
91                 char *p = lines[*line] + strspn(lines[*line], " \t");
92                 if (!strstarts(p, "//"))
93                         continue;
94                 p += strspn(p, "/ ");
95                 foreach_ptr(fmt,
96                             "given '%s', outputs '%s'",
97                             "given '%s' outputs '%s'",
98                             "given \"%s\", outputs \"%s\"",
99                             "given \"%s\" outputs \"%s\"") {
100                         if (scan_for(file, p, fmt, input, &expect)) {
101                                 *exact = true;
102                                 return expect;
103                         }
104                 }
105
106                 foreach_ptr(fmt,
107                             "given '%s', output contains '%s'",
108                             "given '%s' output contains '%s'",
109                             "given \"%s\", output contains \"%s\"",
110                             "given \"%s\" output contains \"%s\"") {
111                         if (scan_for(file, p, fmt, input, &expect)) {
112                                 *exact = false;
113                                 return expect;
114                         }
115                 }
116
117                 foreach_ptr(fmt, "outputs '%s'", "outputs \"%s\"") {
118                         if (scan_for(file, p, fmt, &expect)) {
119                                 *input = "";
120                                 *exact = true;
121                                 return expect;
122                         }
123                 }
124
125                 foreach_ptr(fmt,
126                             "given '%s', output contains '%s'",
127                             "given '%s' output contains '%s'",
128                             "given \"%s\", output contains \"%s\"",
129                             "given \"%s\" output contains \"%s\"") {
130                         if (scan_for(file, p, fmt, input, &expect)) {
131                                 *exact = false;
132                                 return expect;
133                         }
134                 }
135
136                 /* Unquoted versions... we can get this wrong! */
137                 foreach_ptr(fmt,
138                             "given %s, outputs '%s'",
139                             "given '%s', outputs %s",
140                             "given %s, outputs \"%s\"",
141                             "given \"%s\", outputs %s",
142                             "given %s, outputs %s",
143                             "given %s outputs '%s'",
144                             "given '%s' outputs %s",
145                             "given %s outputs \"%s\"",
146                             "given \"%s\" outputs %s",
147                             "given %s outputs %s") {
148                         if (scan_for(file, p, fmt, input, &expect)) {
149                                 *exact = true;
150                                 return expect;
151                         }
152                 }
153
154                 foreach_ptr(fmt,
155                             "given %s, output contains '%s'",
156                             "given '%s', output contains %s",
157                             "given %s, output contains \"%s\"",
158                             "given \"%s\", output contains %s",
159                             "given %s, output contains %s",
160                             "given %s output contains '%s'",
161                             "given '%s' output contains %s",
162                             "given %s output contains \"%s\"",
163                             "given \"%s\" output contains %s",
164                             "given %s output contains %s") {
165                         if (scan_for(file, p, fmt, input, &expect)) {
166                                 *exact = false;
167                                 return expect;
168                         }
169                 }
170
171                 foreach_ptr(fmt,
172                             "outputs '%s'",
173                             "outputs \"%s\"",
174                             "outputs %s") {
175                         if (scan_for(file, p, fmt, &expect)) {
176                                 *input = "";
177                                 *exact = true;
178                                 return expect;
179                         }
180                 }
181
182                 foreach_ptr(fmt,
183                             "output contains '%s'",
184                             "output contains \"%s\"",
185                             "output contains %s") {
186                         if (scan_for(file, p, fmt, &expect)) {
187                                 *input = "";
188                                 *exact = false;
189                                 return expect;
190                         }
191                 }
192         }               
193         return NULL;
194 }
195
196 static char *trim(char *string)
197 {
198         while (strends(string, "\n"))
199                string[strlen(string)-1] = '\0';
200         return string;
201 }
202
203 static char *unexpected(struct ccan_file *i, const char *input,
204                         const char *expect, bool exact)
205 {
206         char *output, *cmd;
207         bool ok;
208         unsigned int default_time = default_timeout_ms;
209
210         cmd = talloc_asprintf(i, "echo '%s' | %s %s",
211                               input, i->compiled, input);
212
213         output = run_with_timeout(i, cmd, &ok, &default_time);
214         if (!ok)
215                 return talloc_asprintf(i, "Exited with non-zero status\n");
216
217         if (exact) {
218                 if (streq(output, expect) || streq(trim(output), expect))
219                         return NULL;
220         } else {
221                 if (strstr(output, expect))
222                         return NULL;
223         }
224         return output;
225 }
226
227 static void run_examples(struct manifest *m, bool keep,
228                          unsigned int *timeleft, struct score *score)
229 {
230         struct ccan_file *i;
231         struct list_head *list;
232
233         score->total = 0;
234         score->pass = true;
235
236         foreach_ptr(list, &m->examples, &m->mangled_examples) {
237                 list_for_each(list, i, list) {
238                         char **lines, *expect, *input, *output;
239                         unsigned int linenum = 0;
240                         bool exact;
241
242                         lines = get_ccan_file_lines(i);
243
244                         for (expect = find_expect(i, lines, &input, &exact,
245                                                   &linenum);
246                              expect;
247                              linenum++,
248                                      expect = find_expect(i, lines, &input,
249                                                           &exact, &linenum)) {
250                                 char *err;
251                                 if (i->compiled == NULL)
252                                         continue;
253
254                                 score->total++;
255                                 output = unexpected(i, input, expect, exact);
256                                 if (!output) {
257                                         score->score++;
258                                         continue;
259                                 }
260                                 err = talloc_asprintf(score,
261                                                       "output '%s' didn't"
262                                                       " %s '%s'\n",
263                                                       output,
264                                                       exact
265                                                       ? "match" : "contain",
266                                                       expect);
267                                 score_file_error(score, i, linenum+1, err);
268                                 score->pass = false;
269                         }
270                 }
271         }
272 }
273
274 struct ccanlint examples_run = {
275         .key = "examples_run",
276         .name = "Module examples with expected output give that output",
277         .check = run_examples,
278         .can_run = can_run,
279         .needs = "examples_compile"
280 };
281
282 REGISTER_TEST(examples_run);