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