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