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 119 120 121 122 123 124 125
|
package docs_test
import (
"go/ast"
"go/doc"
"go/parser"
"go/token"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/internal/test_helpers"
. "github.com/onsi/gomega"
)
func TestDocs(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Docs Suite")
}
var anchors test_helpers.Anchors
var _ = BeforeSuite(func() {
var err error
anchors, err = test_helpers.LoadAnchors(test_helpers.DOCS, "../")
Ω(err).ShouldNot(HaveOccurred())
})
var _ = Describe("Validating internal links", func() {
var entries = []TableEntry{
Entry("Narrative Documentation", "index.md"),
Entry("V2 Migration Documentation", "MIGRATING_TO_V2.md"),
Entry("Repo Readme", "README.md"),
}
DescribeTable("Ensuring no headings have any markdown formatting characters in them", func(name string) {
headings, err := test_helpers.LoadMarkdownHeadings(test_helpers.DOCS.DocWithName(name).Path("../"))
Ω(err).ShouldNot(HaveOccurred())
failed := false
for _, heading := range headings {
if strings.ContainsAny(heading, "`*_~#") {
failed = true
GinkgoWriter.Printf("%s: '%s'\n", name, heading)
}
}
if failed {
Fail("Identified invalid headings")
}
}, entries)
DescribeTable("Ensuring all anchors resolve", func(name string) {
links, err := test_helpers.LoadMarkdownLinks(test_helpers.DOCS.DocWithName(name).Path("../"))
Ω(err).ShouldNot(HaveOccurred())
Ω(links).ShouldNot(BeEmpty())
failed := false
for _, link := range links {
if !anchors.IsResolvable(name, link) {
failed = true
GinkgoWriter.Printf("%s: '%s'\n", name, link)
}
}
if failed {
Fail("Identified invalid links")
}
}, entries)
})
var _ = Describe("Validating godoc links", func() {
It("validates that all links in the core dsl package are good", func() {
fset := token.NewFileSet()
entries, err := os.ReadDir("../")
Ω(err).ShouldNot(HaveOccurred())
parsedFiles := []*ast.File{}
for _, entry := range entries {
name := entry.Name()
if !strings.HasSuffix(name, ".go") {
continue
}
parsed, err := parser.ParseFile(fset, filepath.Join("../", name), nil, parser.ParseComments)
Ω(err).ShouldNot(HaveOccurred())
parsedFiles = append(parsedFiles, parsed)
}
p, err := doc.NewFromFiles(fset, parsedFiles, "github.com/onsi/ginkgo/v2")
Ω(err).ShouldNot(HaveOccurred())
var b strings.Builder
b.WriteString(p.Doc)
b.WriteString("\n")
for _, elem := range p.Consts {
b.WriteString(elem.Doc)
b.WriteString("\n")
}
for _, elem := range p.Types {
b.WriteString(elem.Doc)
b.WriteString("\n")
}
for _, elem := range p.Vars {
b.WriteString(elem.Doc)
b.WriteString("\n")
}
for _, elem := range p.Funcs {
b.WriteString(elem.Doc)
b.WriteString("\n")
}
doc := b.String()
urlRegexp := regexp.MustCompile(`https*[\w:/#\-\.]*`)
links := urlRegexp.FindAllString(doc, -1)
failed := false
for _, link := range links {
if !anchors.IsResolvable("", link) {
failed = true
GinkgoWriter.Printf("Godoc: '%s'\n", link)
}
}
if failed {
Fail("Identified invalid links")
}
})
})
|