From 464ce98aaec1158507d10f54cd40d810b99eb4ff Mon Sep 17 00:00:00 2001 From: Adrien Duermael Date: Wed, 7 Dec 2016 14:10:34 -0800 Subject: [PATCH] added test for absolute links to docs.docker.com Signed-off-by: Adrien Duermael --- tests/src/validator/markdown_test.go | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/src/validator/markdown_test.go b/tests/src/validator/markdown_test.go index dbdfc51ed9..5e85b6181e 100644 --- a/tests/src/validator/markdown_test.go +++ b/tests/src/validator/markdown_test.go @@ -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 //-----------------