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