Skip to content

Commit

Permalink
docs: add test for linked pages
Browse files Browse the repository at this point in the history
This change checks the SUMMARY.md to verify that all Markdown files are
linked.

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jan 19, 2022
1 parent 4d8902a commit 8cfac96
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
3 changes: 0 additions & 3 deletions docs/contributor/contributor.md

This file was deleted.

65 changes: 65 additions & 0 deletions docs/listing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package docs

import (
"bufio"
"io/fs"
"os"
"path"
"regexp"
"sort"
"testing"

"github.com/google/go-cmp/cmp"
)

// TestListing fails if the SUMMARY.md falls out of sync with the markdown files
// in this directory.
func TestListing(t *testing.T) {
linkline, err := regexp.Compile(`\s*- \[.+\]\((.+)\)`)
if err != nil {
t.Fatal(err)
}
f, err := os.Open("SUMMARY.md")
if err != nil {
t.Fatal(err)
}
defer f.Close()

linked := []string{"SUMMARY.md"}
s := bufio.NewScanner(f)
for s.Scan() {
ms := linkline.FindSubmatch(s.Bytes())
switch {
case ms == nil, len(ms) == 1:
continue
case len(ms) == 2:
linked = append(linked, path.Clean(string(ms[1])))
}
}
if err := s.Err(); err != nil {
t.Error(err)
}
sort.Strings(linked)

var files []string
err = fs.WalkDir(os.DirFS("."), ".", func(p string, d fs.DirEntry, err error) error {
switch {
case err != nil:
return err
case d.IsDir():
return nil
case path.Ext(d.Name()) != ".md":
return nil
}
files = append(files, p)
return nil
})
if err != nil {
t.Error(err)
}
sort.Strings(files)

if !cmp.Equal(linked, files) {
t.Error(cmp.Diff(linked, files))
}
}

0 comments on commit 8cfac96

Please sign in to comment.