ccanlint: get rid of separate class of compulsory tests.
[ccan] / tools / ccanlint / tests / info_exists.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 <string.h>
12 #include <ccan/noerr/noerr.h>
13 #include <ccan/talloc/talloc.h>
14
15 static void check_has_info(struct manifest *m,
16                            bool keep,
17                            unsigned int *timeleft,
18                            struct score *score)
19 {
20         if (m->info_file) {
21                 score->pass = true;
22                 score->score = score->total;
23         } else {
24                 score->error = talloc_strdup(score,
25         "You have no _info file.\n\n"
26         "The file _info contains the metadata for a ccan package: things\n"
27         "like the dependencies, the documentation for the package as a whole\n"
28         "and license information.\n");
29         }
30 }
31
32 static const char template[] =
33         "#include <string.h>\n"
34         "#include \"config.h\"\n"
35         "\n"
36         "/**\n"
37         " * %s - YOUR-ONE-LINE-DESCRIPTION-HERE\n"
38         " *\n"
39         " * This code ... YOUR-BRIEF-SUMMARY-HERE\n"
40         " *\n"
41         " * Example:\n"
42         " *     FULLY-COMPILABLE-INDENTED-TRIVIAL-BUT-USEFUL-EXAMPLE-HERE\n"
43         " */\n"
44         "int main(int argc, char *argv[])\n"
45         "{\n"
46         "       /* Expect exactly one argument */\n"
47         "       if (argc != 2)\n"
48         "               return 1;\n"
49         "\n"
50         "       if (strcmp(argv[1], \"depends\") == 0) {\n"
51         "               PRINTF-CCAN-PACKAGES-YOU-NEED-ONE-PER-LINE-IF-ANY\n"
52         "               return 0;\n"
53         "       }\n"
54         "\n"
55         "       return 1;\n"
56         "}\n";
57
58 static void create_info_template(struct manifest *m, struct score *score)
59 {
60         FILE *info;
61         const char *filename;
62
63         if (!ask("Should I create a template _info file for you?"))
64                 return;
65
66         filename = talloc_asprintf(m, "%s/%s", m->dir, "_info");
67         info = fopen(filename, "w");
68         if (!info)
69                 err(1, "Trying to create a template _info in %s", filename);
70
71         if (fprintf(info, template, m->basename) < 0) {
72                 unlink_noerr(filename);
73                 err(1, "Writing template into %s", filename);
74         }
75         fclose(info);
76 }
77
78 struct ccanlint info_exists = {
79         .key = "info_exists",
80         .name = "Module has _info file",
81         .compulsory = true,
82         .check = check_has_info,
83         .handle = create_info_template,
84         .needs = ""
85 };
86
87 REGISTER_TEST(info_exists);