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