json_out: make json_out_finished finish buffer.
[ccan] / tools / ccanlint / tests / info_exists.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <ccan/tal/tal.h>
3 #include <ccan/tal/str/str.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <err.h>
13 #include <string.h>
14 #include <ccan/noerr/noerr.h>
15
16 static void check_has_info(struct manifest *m,
17                            unsigned int *timeleft UNNEEDED,
18                            struct score *score)
19 {
20         if (m->info_file) {
21                 score->pass = true;
22                 score->score = score->total;
23                 add_info_options(m);
24         } else {
25                 score->error = tal_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 \"config.h\"\n"
35         "#include <stdio.h>\n"
36         "#include <string.h>\n"
37         "\n"
38         "/**\n"
39         " * %s - YOUR-ONE-LINE-DESCRIPTION-HERE\n"
40         " *\n"
41         " * This code ... YOUR-BRIEF-SUMMARY-HERE\n"
42         " *\n"
43         " * Example:\n"
44         " *     FULLY-COMPILABLE-INDENTED-TRIVIAL-BUT-USEFUL-EXAMPLE-HERE\n"
45         " */\n"
46         "int main(int argc, char *argv[])\n"
47         "{\n"
48         "       /* Expect exactly one argument */\n"
49         "       if (argc != 2)\n"
50         "               return 1;\n"
51         "\n"
52         "       if (strcmp(argv[1], \"depends\") == 0) {\n"
53         "               PRINTF-CCAN-PACKAGES-YOU-NEED-ONE-PER-LINE-IF-ANY\n"
54         "               return 0;\n"
55         "       }\n"
56         "\n"
57         "       return 1;\n"
58         "}\n";
59
60 static void create_info_template(struct manifest *m,
61                                  struct score *score UNNEEDED)
62 {
63         FILE *info;
64         const char *filename;
65
66         if (!ask("Should I create a template _info file for you?"))
67                 return;
68
69         filename = tal_fmt(m, "%s/%s", m->dir, "_info");
70         info = fopen(filename, "w");
71         if (!info)
72                 err(1, "Trying to create a template _info in %s", filename);
73
74         if (fprintf(info, template, m->modname) < 0) {
75                 unlink_noerr(filename);
76                 err(1, "Writing template into %s", filename);
77         }
78         fclose(info);
79 }
80
81 struct ccanlint info_exists = {
82         .key = "info_exists",
83         .name = "Module has _info file",
84         .compulsory = true,
85         .check = check_has_info,
86         .handle = create_info_template,
87         .needs = ""
88 };
89
90 REGISTER_TEST(info_exists);