]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_exist.c
ccanlint: score_file_error() takes printf-format
[ccan] / tools / ccanlint / tests / tests_exist.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <limits.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <err.h>
11 #include <ccan/talloc/talloc.h>
12
13 extern struct ccanlint tests_exist;
14
15 static void handle_no_tests(struct manifest *m, struct score *score)
16 {
17         FILE *run;
18         struct ccan_file *i;
19         char *test_dir = talloc_asprintf(m, "%s/test", m->dir);
20
21         printf(
22         "CCAN modules have a directory called test/ which contains tests.\n"
23         "There are four kinds of tests: api, run, compile_ok and compile_fail:\n"
24         "you can tell which type of test a C file is by its name, eg 'run.c'\n"
25         "and 'run-simple.c' are both run tests.\n\n"
26
27         "The simplest kind of test is a run test, which must compile with no\n"
28         "warnings, and then run: it is expected to use ccan/tap to report its\n"
29         "results in a simple and portable format.  It should #include the C\n"
30         "files from the module directly (so it can probe the internals): the\n"
31         "module will not be linked in.  The test will be run in a temporary\n"
32         "directory, with the test directory symlinked under test/.\n\n"
33
34         "api tests are just like a run test, except it is a guarantee of API\n"
35         "stability: this test should pass on all future versions of the\n"
36         "module.  They *are* linked to the module, since they should only\n"
37         "test the API, not the internal state.\n\n"
38
39         "compile_ok tests are a subset of run tests: they must compile and\n"
40         "link, but aren't run.\n\n"
41
42         "compile_fail tests are tests which should fail to compile (or emit\n"
43         "warnings) or link when FAIL is defined, but should compile and link\n"
44         "when it's not defined: this helps ensure unrelated errors don't make\n"
45         "compilation fail.\n\n"
46
47         "Note that only API tests are linked against the files in the module!\n"
48                 );
49
50         if (!ask("Should I create a template test/run.c file for you?"))
51                 return;
52
53         if (mkdir(test_dir, 0700) != 0) {
54                 if (errno != EEXIST)
55                         err(1, "Creating test/ directory");
56         }
57
58         run = fopen("test/run.c", "w");
59         if (!run)
60                 err(1, "Trying to create a test/run.c");
61
62         fprintf(run, "#include <ccan/%s/%s.h>\n", m->basename, m->basename);
63         if (!list_empty(&m->c_files)) {
64                 fputs("/* Include the C files directly. */\n", run);
65                 list_for_each(&m->c_files, i, list)
66                         fprintf(run, "#include <ccan/%s/%s>\n",
67                                 m->basename, i->name);
68         }
69         fprintf(run, "%s",
70                 "#include <ccan/tap/tap.h>\n\n"
71                 "int main(void)\n"
72                 "{\n"
73                 "       /* This is how many tests you plan to run */\n"
74                 "       plan_tests(3);\n"
75                 "\n"
76                 "       /* Simple thing we expect to succeed */\n"
77                 "       ok1(some_test())\n"
78                 "       /* Same, with an explicit description of the test. */\n"
79                 "       ok(some_test(), \"%s with no args should return 1\", \"some_test\")\n"
80                 "       /* How to print out messages for debugging. */\n"
81                 "       diag(\"Address of some_test is %p\", &some_test)\n"
82                 "       /* Conditional tests must be explicitly skipped. */\n"
83                 "#if HAVE_SOME_FEATURE\n"
84                 "       ok1(test_some_feature())\n"
85                 "#else\n"
86                 "       skip(1, \"Don\'t have SOME_FEATURE\")\n"
87                 "#endif\n"
88                 "\n"
89                 "       /* This exits depending on whether all tests passed */\n"
90                 "       return exit_status();\n"
91                 "}\n");
92         fclose(run);
93 }
94
95 static void check_tests_exist(struct manifest *m,
96                             bool keep,
97                             unsigned int *timeleft, struct score *score)
98 {
99         struct stat st;
100         char *test_dir = talloc_asprintf(m, "%s/test", m->dir);
101
102         if (lstat(test_dir, &st) != 0) {
103                 score->error = talloc_strdup(score, "No test directory");
104                 if (errno != ENOENT)
105                         err(1, "statting %s", test_dir);
106                 tests_exist.handle = handle_no_tests;
107                 return;
108         }
109
110         if (!S_ISDIR(st.st_mode)) {
111                 score->error = talloc_strdup(score, "test is not a directory");
112                 return;
113         }
114
115         if (list_empty(&m->api_tests)
116             && list_empty(&m->run_tests)
117             && list_empty(&m->compile_ok_tests)) {
118                 if (list_empty(&m->compile_fail_tests)) {
119                         score->error = talloc_strdup(score,
120                                              "No tests in test directory");
121                         tests_exist.handle = handle_no_tests;
122                 } else
123                         score->error = talloc_strdup(score,
124                                      "No positive tests in test directory");
125                 return;
126         }
127         score->pass = true;
128         score->score = score->total;
129 }
130
131 struct ccanlint tests_exist = {
132         .key = "tests_exist",
133         .name = "Module has test directory with tests in it",
134         .check = check_tests_exist,
135         .needs = ""
136 };
137
138 REGISTER_TEST(tests_exist);