ccanlint: compile and run tests.
[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 static void *do_run_tests(struct manifest *m)
25 {
26         struct list_head *list = talloc(m, struct list_head);
27         char *failures = talloc_strdup(m, "");
28         struct ccan_file *i;
29
30         list_head_init(list);
31
32         run_tests.total_score = 0;
33         list_for_each(&m->run_tests, i, list) {
34                 char *testout;
35                 run_tests.total_score++;
36                 /* FIXME: timeout here */
37                 testout = run_command(m, i->compiled);
38                 if (!testout)
39                         continue;
40                 failures = talloc_asprintf_append(failures,
41                                                   "Running %s failed:\n",
42                                                   i->name);
43                 failures = talloc_append_string(failures, testout);
44         }
45
46         list_for_each(&m->api_tests, i, list) {
47                 char *testout;
48                 run_tests.total_score++;
49                 /* FIXME: timeout here */
50                 testout = run_command(m, i->compiled);
51                 if (!testout)
52                         continue;
53                 failures = talloc_asprintf_append(failures,
54                                                   "Running %s failed:\n",
55                                                   i->name);
56                 failures = talloc_append_string(failures, testout);
57         }
58
59         if (streq(failures, "")) {
60                 talloc_free(failures);
61                 failures = NULL;
62         }
63
64         return failures;
65 }
66
67 static unsigned int score_run_tests(struct manifest *m, void *check_result)
68 {
69         /* FIXME: be cleverer here */
70         return 0;
71 }
72
73 static const char *describe_run_tests(struct manifest *m,
74                                           void *check_result)
75 {
76         char *descrip = talloc_strdup(check_result, "Running tests failed:\n");
77
78         return talloc_append_string(descrip, check_result);
79 }
80
81 struct ccanlint run_tests = {
82         .name = "run and api tests run successfully",
83         .total_score = 1,
84         .score = score_run_tests,
85         .check = do_run_tests,
86         .describe = describe_run_tests,
87         .can_run = can_run,
88 };
89
90 REGISTER_TEST(run_tests, &compile_tests, NULL);