ccanlint: Add -k option to keep results.
[ccan] / tools / ccanlint / compulsory_tests / run_tests.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 static const char *can_run(struct manifest *m)
18 {
19         if (safe_mode)
20                 return "Safe mode enabled";
21         return NULL;
22 }
23
24 struct run_tests_result {
25         struct list_node list;
26         struct ccan_file *file;
27         const char *output;
28 };
29
30 static void *do_run_tests(struct manifest *m,
31                           bool keep,
32                           unsigned int *timeleft)
33 {
34         struct list_head *list = talloc(m, struct list_head);
35         struct run_tests_result *res;
36         struct ccan_file *i;
37         char *cmdout;
38         char *olddir;
39
40         /* We run tests in the module directory, so any paths
41          * referenced can all be module-local. */
42         olddir = talloc_getcwd(m);
43         if (!olddir)
44                 err(1, "Could not save cwd");
45         if (chdir(m->dir) != 0)
46                 err(1, "Could not chdir to %s", m->dir);
47
48         list_head_init(list);
49
50         list_for_each(&m->run_tests, i, list) {
51                 run_tests.total_score++;
52                 cmdout = run_command(m, timeleft, i->compiled);
53                 if (cmdout) {
54                         res = talloc(list, struct run_tests_result);
55                         res->file = i;
56                         res->output = talloc_steal(res, cmdout);
57                         list_add_tail(list, &res->list);
58                 }
59         }
60
61         list_for_each(&m->api_tests, i, list) {
62                 run_tests.total_score++;
63                 cmdout = run_command(m, timeleft, 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         if (chdir(olddir) != 0)
78                 err(1, "Could not chdir to %s", olddir);
79
80         return list;
81 }
82
83 static unsigned int score_run_tests(struct manifest *m, void *check_result)
84 {
85         struct list_head *list = check_result;
86         struct run_tests_result *i;
87         unsigned int score = run_tests.total_score;
88
89         list_for_each(list, i, list)
90                 score--;
91         return score;
92 }
93
94 static const char *describe_run_tests(struct manifest *m,
95                                           void *check_result)
96 {
97         struct list_head *list = check_result;
98         char *descrip = talloc_strdup(check_result, "Running tests failed:\n");
99         struct run_tests_result *i;
100
101         list_for_each(list, i, list)
102                 descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
103                                                  i->file->name, i->output);
104         return descrip;
105 }
106
107 /* Gcc's warn_unused_result is fascist bullshit. */
108 #define doesnt_matter()
109
110 static void run_under_debugger(struct manifest *m, void *check_result)
111 {
112         char *command;
113         struct list_head *list = check_result;
114         struct run_tests_result *first;
115
116         if (!ask("Should I run the first failing test under the debugger?"))
117                 return;
118
119         first = list_top(list, struct run_tests_result, list);
120         command = talloc_asprintf(m, "gdb -ex 'break tap.c:136' -ex 'run' %s",
121                                   first->file->compiled);
122         if (system(command))
123                 doesnt_matter();
124 }
125
126 struct ccanlint run_tests = {
127         .key = "run",
128         .name = "Module's run and api tests pass",
129         .score = score_run_tests,
130         .check = do_run_tests,
131         .describe = describe_run_tests,
132         .can_run = can_run,
133         .handle = run_under_debugger
134 };
135
136 REGISTER_TEST(run_tests, &compile_tests, NULL);