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