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