ccanlint: fix valgrind test.
[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 --error-exitcode=0 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 --error-exitcode=100 %s",
58                                      i->compiled);
59                 if (cmdout) {
60                         res = talloc(list, struct run_tests_result);
61                         res->file = i;
62                         res->output = talloc_steal(res, cmdout);
63                         list_add_tail(list, &res->list);
64                 }
65         }
66
67         list_for_each(&m->api_tests, i, list) {
68                 run_tests_vg.total_score++;
69                 cmdout = run_command(m, timeleft,
70                                      "valgrind -q --error-exitcode=100 %s",
71                                      i->compiled);
72                 if (cmdout) {
73                         res = talloc(list, struct run_tests_result);
74                         res->file = i;
75                         res->output = talloc_steal(res, cmdout);
76                         list_add_tail(list, &res->list);
77                 }
78         }
79
80         if (list_empty(list)) {
81                 talloc_free(list);
82                 list = NULL;
83         }
84
85         if (chdir(olddir) != 0)
86                 err(1, "Could not chdir to %s", olddir);
87
88         return list;
89 }
90
91 static unsigned int score_run_tests_vg(struct manifest *m, void *check_result)
92 {
93         struct list_head *list = check_result;
94         struct run_tests_result *i;
95         unsigned int score = run_tests_vg.total_score;
96
97         list_for_each(list, i, list)
98                 score--;
99         return score;
100 }
101
102 static const char *describe_run_tests_vg(struct manifest *m,
103                                          void *check_result)
104 {
105         struct list_head *list = check_result;
106         char *descrip = talloc_strdup(check_result, "Running tests under valgrind failed:\n");
107         struct run_tests_result *i;
108
109         list_for_each(list, i, list)
110                 descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
111                                                  i->file->name, i->output);
112         return descrip;
113 }
114
115 /* Gcc's warn_unused_result is fascist bullshit. */
116 #define doesnt_matter()
117
118 static void run_under_debugger_vg(struct manifest *m, void *check_result)
119 {
120         struct list_head *list = check_result;
121         struct run_tests_result *first;
122         char *command;
123
124         if (!ask("Should I run the first failing test under the debugger?"))
125                 return;
126
127         first = list_top(list, struct run_tests_result, list);
128         command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
129                                   first->file->compiled);
130         if (system(command))
131                 doesnt_matter();
132 }
133
134 struct ccanlint run_tests_vg = {
135         .key = "valgrind-tests",
136         .name = "Module's run and api tests succeed under valgrind",
137         .score = score_run_tests_vg,
138         .check = do_run_tests_vg,
139         .describe = describe_run_tests_vg,
140         .can_run = can_run_vg,
141         .handle = run_under_debugger_vg
142 };
143
144 REGISTER_TEST(run_tests_vg, &run_tests, NULL);