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