Skip to content

Commit

Permalink
CI - added tests for relative links
Browse files Browse the repository at this point in the history
Signed-off-by: Adrien Duermael <[email protected]>
  • Loading branch information
aduermael committed Jan 9, 2017
1 parent 75cde9c commit 047a4e8
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions tests/src/validator/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"golang.org/x/net/html"
"net/url"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestURLs(t *testing.T) {

count++

err = testURLs(htmlBytes)
err = testURLs(htmlBytes, path)
if err != nil {
t.Error(err.Error(), "-", relPath)
}
Expand All @@ -62,7 +63,7 @@ func TestURLs(t *testing.T) {

// testURLs tests if we're not using absolute paths for URLs
// when pointing to local pages.
func testURLs(htmlBytes []byte) error {
func testURLs(htmlBytes []byte, htmlPath string) error {

reader := bytes.NewReader(htmlBytes)

Expand All @@ -78,7 +79,7 @@ func testURLs(htmlBytes []byte) error {
case html.StartTagToken:
t := z.Token()

url := ""
urlStr := ""

// check tag types
switch t.Data {
Expand All @@ -89,29 +90,72 @@ func testURLs(htmlBytes []byte) error {
if !ok {
break
}
url = href
urlStr = href

case "img":
countImages++
ok, src := getSrc(t)
if !ok {
return errors.New("img with no src: " + t.String())
}
url = src
urlStr = src
}

// there's an url to test!
if url != "" {
if strings.HasPrefix(url, "http://docs.docker.com") || strings.HasPrefix(url, "https://docs.docker.com") {
if urlStr != "" {
u, err := url.Parse(urlStr)
if err != nil {
return errors.New("can't parse url: " + t.String())
}
// test with github.com
if u.Scheme != "" && u.Host == "docs.docker.com" {
return errors.New("found absolute link: " + t.String())
}

// relative link
if u.Scheme == "" {
p := filepath.Join(htmlPath, mdToHtmlPath(u.Path))
if _, err := os.Stat(p); os.IsNotExist(err) {

fail := true

// index.html could mean there's a corresponding index.md meaning built the correct path
// but Jekyll actually creates index.html files for all md files.
// foo.md -> foo/index.html
// it does this to prettify urls, content of foo.md would then be rendered here:
// http://domain.com/foo/ (instead of http://domain.com/foo.html)
// so if there's an error, let's see if index.md exists, otherwise retry from parent folder
if filepath.Base(htmlPath) == "index.html" {
// retry from parent folder
p = filepath.Join(filepath.Dir(htmlPath), "..", mdToHtmlPath(u.Path))
if _, err := os.Stat(p); err == nil {
fail = false
}
}

if fail {
return errors.New("relative link to non-existent resource: " + t.String())
}
}
}
}
}
}

return nil
}

func mdToHtmlPath(mdPath string) string {
if strings.HasSuffix(mdPath, ".md") == false {
// file is not a markdown, don't change anything
return mdPath
}
if strings.HasSuffix(mdPath, "index.md") {
return strings.TrimSuffix(mdPath, "md") + "html"
}
return strings.TrimSuffix(mdPath, ".md") + "/index.html"
}

// helpers

func getHref(t html.Token) (ok bool, href string) {
Expand Down

0 comments on commit 047a4e8

Please sign in to comment.