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