b3c1188de592f4eece24b81cfa45561dc0a83be3
[ccan] / tools / ccanlint / compulsory_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 struct run_tests_result {
25         struct list_node list;
26         struct ccan_file *file;
27         const char *output;
28 };
29
30 static void *do_run_tests(struct manifest *m)
31 {
32         struct list_head *list = talloc(m, struct list_head);
33         struct run_tests_result *res;
34         struct ccan_file *i;
35         char *cmdout;
36
37         list_head_init(list);
38
39         list_for_each(&m->run_tests, i, list) {
40                 run_tests.total_score++;
41                 /* FIXME: timeout here */
42                 cmdout = run_command(m, i->compiled);
43                 if (cmdout) {
44                         res = talloc(list, struct run_tests_result);
45                         res->file = i;
46                         res->output = talloc_steal(res, cmdout);
47                         list_add_tail(list, &res->list);
48                 }
49         }
50
51         list_for_each(&m->api_tests, i, list) {
52                 run_tests.total_score++;
53                 /* FIXME: timeout here */
54                 cmdout = run_command(m, i->compiled);
55                 if (cmdout) {
56                         res = talloc(list, struct run_tests_result);
57                         res->file = i;
58                         res->output = talloc_steal(res, cmdout);
59                         list_add_tail(list, &res->list);
60                 }
61         }
62
63         if (list_empty(list)) {
64                 talloc_free(list);
65                 list = NULL;
66         }
67
68         return list;
69 }
70
71 static unsigned int score_run_tests(struct manifest *m, void *check_result)
72 {
73         struct list_head *list = check_result;
74         struct run_tests_result *i;
75         unsigned int score = run_tests.total_score;
76
77         list_for_each(list, i, list)
78                 score--;
79         return score;
80 }
81
82 static const char *describe_run_tests(struct manifest *m,
83                                           void *check_result)
84 {
85         struct list_head *list = check_result;
86         char *descrip = talloc_strdup(check_result, "Running tests failed:\n");
87         struct run_tests_result *i;
88
89         list_for_each(list, i, list)
90                 descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
91                                                  i->file->name, i->output);
92         return descrip;
93 }
94
95 static void run_under_debugger(struct manifest *m, void *check_result)
96 {
97         char *command;
98         struct list_head *list = check_result;
99         struct run_tests_result *first;
100
101         if (!ask("Should I run the first failing test under the debugger?"))
102                 return;
103
104         first = list_top(list, struct run_tests_result, list);
105         command = talloc_asprintf(m, "gdb -ex 'break tap.c:136' -ex 'run' %s",
106                                   first->file->compiled);
107         system(command);
108 }
109
110 struct ccanlint run_tests = {
111         .name = "run and api tests run successfully",
112         .score = score_run_tests,
113         .check = do_run_tests,
114         .describe = describe_run_tests,
115         .can_run = can_run,
116         .handle = run_under_debugger
117 };
118
119 REGISTER_TEST(run_tests, &compile_tests, NULL);