ccanlint: fix directory issues properly.
[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 {
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                 /* FIXME: timeout here */
51                 cmdout = run_command(m, i->compiled);
52                 if (cmdout) {
53                         res = talloc(list, struct run_tests_result);
54                         res->file = i;
55                         res->output = talloc_steal(res, cmdout);
56                         list_add_tail(list, &res->list);
57                 }
58         }
59
60         list_for_each(&m->api_tests, i, list) {
61                 run_tests.total_score++;
62                 /* FIXME: timeout here */
63                 cmdout = run_command(m, 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 static void run_under_debugger(struct manifest *m, void *check_result)
108 {
109         char *command;
110         struct list_head *list = check_result;
111         struct run_tests_result *first;
112
113         if (!ask("Should I run the first failing test under the debugger?"))
114                 return;
115
116         first = list_top(list, struct run_tests_result, list);
117         command = talloc_asprintf(m, "gdb -ex 'break tap.c:136' -ex 'run' %s",
118                                   first->file->compiled);
119         system(command);
120 }
121
122 struct ccanlint run_tests = {
123         .name = "run and api tests run successfully",
124         .score = score_run_tests,
125         .check = do_run_tests,
126         .describe = describe_run_tests,
127         .can_run = can_run,
128         .handle = run_under_debugger
129 };
130
131 REGISTER_TEST(run_tests, &compile_tests, NULL);