ccanlint: add ccanlint section to _info
[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 <ccan/foreach/foreach.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <err.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 /* Note: we already test safe_mode in run_tests.c */
19 static const char *can_run_vg(struct manifest *m)
20 {
21         unsigned int timeleft = default_timeout_ms;
22         char *output;
23
24         if (!run_command(m, &timeleft, &output,
25                          "valgrind -q --error-exitcode=0 true"))
26                 return talloc_asprintf(m, "No valgrind support: %s", output);
27         return NULL;
28 }
29
30 /* FIXME: Run examples, too! */
31 static void do_run_tests_vg(struct manifest *m,
32                              bool keep,
33                             unsigned int *timeleft,
34                             struct score *score)
35 {
36         struct ccan_file *i;
37         struct list_head *list;
38         char *cmdout;
39
40         score->total = 0;
41         foreach_ptr(list, &m->run_tests, &m->api_tests) {
42                 list_for_each(list, i, list) {
43                         score->total++;
44                         if (run_command(score, timeleft, &cmdout,
45                                         "valgrind -q --error-exitcode=100%s %s",
46                                         run_tests_vg.options ?
47                                         run_tests_vg.options : "",
48                                         i->compiled)) {
49                                 score->score++;
50                         } else {
51                                 score_file_error(score, i, 0, cmdout);
52                         }
53                 }
54         }
55
56         if (score->score == score->total)
57                 score->pass = true;
58 }
59
60 /* Gcc's warn_unused_result is fascist bullshit. */
61 #define doesnt_matter()
62
63 static void run_under_debugger_vg(struct manifest *m, struct score *score)
64 {
65         struct file_error *first;
66         char *command;
67
68         if (!ask("Should I run the first failing test under the debugger?"))
69                 return;
70
71         first = list_top(&score->per_file_errors, struct file_error, list);
72         command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
73                                   first->file->compiled);
74         if (system(command))
75                 doesnt_matter();
76 }
77
78 struct ccanlint run_tests_vg = {
79         .key = "valgrind-tests",
80         .name = "Module's run and api tests succeed under valgrind",
81         .can_run = can_run_vg,
82         .check = do_run_tests_vg,
83         .handle = run_under_debugger_vg,
84         .takes_options = true
85 };
86
87 REGISTER_TEST(run_tests_vg, &run_tests, NULL);