added test for absolute links to docs.docker.com

Signed-off-by: Adrien Duermael <adrien@duermael.com>
This commit is contained in:
Adrien Duermael 2016-12-07 14:10:34 -08:00
parent b72b9d3ba5
commit 464ce98aae
1 changed files with 47 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)
@ -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
//-----------------