json_out: make json_out_finished finish buffer.
[ccan] / tools / ccanlint / tests / info_documentation_exists.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/doc_extract.h>
3 #include <tools/tools.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 <ccan/str/str.h>
14 #include <ccan/noerr/noerr.h>
15 #include <ccan/tal/grab_file/grab_file.h>
16
17 static void check_info_documentation_exists(struct manifest *m,
18                                             unsigned int *timeleft,
19                                             struct score *score);
20
21 static struct ccanlint info_documentation_exists = {
22         .key = "info_documentation_exists",
23         .name = "Module has documentation in _info",
24         .check = check_info_documentation_exists,
25         .needs = "info_exists"
26 };
27
28 static void create_info_template_doc(struct manifest *m,
29                                      struct score *score UNNEEDED)
30 {
31         int fd;
32         FILE *new;
33         char *oldcontents;
34
35         if (!ask("Should I prepend description to _info file for you?"))
36                 return;
37
38         fd = open("_info.new", O_WRONLY|O_CREAT|O_EXCL, 0666);
39         if (fd < 0 || !(new = fdopen(fd, "w")))
40                 err(1, "Creating _info.new to insert documentation");
41
42         if (fprintf(new,
43                     "/**\n"
44                     " * %s - [[ONE LINE DESCRIPTION HERE]]\n"
45                     " *\n"
46                     " * Paragraphs why %s exists and where to use it.\n"
47                     " *\n"
48                     " * Followed by an Example: section with a standalone\n"
49                     " * (trivial and usually useless) program\n"
50                     " */\n", m->modname, m->basename) < 0) {
51                 unlink_noerr("_info.new");
52                 err(1, "Writing to _info.new to insert documentation");
53         }
54
55         oldcontents = grab_file(m, m->info_file->fullname);
56         if (!oldcontents) {
57                 unlink_noerr("_info.new");
58                 err(1, "Reading %s", m->info_file->fullname);
59         }
60         if (fprintf(new, "%s", oldcontents) < 0) {
61                 unlink_noerr("_info.new");
62                 err(1, "Appending _info to _info.new");
63         }
64         if (fclose(new) != 0) {
65                 unlink_noerr("_info.new");
66                 err(1, "Closing _info.new");
67         }
68         if (!move_file("_info.new", m->info_file->fullname)) {
69                 unlink_noerr("_info.new");
70                 err(1, "Renaming _info.new to %s", m->info_file->fullname);
71         }
72 }
73
74 static void check_info_documentation_exists(struct manifest *m,
75                                             unsigned int *timeleft UNNEEDED,
76                                             struct score *score)
77 {
78         struct list_head *infodocs = get_ccan_file_docs(m->info_file);
79         struct doc_section *d;
80         bool summary = false, description = false;
81
82         /* We don't fail ccanlint for this. */
83         score->pass = true;
84
85         list_for_each(infodocs, d, list) {
86                 if (!streq(d->function, m->modname))
87                         continue;
88                 if (streq(d->type, "summary"))
89                         summary = true;
90                 if (streq(d->type, "description"))
91                         description = true;
92         }
93
94         if (summary && description) {
95                 score->score = score->total;
96         } else if (!summary) {
97                 score->error = tal_strdup(score,
98                 "_info file has no module documentation.\n\n"
99                 "CCAN modules use /**-style comments for documentation: the\n"
100                 "overall documentation belongs in the _info metafile.\n");
101                 info_documentation_exists.handle = create_info_template_doc;
102         } else if (!description)  {
103                 score->error = tal_strdup(score,
104                 "_info file has no module description.\n\n"
105                 "The lines after the first summary line in the _info file\n"
106                 "documentation should describe the purpose and use of the\n"
107                 "overall package\n");
108         }
109 }
110
111 REGISTER_TEST(info_documentation_exists);
112