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