]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/compulsory_tests/run_tests.c
ccanlint: use familiar names for temporary files, show them with -vv.
[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
39         list_head_init(list);
40
41         list_for_each(&m->run_tests, i, list) {
42                 run_tests.total_score++;
43                 cmdout = run_command(m, timeleft, i->compiled);
44                 if (cmdout) {
45                         res = talloc(list, struct run_tests_result);
46                         res->file = i;
47                         res->output = talloc_steal(res, cmdout);
48                         list_add_tail(list, &res->list);
49                 }
50         }
51
52         list_for_each(&m->api_tests, i, list) {
53                 run_tests.total_score++;
54                 cmdout = run_command(m, timeleft, i->compiled);
55                 if (cmdout) {
56                         res = talloc(list, struct run_tests_result);
57                         res->file = i;
58                         res->output = talloc_steal(res, cmdout);
59                         list_add_tail(list, &res->list);
60                 }
61         }
62
63         if (list_empty(list)) {
64                 talloc_free(list);
65                 list = NULL;
66         }
67
68         return list;
69 }
70
71 static unsigned int score_run_tests(struct manifest *m, void *check_result)
72 {
73         struct list_head *list = check_result;
74         struct run_tests_result *i;
75         unsigned int score = run_tests.total_score;
76
77         list_for_each(list, i, list)
78                 score--;
79         return score;
80 }
81
82 static const char *describe_run_tests(struct manifest *m,
83                                           void *check_result)
84 {
85         struct list_head *list = check_result;
86         char *descrip = talloc_strdup(check_result, "Running tests failed:\n");
87         struct run_tests_result *i;
88
89         list_for_each(list, i, list)
90                 descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
91                                                  i->file->name, i->output);
92         return descrip;
93 }
94
95 /* Gcc's warn_unused_result is fascist bullshit. */
96 #define doesnt_matter()
97
98 static void run_under_debugger(struct manifest *m, void *check_result)
99 {
100         char *command;
101         struct list_head *list = check_result;
102         struct run_tests_result *first;
103
104         if (!ask("Should I run the first failing test under the debugger?"))
105                 return;
106
107         first = list_top(list, struct run_tests_result, list);
108         command = talloc_asprintf(m, "gdb -ex 'break tap.c:136' -ex 'run' %s",
109                                   first->file->compiled);
110         if (system(command))
111                 doesnt_matter();
112 }
113
114 struct ccanlint run_tests = {
115         .key = "run",
116         .name = "Module's run and api tests pass",
117         .score = score_run_tests,
118         .check = do_run_tests,
119         .describe = describe_run_tests,
120         .can_run = can_run,
121         .handle = run_under_debugger
122 };
123
124 REGISTER_TEST(run_tests, &compile_tests, NULL);