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