]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/has_info_documentation.c
ccanlint: clean up test short descriptions
[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 struct info_docs
19 {
20         bool summary;
21         bool description;
22         bool example;
23 };
24
25 static void *check_has_info_documentation(struct manifest *m)
26 {
27         struct list_head *infodocs = get_ccan_file_docs(m->info_file);
28         struct doc_section *d;
29         struct info_docs id = { false, false, false };
30
31         list_for_each(infodocs, d, list) {
32                 if (!streq(d->function, m->basename))
33                         continue;
34                 if (streq(d->type, "summary"))
35                         id.summary = true;
36                 if (streq(d->type, "description"))
37                         id.description = true;
38                 if (streq(d->type, "example"))
39                         id.example = true;
40         }
41
42         if (id.summary && id.description && id.example)
43                 return NULL;
44         return talloc_memdup(m, &id, sizeof(id));
45 }
46
47 /* This is defined below. */
48 extern struct ccanlint has_info_documentation;
49
50 static void create_info_template_doc(struct manifest *m, void *check_result)
51 {
52         int fd = open("_info.new", O_WRONLY|O_CREAT|O_EXCL, 0666);
53         FILE *new;
54         char *oldcontents;
55
56         if (fd < 0 || !(new = fdopen(fd, "w")))
57                 err(1, "Creating _info.new to insert documentation");
58
59         if (fprintf(new,
60                     "/**\n"
61                     " * %s - [[ONE LINE DESCRIPTION HERE]]\n"
62                     " *\n"
63                     " * Paragraphs why %s exists and where to use it.\n"
64                     " *\n"
65                     " * Followed by an Example: section with a standalone\n"
66                     " * (trivial and usually useless) program\n"
67                     " */\n", m->basename, m->basename) < 0) {
68                 unlink_noerr("_info.new");
69                 err(1, "Writing to _info.new to insert documentation");
70         }
71
72         oldcontents = grab_file(m, "_info", NULL);
73         if (!oldcontents) {
74                 unlink_noerr("_info.new");
75                 err(1, "Reading _info");
76         }
77         if (fprintf(new, "%s", oldcontents) < 0) {
78                 unlink_noerr("_info.new");
79                 err(1, "Appending _info to _info.new");
80         }
81         if (fclose(new) != 0) {
82                 unlink_noerr("_info.new");
83                 err(1, "Closing _info.new");
84         }
85         if (!move_file("_info.new", "_info")) {
86                 unlink_noerr("_info.new");
87                 err(1, "Renaming _info.new to _info");
88         }
89 }
90
91 static const char *describe_has_info_documentation(struct manifest *m,
92                                                    void *check_result)
93 {
94         struct info_docs *id = check_result;
95         char *reason = talloc_strdup(m, "");
96
97         if (!id->summary) {
98                 has_info_documentation.handle = create_info_template_doc;
99                 reason = talloc_asprintf_append(reason,
100                 "Your _info file has no module documentation.\n\n"
101                 "CCAN modules use /**-style comments for documentation: the\n"
102                 "overall documentation belongs in the _info metafile.\n");
103         }
104         if (!id->description)
105                 reason = talloc_asprintf_append(reason,
106                 "Your _info file has no module description.\n\n"
107                 "The lines after the first summary line in the _info file\n"
108                 "documentation should describe the purpose and use of the\n"
109                 "overall package\n");
110         if (!id->example)
111                 reason = talloc_asprintf_append(reason,
112                 "Your _info file has no module example.\n\n"
113                 "There should be an Example: section of the _info documentation\n"
114                 "which provides a concise toy program which uses your module\n");
115         return reason;
116 }
117
118 static unsigned int has_info_documentation_score(struct manifest *m,
119                                                  void *check_result)
120 {
121         struct info_docs *id = check_result;
122         return id->summary + id->description + id->example;
123 }
124
125 struct ccanlint has_info_documentation = {
126         .key = "info-documentation",
127         .name = "Module has documentation in _info",
128         .total_score = 3,
129         .score = has_info_documentation_score,
130         .check = check_has_info_documentation,
131         .describe = describe_has_info_documentation,
132 };
133
134 REGISTER_TEST(has_info_documentation, NULL);