Skip to content

Commit

Permalink
Documentation: add link checker
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Mar 23, 2022
1 parent c76efae commit d09b319
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Documentation/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
- [API Definition](./howto/api.md)
- [Testing ClairV4](./howto/testing.md)
- [Concepts](./concepts.md)
- [Indexing](./concepts/indexing.md)
- [Matching](./concepts/matching.md)
- [Internal API](./concepts/api_internal.md)
- [Authentication](./concepts/authentication.md)
- [Notifications](./concepts/notifications.md)
- [Updaters and Airgap](./concepts/updatersandairgap.md)
- [Operation](./concepts/operation.md)
- [Contribution](./contribution.md)
- [Releases](./contribution/releases.md)
- [Commit Style](./contribution/commit_style.md)
Expand Down
1 change: 0 additions & 1 deletion Documentation/TODO.md

This file was deleted.

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

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) {
// Check that this is the docs test.
// These files are copied into the "book" directory, so when left around in
// a work tree, test will run there as well.
if _, err := os.Stat("index.html"); err == nil {
t.Skip("skip listing check in compiled docs")
}

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 d09b319

Please sign in to comment.