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