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