ccanlint: keep separate array of compiled versions.
[ccan] / tools / ccanlint / tests / tests_pass.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 #include "tests_pass.h"
18
19 bool do_valgrind = false;
20
21 static const char *can_run(struct manifest *m)
22 {
23         unsigned int timeleft = default_timeout_ms;
24         char *output;
25         if (safe_mode)
26                 return "Safe mode enabled";
27
28         if (!is_excluded("tests_pass_valgrind")
29             && run_command(m, &timeleft, &output,
30                            "valgrind -q true"))
31                 do_valgrind = true;
32
33         return NULL;
34 }
35
36 static const char *concat(struct score *score, char *bits[])
37 {
38         unsigned int i;
39         char *ret = talloc_strdup(score, "");
40
41         for (i = 0; bits[i]; i++) {
42                 if (i)
43                         ret = talloc_append_string(ret, " ");
44                 ret = talloc_append_string(ret, bits[i]);
45         }
46         return ret;
47 }
48
49 static bool run_test(void *ctx,
50                      struct manifest *m,
51                      unsigned int *timeleft, char **cmdout,
52                      struct ccan_file *i)
53 {
54         if (do_valgrind) {
55                 const char *options;
56                 options = concat(ctx,
57                                  per_file_options(&tests_pass_valgrind, i));
58
59                 if (!streq(options, "FAIL")) {
60                         /* FIXME: Valgrind's output sucks.  XML is
61                          * unreadable by humans *and* doesn't support
62                          * children reporting. */
63                         i->valgrind_log = talloc_asprintf(m,
64                                           "%s.valgrind-log",
65                                           i->compiled[COMPILE_NORMAL]);
66                         talloc_set_destructor(i->valgrind_log,
67                                               unlink_file_destructor);
68
69                         return run_command(ctx, timeleft, cmdout,
70                                            "valgrind -q"
71                                            " --leak-check=full"
72                                            " --log-fd=3 %s %s"
73                                            " 3> %s",
74                                            options,
75                                            i->compiled[COMPILE_NORMAL],
76                                            i->valgrind_log);
77                 }
78         }
79
80         return run_command(m, timeleft, cmdout, "%s",
81                            i->compiled[COMPILE_NORMAL]);
82 }
83
84 static void do_run_tests(struct manifest *m,
85                          bool keep,
86                          unsigned int *timeleft,
87                          struct score *score)
88 {
89         struct list_head *list;
90         struct ccan_file *i;
91         char *cmdout;
92
93         score->total = 0;
94         foreach_ptr(list, &m->run_tests, &m->api_tests) {
95                 list_for_each(list, i, list) {
96                         score->total++;
97                         if (run_test(score, m, timeleft, &cmdout, i))
98                                 score->score++;
99                         else
100                                 score_file_error(score, i, 0, "%s", cmdout);
101                 }
102         }
103
104         if (score->score == score->total)
105                 score->pass = true;
106 }
107
108
109 /* Gcc's warn_unused_result is fascist bullshit. */
110 #define doesnt_matter()
111
112 static void run_under_debugger(struct manifest *m, struct score *score)
113 {
114         char *command;
115         struct file_error *first;
116
117         first = list_top(&score->per_file_errors, struct file_error, list);
118
119         if (!ask("Should I run the first failing test under the debugger?"))
120                 return;
121
122         command = talloc_asprintf(m, "gdb -ex 'break tap.c:139' -ex 'run' %s",
123                                   first->file->compiled[COMPILE_NORMAL]);
124         if (system(command))
125                 doesnt_matter();
126 }
127
128 struct ccanlint tests_pass = {
129         .key = "tests_pass",
130         .name = "Module's run and api tests pass",
131         .check = do_run_tests,
132         .handle = run_under_debugger,
133         .can_run = can_run,
134         .needs = "tests_compile"
135 };
136
137 REGISTER_TEST(tests_pass);