1 #include <tools/ccanlint/ccanlint.h>
11 #include <ccan/talloc/talloc.h>
13 static void check_tests_exist(struct manifest *m,
14 unsigned int *timeleft, struct score *score);
16 static struct ccanlint tests_exist = {
18 .name = "Module has test directory with tests in it",
19 .check = check_tests_exist,
20 .needs = "info_exists"
22 REGISTER_TEST(tests_exist);
24 static void handle_no_tests(struct manifest *m, struct score *score)
28 char *test_dir = talloc_asprintf(m, "%s/test", m->dir), *run_file;
31 "CCAN modules have a directory called test/ which contains tests.\n"
32 "There are four kinds of tests: api, run, compile_ok and compile_fail:\n"
33 "you can tell which type of test a C file is by its name, eg 'run.c'\n"
34 "and 'run-simple.c' are both run tests.\n\n"
36 "The simplest kind of test is a run test, which must compile with no\n"
37 "warnings, and then run: it is expected to use ccan/tap to report its\n"
38 "results in a simple and portable format. It should #include the C\n"
39 "files from the module directly (so it can probe the internals): the\n"
40 "module will not be linked in. The test will be run in a temporary\n"
41 "directory, with the test directory symlinked under test/.\n\n"
43 "api tests are just like a run test, except it is a guarantee of API\n"
44 "stability: this test should pass on all future versions of the\n"
45 "module. They *are* linked to the module, since they should only\n"
46 "test the API, not the internal state.\n\n"
48 "compile_ok tests are a subset of run tests: they must compile and\n"
49 "link, but aren't run.\n\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\n"
56 "Note that only API tests are linked against the files in the module!\n"
59 if (!ask("Should I create a template test/run.c file for you?"))
62 if (mkdir(test_dir, 0700) != 0) {
64 err(1, "Creating test/ directory");
67 run_file = talloc_asprintf(test_dir, "%s/run.c", test_dir);
68 run = fopen(run_file, "w");
70 err(1, "Trying to create a test/run.c");
72 fprintf(run, "#include <ccan/%s/%s.h>\n", m->basename, m->basename);
73 if (!list_empty(&m->c_files)) {
74 fputs("/* Include the C files directly. */\n", run);
75 list_for_each(&m->c_files, i, list)
76 fprintf(run, "#include <ccan/%s/%s>\n",
77 m->basename, i->name);
80 "#include <ccan/tap/tap.h>\n\n"
83 " /* This is how many tests you plan to run */\n"
86 " /* Simple thing we expect to succeed */\n"
88 " /* Same, with an explicit description of the test. */\n"
89 " ok(some_test(), \"%s with no args should return 1\", \"some_test\")\n"
90 " /* How to print out messages for debugging. */\n"
91 " diag(\"Address of some_test is %p\", &some_test)\n"
92 " /* Conditional tests must be explicitly skipped. */\n"
93 "#if HAVE_SOME_FEATURE\n"
94 " ok1(test_some_feature())\n"
96 " skip(1, \"Don\'t have SOME_FEATURE\")\n"
99 " /* This exits depending on whether all tests passed */\n"
100 " return exit_status();\n"
105 static void check_tests_exist(struct manifest *m,
106 unsigned int *timeleft, struct score *score)
109 char *test_dir = talloc_asprintf(m, "%s/test", m->dir);
111 if (lstat(test_dir, &st) != 0) {
112 score->error = talloc_strdup(score, "No test directory");
114 err(1, "statting %s", test_dir);
115 tests_exist.handle = handle_no_tests;
116 /* We "pass" this. */
121 if (!S_ISDIR(st.st_mode)) {
122 score->error = talloc_strdup(score, "test is not a directory");
126 if (list_empty(&m->api_tests)
127 && list_empty(&m->run_tests)
128 && list_empty(&m->compile_ok_tests)
129 && list_empty(&m->compile_fail_tests)) {
130 score->error = talloc_strdup(score,
131 "No tests in test directory");
132 tests_exist.handle = handle_no_tests;
136 score->score = score->total;