ccanlint: with -vv, give details on attempts to compile examples.
[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 };
23
24 static void *check_has_info_documentation(struct manifest *m,
25                                           bool keep,
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 };
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         }
40
41         if (id.summary && id.description)
42                 return NULL;
43         return talloc_memdup(m, &id, sizeof(id));
44 }
45
46 /* This is defined below. */
47 extern struct ccanlint has_info_documentation;
48
49 static void create_info_template_doc(struct manifest *m, void *check_result)
50 {
51         int fd = open("_info.new", O_WRONLY|O_CREAT|O_EXCL, 0666);
52         FILE *new;
53         char *oldcontents;
54
55         if (fd < 0 || !(new = fdopen(fd, "w")))
56                 err(1, "Creating _info.new to insert documentation");
57
58         if (fprintf(new,
59                     "/**\n"
60                     " * %s - [[ONE LINE DESCRIPTION HERE]]\n"
61                     " *\n"
62                     " * Paragraphs why %s exists and where to use it.\n"
63                     " *\n"
64                     " * Followed by an Example: section with a standalone\n"
65                     " * (trivial and usually useless) program\n"
66                     " */\n", m->basename, m->basename) < 0) {
67                 unlink_noerr("_info.new");
68                 err(1, "Writing to _info.new to insert documentation");
69         }
70
71         oldcontents = grab_file(m, "_info", NULL);
72         if (!oldcontents) {
73                 unlink_noerr("_info.new");
74                 err(1, "Reading _info");
75         }
76         if (fprintf(new, "%s", oldcontents) < 0) {
77                 unlink_noerr("_info.new");
78                 err(1, "Appending _info to _info.new");
79         }
80         if (fclose(new) != 0) {
81                 unlink_noerr("_info.new");
82                 err(1, "Closing _info.new");
83         }
84         if (!move_file("_info.new", "_info")) {
85                 unlink_noerr("_info.new");
86                 err(1, "Renaming _info.new to _info");
87         }
88 }
89
90 static const char *describe_has_info_documentation(struct manifest *m,
91                                                    void *check_result)
92 {
93         struct info_docs *id = check_result;
94         char *reason = talloc_strdup(m, "");
95
96         if (!id->summary) {
97                 has_info_documentation.handle = create_info_template_doc;
98                 reason = talloc_asprintf_append(reason,
99                 "Your _info file has no module documentation.\n\n"
100                 "CCAN modules use /**-style comments for documentation: the\n"
101                 "overall documentation belongs in the _info metafile.\n");
102         }
103         if (!id->description)
104                 reason = talloc_asprintf_append(reason,
105                 "Your _info file has no module description.\n\n"
106                 "The lines after the first summary line in the _info file\n"
107                 "documentation should describe the purpose and use of the\n"
108                 "overall package\n");
109         return reason;
110 }
111
112 static unsigned int has_info_documentation_score(struct manifest *m,
113                                                  void *check_result)
114 {
115         struct info_docs *id = check_result;
116         return (unsigned int)id->summary + id->description;
117 }
118
119 struct ccanlint has_info_documentation = {
120         .key = "info-documentation",
121         .name = "Module has documentation in _info",
122         .total_score = 2,
123         .score = has_info_documentation_score,
124         .check = check_has_info_documentation,
125         .describe = describe_has_info_documentation,
126 };
127
128 REGISTER_TEST(has_info_documentation, NULL);