Skip to content

Commit

Permalink
added test for absolute links to docs.docker.com
Browse files Browse the repository at this point in the history
Signed-off-by: Adrien Duermael <adrien@duermael.com>
  • Loading branch information
aduermael committed Dec 27, 2016
1 parent b72b9d3 commit 464ce98
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tests/src/validator/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)
Expand Down Expand Up @@ -89,6 +90,52 @@ func testFrontMatterKeywords(mdBytes []byte) error {
return nil
}

// TestURLs tests if we're not using absolute paths for URLs
// when pointing to local pages.
func TestURLs(t *testing.T) {
filepath.Walk("/docs", func(path string, info os.FileInfo, err error) error {
if err != nil {
t.Error(err.Error(), "-", path)
}
published, mdBytes, err := isPublishedMarkdown(path)
if err != nil {
t.Error(err.Error(), "-", path)
}
if published == false {
return nil
}
err = testURLs(mdBytes)
if err != nil {
t.Error(err.Error(), "-", path)
}
return nil
})
}

// testURLs tests if we're not using absolute paths for URLs
// when pointing to local pages.
func testURLs(mdBytes []byte) error {
_, md, err := frontparser.ParseFrontmatterAndContent(mdBytes)
if err != nil {
return err
}

regularExpression, err := regexp.Compile(`\[[^\]]+\]\(([^\)]+)\)`)
if err != nil {
return err
}

submatches := regularExpression.FindAllStringSubmatch(string(md), -1)

for _, submatch := range submatches {
if strings.Contains(submatch[1], "docs.docker.com") {
return errors.New("found absolute link (" + strings.TrimSpace(submatch[1]) + ")")
}
}

return nil
}

//-----------------
// utils
//-----------------
Expand Down

0 comments on commit 464ce98

Please sign in to comment.