Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply IgnoreURLs regexs to internal and file URLs too #175

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ htmltest uses a YAML configuration file. Put `.htmltest.yml` in the same directo
| `EnforceHTML5` | Fails when the doctype isn't `<!DOCTYPE html>`. | `false` |
| `EnforceHTTPS` | Fails when encountering an `http://` link. Useful to prevent mixed content errors when serving over HTTPS. | `false` |
| `IgnoreURLs` | Array of regexs of URLs to ignore. | empty |
| `IgnoreInternalURLs` | Array of strings of internal URLs to ignore. | empty |
| `IgnoreInternalURLs` | Array of strings of internal URLs to ignore. Exact matches only. ⚠ Likely to be deprecated, use `IgnoreURLs` instead. | empty |
| `IgnoreDirs` | Array of regexs of directories to ignore when scanning for HTML files. | empty |
| `IgnoreInternalEmptyHash` | When true prevents raising an error for links with `href="#"`. | `false` |
| `IgnoreEmptyHref` | When true prevents raising an error for links with `href=""`. | `false` |
Expand Down Expand Up @@ -189,8 +189,7 @@ DirectoryPath: "_site"
EnforceHTTPS: true
IgnoreURLs:
- "example.com"
IgnoreInternalURLs:
- "/misc/js/script.js"
- "^/misc/js/script.js$"
IgnoreDirs:
- "lib"
CacheExpires: "6h"
Expand Down
6 changes: 3 additions & 3 deletions htmltest/check-link.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,11 @@ func (hT *HTMLTest) checkInternal(ref *htmldoc.Reference) {
return
}

// Solve #168
urlStr := ref.URLString()

// Does this internal url match an internal url ignore rule?
if hT.opts.isInternalURLIgnored(urlStr) {
// Does this internal url match either a standard URL ignore rule or internal
// url ignore rule?
if hT.opts.isInternalURLIgnored(urlStr) || hT.opts.isURLIgnored(urlStr) {
return
}

Expand Down
49 changes: 45 additions & 4 deletions htmltest/check-link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,24 @@ func TestAnchorInternalBrokenIgnore(t *testing.T) {
tExpectIssueCount(t, hT, 0)
}

func TestAnchorInternalIgnoreUrl(t *testing.T) {
// ignores internal links in IgnoreURLs
hT := tTestFileOpts("fixtures/links/brokenLinkInternal.html",
map[string]interface{}{
"IgnoreURLs": []interface{}{"no\\w+.html"},
})
tExpectIssueCount(t, hT, 0)
}

func TestAnchorFileIgnoreUrl(t *testing.T) {
// ignores file links in IgnoreURLs
hT := tTestFileOpts("fixtures/links/brokenLinkFile.html",
map[string]interface{}{
"IgnoreURLs": []interface{}{"no\\w+.html"},
})
tExpectIssueCount(t, hT, 0)
}

func TestAnchorInternalRelativeLinksBase(t *testing.T) {
// passes for relative links with a base
hT := tTestFile("fixtures/links/relativeLinksWithBase.html")
Expand Down Expand Up @@ -498,20 +516,43 @@ func TestAnchorInternalHashWeird(t *testing.T) {
tExpectIssueCount(t, hT, 0)
}

func TestAnchorInternalUrl(t *testing.T) {
// fails for internal linking writen not in IgnoreInternalURLs (#168)
func TestAnchorInternalUrlDoesNotExist(t *testing.T) {
// fails for internal link not in IgnoreInternalURLs (#168)
hT := tTestFile("fixtures/links/link_directory_internal_invalid.html")
tExpectIssueCount(t, hT, 1)
tExpectIssue(t, hT, "target does not exist", 1)
}

func TestAnchorInternalUrlOption(t *testing.T) {
// passes for internal linking writen in IgnoreInternalURLs option (#168)
func TestAnchorInternalUrlIgnoreUsingIgnoreInternalURLs(t *testing.T) {
// passes for internal link in IgnoreInternalURLs option (#168)
hT := tTestFileOpts("fixtures/links/link_directory_internal_valid.html",
map[string]interface{}{"IgnoreInternalURLs": []interface{}{"/misc/js/script.js"}})
tExpectIssueCount(t, hT, 0)
}

func TestAnchorInternalUrlIgnoreInternalURLsIsStrict(t *testing.T) {
// fails as IgnoreInternalURLs requires a string match
hT := tTestFileOpts("fixtures/links/link_directory_internal_valid.html",
map[string]interface{}{"IgnoreInternalURLs": []interface{}{"misc/js/script.js"}})
tExpectIssueCount(t, hT, 1)
tExpectIssue(t, hT, "target does not exist", 1)
}

func TestAnchorInternalUrlIgnoreUsingIgnoreURLs(t *testing.T) {
// passes for internal link in IgnoreURLs option using regex
hT := tTestFileOpts("fixtures/links/link_directory_internal_valid.html",
map[string]interface{}{"IgnoreURLs": []interface{}{"^/misc/js/script.js$"}})
tExpectIssueCount(t, hT, 0)
}

func TestAnchorInternalUrlNotIgnoreUsingIgnoreURLs(t *testing.T) {
// fails for internal link in when doesn't match strict regex in IgnoreURLs
hT := tTestFileOpts("fixtures/links/link_directory_internal_invalid.html",
map[string]interface{}{"IgnoreURLs": []interface{}{"^/misc/js/script.js$"}})
tExpectIssueCount(t, hT, 1)
tExpectIssue(t, hT, "target does not exist", 1)
}

func TestAnchorMultipleProblems(t *testing.T) {
// finds a mix of broken and unbroken links
t.Skip("Only single problem, and an hash which is not yet supported.")
Expand Down
10 changes: 10 additions & 0 deletions htmltest/fixtures/links/brokenLinkFile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>

<body>

<p>Blah blah blah. <a href="file://a/b/c/notreal.html">Not a real link!</a></p>
<p id="safeHash">Blah blah blah. <a href="./missingLinkHref.html">A real link!</a></p>

</body>

</html>