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