]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_pass.c
ccanlint: show each test as we execute it with -vv
[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 (verbose >= 2)
98                                 printf("   %s\n", i->name);
99                         if (run_test(score, m, timeleft, &cmdout, i))
100                                 score->score++;
101                         else
102                                 score_file_error(score, i, 0, "%s", cmdout);
103                 }
104         }
105
106         if (score->score == score->total)
107                 score->pass = true;
108 }
109
110
111 /* Gcc's warn_unused_result is fascist bullshit. */
112 #define doesnt_matter()
113
114 static void run_under_debugger(struct manifest *m, struct score *score)
115 {
116         char *command;
117         struct file_error *first;
118
119         first = list_top(&score->per_file_errors, struct file_error, list);
120
121         if (!ask("Should I run the first failing test under the debugger?"))
122                 return;
123
124         command = talloc_asprintf(m, "gdb -ex 'break tap.c:139' -ex 'run' %s",
125                                   first->file->compiled[COMPILE_NORMAL]);
126         if (system(command))
127                 doesnt_matter();
128 }
129
130 struct ccanlint tests_pass = {
131         .key = "tests_pass",
132         .name = "Module's run and api tests pass",
133         .check = do_run_tests,
134         .handle = run_under_debugger,
135         .can_run = can_run,
136         .needs = "tests_compile"
137 };
138
139 REGISTER_TEST(tests_pass);