-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathdocs_structure_test.go
More file actions
118 lines (103 loc) · 3.65 KB
/
Copy pathdocs_structure_test.go
File metadata and controls
118 lines (103 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package fiber
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/gofiber/utils/v2"
"github.com/stretchr/testify/require"
)
// middlewareDocsDir holds one page per built-in middleware.
const middlewareDocsDir = "docs/middleware"
// Sections every middleware page carries, so a reader can jump to the
// signature or the config of any middleware without first reading the page.
var middlewareDocSections = []string{
"## Signatures",
"## Config",
"## Examples",
}
type middlewareDocException struct {
file string
reason string
skip []string
}
// Pages that are not shaped like a middleware page. Listing them here rather
// than loosening the check keeps the deviation visible and reviewable.
var middlewareDocExceptions = []middlewareDocException{
{
file: "adaptor.md",
skip: []string{"## Signatures", "## Config"},
reason: "a converter between net/http and Fiber, documented one function at a time and with no config",
},
{
file: "csrf.md",
skip: []string{"## Signatures", "## Config", "## Examples"},
reason: "guide-shaped page, config lives under Advanced Configuration and the examples under Recipes",
},
{
file: "session.md",
skip: []string{"## Signatures", "## Config"},
reason: "guide-shaped page, the config is reached through Configuration",
},
{
file: "skip.md",
skip: []string{"## Config"},
reason: "wraps a handler behind a predicate, there is no config struct",
},
}
// Test_Docs_MiddlewarePageSections keeps the middleware pages to one layout.
// The docs are synced to gofiber.io as they are, so a page that invents its
// own section names is only noticed once it is published.
func Test_Docs_MiddlewarePageSections(t *testing.T) {
t.Parallel()
skips := make(map[string]map[string]bool, len(middlewareDocExceptions))
for _, exception := range middlewareDocExceptions {
set := make(map[string]bool, len(exception.skip))
for _, section := range exception.skip {
set[section] = true
}
skips[exception.file] = set
}
entries, err := os.ReadDir(middlewareDocsDir)
require.NoError(t, err)
pages := make(map[string]bool, len(entries))
for _, entry := range entries {
name := entry.Name()
if entry.IsDir() || !strings.HasSuffix(name, ".md") {
continue
}
pages[name] = true
body, readErr := os.ReadFile(filepath.Join(middlewareDocsDir, name))
require.NoError(t, readErr)
headings := make(map[string]bool)
for line := range strings.SplitSeq(string(body), "\n") {
line = strings.TrimRight(line, " \r")
if strings.HasPrefix(line, "## ") {
headings[line] = true
}
}
for _, section := range middlewareDocSections {
if skips[name][section] {
// A page that has grown the section back keeps its exception alive
// and with it a hole in the check, so the skip has to go.
require.False(t, headings[section],
"%s/%s carries %q again - drop that section from its middlewareDocExceptions entry",
middlewareDocsDir, name, section)
continue
}
require.True(t, headings[section],
"%s/%s is missing %q - add the section, or add the page to middlewareDocExceptions with a reason",
middlewareDocsDir, name, section)
}
}
require.NotEmpty(t, pages, "no middleware pages found under %s", middlewareDocsDir)
// An exception for a page that no longer exists would silently excuse that
// page the day someone adds it back.
for _, exception := range middlewareDocExceptions {
require.NotEmpty(t, utils.TrimSpace(exception.reason),
"middlewareDocExceptions covers %s without a reason", exception.file)
require.True(t, pages[exception.file],
"middlewareDocExceptions covers %s (%s) but that page does not exist",
exception.file, exception.reason)
}
}