ccanlint: rework so checks have more structure.
[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 = run_command(m, &timeleft,
23                                    "valgrind -q --error-exitcode=0 true");
24
25         if (output)
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                         cmdout = run_command(m, timeleft,
45                                      "valgrind -q --error-exitcode=100 %s",
46                                      i->compiled);
47                         if (cmdout) {
48                                 score->error = "Running under valgrind";
49                                 score_file_error(score, i, 0, cmdout);
50                         } else
51                                 score->score++;
52                 }
53         }
54
55         if (score->score == score->total)
56                 score->pass = true;
57 }
58
59 /* Gcc's warn_unused_result is fascist bullshit. */
60 #define doesnt_matter()
61
62 static void run_under_debugger_vg(struct manifest *m, struct score *score)
63 {
64         struct file_error *first;
65         char *command;
66
67         if (!ask("Should I run the first failing test under the debugger?"))
68                 return;
69
70         first = list_top(&score->per_file_errors, struct file_error, list);
71         command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
72                                   first->file->compiled);
73         if (system(command))
74                 doesnt_matter();
75 }
76
77 struct ccanlint run_tests_vg = {
78         .key = "valgrind-tests",
79         .name = "Module's run and api tests succeed under valgrind",
80         .can_run = can_run_vg,
81         .check = do_run_tests_vg,
82         .handle = run_under_debugger_vg
83 };
84
85 REGISTER_TEST(run_tests_vg, &run_tests, NULL);