]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/run_tests_valgrind.c
ccanlint: with -vv, give details on attempts to compile examples.
[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
43         list_head_init(list);
44
45         list_for_each(&m->run_tests, i, list) {
46                 run_tests_vg.total_score++;
47                 cmdout = run_command(m, timeleft,
48                                      "valgrind -q --error-exitcode=100 %s",
49                                      i->compiled);
50                 if (cmdout) {
51                         res = talloc(list, struct run_tests_result);
52                         res->file = i;
53                         res->output = talloc_steal(res, cmdout);
54                         list_add_tail(list, &res->list);
55                 }
56         }
57
58         list_for_each(&m->api_tests, i, list) {
59                 run_tests_vg.total_score++;
60                 cmdout = run_command(m, timeleft,
61                                      "valgrind -q --error-exitcode=100 %s",
62                                      i->compiled);
63                 if (cmdout) {
64                         res = talloc(list, struct run_tests_result);
65                         res->file = i;
66                         res->output = talloc_steal(res, cmdout);
67                         list_add_tail(list, &res->list);
68                 }
69         }
70
71         if (list_empty(list)) {
72                 talloc_free(list);
73                 list = NULL;
74         }
75
76         return list;
77 }
78
79 static unsigned int score_run_tests_vg(struct manifest *m, void *check_result)
80 {
81         struct list_head *list = check_result;
82         struct run_tests_result *i;
83         unsigned int score = run_tests_vg.total_score;
84
85         list_for_each(list, i, list)
86                 score--;
87         return score;
88 }
89
90 static const char *describe_run_tests_vg(struct manifest *m,
91                                          void *check_result)
92 {
93         struct list_head *list = check_result;
94         char *descrip = talloc_strdup(check_result, "Running tests under valgrind failed:\n");
95         struct run_tests_result *i;
96
97         list_for_each(list, i, list)
98                 descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
99                                                  i->file->name, i->output);
100         return descrip;
101 }
102
103 /* Gcc's warn_unused_result is fascist bullshit. */
104 #define doesnt_matter()
105
106 static void run_under_debugger_vg(struct manifest *m, void *check_result)
107 {
108         struct list_head *list = check_result;
109         struct run_tests_result *first;
110         char *command;
111
112         if (!ask("Should I run the first failing test under the debugger?"))
113                 return;
114
115         first = list_top(list, struct run_tests_result, list);
116         command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
117                                   first->file->compiled);
118         if (system(command))
119                 doesnt_matter();
120 }
121
122 struct ccanlint run_tests_vg = {
123         .key = "valgrind-tests",
124         .name = "Module's run and api tests succeed under valgrind",
125         .score = score_run_tests_vg,
126         .check = do_run_tests_vg,
127         .describe = describe_run_tests_vg,
128         .can_run = can_run_vg,
129         .handle = run_under_debugger_vg
130 };
131
132 REGISTER_TEST(run_tests_vg, &run_tests, NULL);