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