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