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