From d3ffce77f294b7f9ecf8c115b7fe2059d2cc87a5 Mon Sep 17 00:00:00 2001 From: Didier Divinerites Date: Sun, 23 May 2021 18:08:17 +0200 Subject: [PATCH] Add IgnoreInternalURLs option (Solve #168) --- htmltest/check-link.go | 19 ++++++++++++++----- htmltest/options.go | 21 +++++++++++++++++---- htmltest/options_test.go | 14 ++++++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/htmltest/check-link.go b/htmltest/check-link.go index 6a3a14b..5f89f15 100644 --- a/htmltest/check-link.go +++ b/htmltest/check-link.go @@ -3,16 +3,17 @@ package htmltest import ( "crypto/x509" "fmt" - "github.com/badoux/checkmail" - "github.com/wjdp/htmltest/htmldoc" - "github.com/wjdp/htmltest/issues" - "github.com/wjdp/htmltest/output" - "golang.org/x/net/html" "net/http" "net/url" "os" "path" "strings" + + "github.com/badoux/checkmail" + "github.com/wjdp/htmltest/htmldoc" + "github.com/wjdp/htmltest/issues" + "github.com/wjdp/htmltest/output" + "golang.org/x/net/html" ) func (hT *HTMLTest) checkLink(document *htmldoc.Document, node *html.Node) { @@ -276,6 +277,14 @@ func (hT *HTMLTest) checkInternal(ref *htmldoc.Reference) { return } + // Solve #168 + urlStr := ref.URLString() + + // Does this internal url match a internal url ignore rule? + if hT.opts.isInternalURLIgnored(urlStr) { + return + } + // First lookup in document store, refDoc, refExists := hT.documentStore.ResolveRef(ref) diff --git a/htmltest/options.go b/htmltest/options.go index 2a53a72..5fc8f42 100644 --- a/htmltest/options.go +++ b/htmltest/options.go @@ -38,8 +38,9 @@ type Options struct { EnforceHTML5 bool EnforceHTTPS bool - IgnoreURLs []interface{} - IgnoreDirs []interface{} + IgnoreURLs []interface{} + IgnoreInternalURLs []interface{} + IgnoreDirs []interface{} IgnoreInternalEmptyHash bool IgnoreEmptyHref bool @@ -102,8 +103,9 @@ func DefaultOptions() map[string]interface{} { "EnforceHTML5": false, "EnforceHTTPS": false, - "IgnoreURLs": []interface{}{}, - "IgnoreDirs": []interface{}{}, + "IgnoreURLs": []interface{}{}, + "IgnoreInternalURLs": []interface{}{}, + "IgnoreDirs": []interface{}{}, "IgnoreInternalEmptyHash": false, "IgnoreEmptyHref": false, @@ -182,3 +184,14 @@ func (opts *Options) isURLIgnored(url string) bool { } return false } + +// Solve #168 +// Is the given local URL ignored by the current configuration +func (opts *Options) isInternalURLIgnored(url string) bool { + for _, item := range opts.IgnoreInternalURLs { + if item.(string) == url { + return true + } + } + return false +} diff --git a/htmltest/options_test.go b/htmltest/options_test.go index c3b7db3..7206e9a 100644 --- a/htmltest/options_test.go +++ b/htmltest/options_test.go @@ -59,6 +59,20 @@ func TestIsURLIgnored(t *testing.T) { assert.IsFalse(t, "url left alone", hT.opts.isURLIgnored("http://assetstore.info/lib/test.js")) } +func TestIsInternalURLIgnored(t *testing.T) { + userOpts := map[string]interface{}{ + "IgnoreInternalURLs": []interface{}{"/misc/js/script.js"}, + "NoRun": true, + } + + hT, err := Test(userOpts) + output.CheckErrorPanic(err) + + assert.IsTrue(t, "url ignored", hT.opts.isURLIgnored("/misc/js/script.js")) + assert.IsFalse(t, "url left alone", hT.opts.isURLIgnored("misc/js/script.js")) + assert.IsFalse(t, "url left alone", hT.opts.isURLIgnored("/misc/js/script")) +} + func TestMergeHTTPHeaders(t *testing.T) { userOpts := map[string]interface{}{ "HTTPHeaders": map[interface{}]interface{}{