More test fixes.
[ccan] / tools / ccanlint / tests / run_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_run(struct manifest *m)
18 {
19         if (safe_mode)
20                 return "Safe mode enabled";
21         return NULL;
22 }
23
24 static void *do_run_tests(struct manifest *m)
25 {
26         struct list_head *list = talloc(m, struct list_head);
27         char *failures = talloc_strdup(m, "");
28         struct ccan_file *i;
29
30         list_head_init(list);
31
32         list_for_each(&m->run_tests, i, list) {
33                 char *testout;
34                 run_tests.total_score++;
35                 /* FIXME: timeout here */
36                 testout = run_command(m, i->compiled);
37                 if (!testout)
38                         continue;
39                 failures = talloc_asprintf_append(failures,
40                                                   "Running %s failed:\n",
41                                                   i->name);
42                 failures = talloc_append_string(failures, testout);
43         }
44
45         list_for_each(&m->api_tests, i, list) {
46                 char *testout;
47                 run_tests.total_score++;
48                 /* FIXME: timeout here */
49                 testout = run_command(m, i->compiled);
50                 if (!testout)
51                         continue;
52                 failures = talloc_asprintf_append(failures,
53                                                   "Running %s failed:\n",
54                                                   i->name);
55                 failures = talloc_append_string(failures, testout);
56         }
57
58         if (streq(failures, "")) {
59                 talloc_free(failures);
60                 failures = NULL;
61         }
62
63         return failures;
64 }
65
66 static unsigned int score_run_tests(struct manifest *m, void *check_result)
67 {
68         /* FIXME: be cleverer here */
69         return 0;
70 }
71
72 static const char *describe_run_tests(struct manifest *m,
73                                           void *check_result)
74 {
75         char *descrip = talloc_strdup(check_result, "Running tests failed:\n");
76
77         return talloc_append_string(descrip, check_result);
78 }
79
80 /* FIXME: Handle by offering to run under debugger... */
81
82 struct ccanlint run_tests = {
83         .name = "run and api tests run successfully",
84         .score = score_run_tests,
85         .check = do_run_tests,
86         .describe = describe_run_tests,
87         .can_run = can_run,
88 };
89
90 REGISTER_TEST(run_tests, &compile_tests, NULL);