ccanlint: Add -k option to keep results.
[ccan] / tools / ccanlint / tests / run_tests_valgrind.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 /* Note: we already test safe_mode in run_tests.c */
18 static const char *can_run_vg(struct manifest *m)
19 {
20         unsigned int timeleft = default_timeout_ms;
21         char *output = run_command(m, &timeleft, "valgrind -q true");
22
23         if (output)
24                 return talloc_asprintf(m, "No valgrind support: %s", output);
25         return NULL;
26 }
27
28 struct run_tests_result {
29         struct list_node list;
30         struct ccan_file *file;
31         const char *output;
32 };
33
34 static void *do_run_tests_vg(struct manifest *m,
35                              bool keep,
36                              unsigned int *timeleft)
37 {
38         struct list_head *list = talloc(m, struct list_head);
39         struct run_tests_result *res;
40         struct ccan_file *i;
41         char *cmdout;
42         char *olddir;
43
44         /* We run tests in the module directory, so any paths
45          * referenced can all be module-local. */
46         olddir = talloc_getcwd(m);
47         if (!olddir)
48                 err(1, "Could not save cwd");
49         if (chdir(m->dir) != 0)
50                 err(1, "Could not chdir to %s", m->dir);
51
52         list_head_init(list);
53
54         list_for_each(&m->run_tests, i, list) {
55                 run_tests_vg.total_score++;
56                 cmdout = run_command(m, timeleft,
57                                      "valgrind -q %s", i->compiled);
58                 if (cmdout) {
59                         res = talloc(list, struct run_tests_result);
60                         res->file = i;
61                         res->output = talloc_steal(res, cmdout);
62                         list_add_tail(list, &res->list);
63                 }
64         }
65
66         list_for_each(&m->api_tests, i, list) {
67                 run_tests_vg.total_score++;
68                 cmdout = run_command(m, timeleft,
69                                      "valgrind -q %s", i->compiled);
70                 if (cmdout) {
71                         res = talloc(list, struct run_tests_result);
72                         res->file = i;
73                         res->output = talloc_steal(res, cmdout);
74                         list_add_tail(list, &res->list);
75                 }
76         }
77
78         if (list_empty(list)) {
79                 talloc_free(list);
80                 list = NULL;
81         }
82
83         if (chdir(olddir) != 0)
84                 err(1, "Could not chdir to %s", olddir);
85
86         return list;
87 }
88
89 static unsigned int score_run_tests_vg(struct manifest *m, void *check_result)
90 {
91         struct list_head *list = check_result;
92         struct run_tests_result *i;
93         unsigned int score = run_tests_vg.total_score;
94
95         list_for_each(list, i, list)
96                 score--;
97         return score;
98 }
99
100 static const char *describe_run_tests_vg(struct manifest *m,
101                                          void *check_result)
102 {
103         struct list_head *list = check_result;
104         char *descrip = talloc_strdup(check_result, "Running tests under valgrind failed:\n");
105         struct run_tests_result *i;
106
107         list_for_each(list, i, list)
108                 descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
109                                                  i->file->name, i->output);
110         return descrip;
111 }
112
113 /* Gcc's warn_unused_result is fascist bullshit. */
114 #define doesnt_matter()
115
116 static void run_under_debugger_vg(struct manifest *m, void *check_result)
117 {
118         struct list_head *list = check_result;
119         struct run_tests_result *first;
120         char *command;
121
122         if (!ask("Should I run the first failing test under the debugger?"))
123                 return;
124
125         first = list_top(list, struct run_tests_result, list);
126         command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
127                                   first->file->compiled);
128         if (system(command))
129                 doesnt_matter();
130 }
131
132 struct ccanlint run_tests_vg = {
133         .key = "valgrind-tests",
134         .name = "Module's run and api tests succeed under valgrind",
135         .score = score_run_tests_vg,
136         .check = do_run_tests_vg,
137         .describe = describe_run_tests_vg,
138         .can_run = can_run_vg,
139         .handle = run_under_debugger_vg
140 };
141
142 REGISTER_TEST(run_tests_vg, &run_tests, NULL);