Skip to content

Commit

Permalink
look for href and src attributes in a and img tags
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 12, 2016
1 parent 7fbde8d commit 045c025
Show file tree
Hide file tree
Showing 510 changed files with 137,924 additions and 83 deletions.
130 changes: 130 additions & 0 deletions tests/src/validator/html_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package main

import (
"bytes"
"errors"
"fmt"
"golang.org/x/net/html"
"os"
"path/filepath"
"strings"
"testing"
)

var countLinks = 0
var countImages = 0

// TestURLs tests if we're not using absolute paths for URLs
// when pointing to local pages.
func TestURLs(t *testing.T) {
count := 0

filepath.Walk("/usr/src/app/allvbuild", func(path string, info os.FileInfo, err error) error {

relPath := strings.TrimPrefix(path, "/usr/src/app/allvbuild")

if err != nil {
t.Error(err.Error(), "-", relPath)
}
b, htmlBytes, err := isHTML(path)
if err != nil {
t.Error(err.Error(), "-", relPath)
}
// don't check non-html files
if b == false {
return nil
}

count++

err = testURLs(htmlBytes)
if err != nil {
t.Error(err.Error(), "-", relPath)
}
return nil
})

fmt.Println("found", count, "html files")
fmt.Println("found", countLinks, "links")
fmt.Println("found", countImages, "images")
}

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

reader := bytes.NewReader(htmlBytes)

z := html.NewTokenizer(reader)

for {
tt := z.Next()

switch tt {
case html.ErrorToken:
// End of the document, we're done
return nil
case html.StartTagToken:
t := z.Token()
// check tag types
switch t.Data {
case "a":
countLinks++
ok, _ := getHref(t)
// skip, it may just be an anchor
if !ok {
break
}

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

// _, 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
}

// helpers

func getHref(t html.Token) (ok bool, href string) {
for _, a := range t.Attr {
if a.Key == "href" {
href = a.Val
ok = true
}
}
return
}

func getSrc(t html.Token) (ok bool, src string) {
for _, a := range t.Attr {
if a.Key == "src" {
src = a.Val
ok = true
}
}
return
}
83 changes: 0 additions & 83 deletions tests/src/validator/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package main
import (
"errors"
"github.com/gdevillele/frontparser"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)

Expand Down Expand Up @@ -89,83 +86,3 @@ 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
//-----------------

// isPublishedMarkdown returns wether a file is a published markdown or not
// as a convenience it also returns the markdown bytes to avoid reading files twice
func isPublishedMarkdown(path string) (bool, []byte, error) {
if strings.HasSuffix(path, ".md") {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return false, nil, err
}
if frontparser.HasFrontmatterHeader(fileBytes) {
fm, _, err := frontparser.ParseFrontmatterAndContent(fileBytes)
if err != nil {
return false, nil, err
}
// skip markdowns that are not published
if published, exists := fm["published"]; exists {
if publishedBool, ok := published.(bool); ok {
if publishedBool {
// file is markdown, has frontmatter and is published
return true, fileBytes, nil
}
}
} else {
// if "published" field is missing, it means published == true
return true, fileBytes, nil
}
}
}
return false, nil, nil
}
57 changes: 57 additions & 0 deletions tests/src/validator/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"github.com/gdevillele/frontparser"
"io/ioutil"
"os"
"strings"
)

// isPublishedMarkdown returns wether a file is a published markdown or not
// as a convenience it also returns the markdown bytes to avoid reading files twice
func isPublishedMarkdown(path string) (bool, []byte, error) {
if strings.HasSuffix(path, ".md") {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return false, nil, err
}
if frontparser.HasFrontmatterHeader(fileBytes) {
fm, _, err := frontparser.ParseFrontmatterAndContent(fileBytes)
if err != nil {
return false, nil, err
}
// skip markdowns that are not published
if published, exists := fm["published"]; exists {
if publishedBool, ok := published.(bool); ok {
if publishedBool {
// file is markdown, has frontmatter and is published
return true, fileBytes, nil
}
}
} else {
// if "published" field is missing, it means published == true
return true, fileBytes, nil
}
}
}
return false, nil, nil
}

// isHTML returns wether a file is an html file or not
// as a convenience it also returns the markdown bytes to avoid reading files twice
func isHTML(path string) (bool, []byte, error) {
if strings.HasSuffix(path, ".html") {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return false, nil, err
}
return true, fileBytes, nil
}
return false, nil, nil
}

// fileExists returns true if the given file exists
func fileExists(name string) bool {
_, err := os.Stat(name)
return err == nil
}
10 changes: 10 additions & 0 deletions tests/src/vendor/golang.org/x/net/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/src/vendor/golang.org/x/net/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/src/vendor/golang.org/x/net/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tests/src/vendor/golang.org/x/net/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/src/vendor/golang.org/x/net/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/src/vendor/golang.org/x/net/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions tests/src/vendor/golang.org/x/net/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/src/vendor/golang.org/x/net/README

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 045c025

Please sign in to comment.