Skip to content

Commit

Permalink
Merge pull request #540 from Graborg/fix/false-positives-when-referin…
Browse files Browse the repository at this point in the history
…g-to-file-in-root-folder

feat: enable specifying a root folder
  • Loading branch information
gjtorikian committed Nov 17, 2019
2 parents f5fa267 + e739950 commit 4a0b789
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -297,6 +297,7 @@ The `HTMLProofer` constructor takes an optional hash of additional options:
| `internal_domains`| An array of Strings containing domains that will be treated as internal urls. | `[]` |
| `log_level` | Sets the logging level, as determined by [Yell](https://github.com/rudionrails/yell). One of `:debug`, `:info`, `:warn`, `:error`, or `:fatal`. | `:info`
| `only_4xx` | Only reports errors for links that fall within the 4xx status code range. | `false` |
| `root_dir` | The absolute path to the directory serving your html-files. Used when running html-proofer on a file, rather than a directory. | "" |
| `typhoeus_config` | A JSON-formatted string. Parsed using `JSON.parse` and mapped on top of the default configuration values so that they can be overridden. | `{}` |
| `url_ignore` | An array of Strings or RegExps containing URLs that are safe to ignore. It affects all HTML attributes. Note that non-HTTP(S) URIs are always ignored. | `[]` |
| `url_swap` | A hash containing key-value pairs of `RegExp => String`. It transforms URLs that match `RegExp` into `String` via `gsub`. | `{}` |
Expand Down
1 change: 1 addition & 0 deletions bin/htmlproofer
Expand Up @@ -47,6 +47,7 @@ Mercenary.program(:htmlproofer) do |p|
p.option 'typhoeus_config', '--typhoeus-config CONFIG', String, 'JSON-formatted string of Typhoeus config. Will override the html-proofer defaults.'
p.option 'url_ignore', '--url-ignore link1,[link2,...]', Array, 'A comma-separated list of Strings or RegExps containing URLs that are safe to ignore. It affects all HTML attributes. Note that non-HTTP(S) URIs are always ignored'
p.option 'url_swap', '--url-swap re:string,[re:string,...]', Array, 'A comma-separated list containing key-value pairs of `RegExp => String`. It transforms URLs that match `RegExp` into `String` via `gsub`. The escape sequences `\\:` should be used to produce literal `:`s.'
p.option 'root_dir', '--root-folder PATH', String, 'The absolute path to the directory serving your html-files. Used when running html-proofer on a file, rather than a directory.'

p.action do |args, opts|
args = ['.'] if args.empty?
Expand Down
7 changes: 6 additions & 1 deletion lib/html-proofer/element.rb
Expand Up @@ -164,7 +164,12 @@ def file_path
end

if path =~ %r{^/} # path relative to root
base = File.directory?(@check.src) ? @check.src : File.dirname(@check.src)
if File.directory?(@check.src)
base = @check.src
else
root_dir = @check.options[:root_dir]
base = root_dir ? root_dir : File.dirname(@check.src)
end
elsif File.exist?(File.expand_path(path, @check.src)) || File.exist?(File.expand_path(path_dot_ext, @check.src)) # relative links, path is a file
base = File.dirname @check.path
elsif File.exist?(File.join(File.dirname(@check.path), path)) || File.exist?(File.join(File.dirname(@check.path), path_dot_ext)) # relative links in nested dir, path is a file
Expand Down
10 changes: 10 additions & 0 deletions spec/html-proofer/fixtures/links/working_root_link_internal.html
@@ -0,0 +1,10 @@
<html>

<body>
<p>
Blah blah blah.
<a href="/html/html5_tags.html">A real link!</a>
</p>
</body>

</html>
6 changes: 6 additions & 0 deletions spec/html-proofer/links_spec.rb
Expand Up @@ -79,6 +79,12 @@
expect(proofer.failed_tests.first).to match(%r{internally linking to .\/notreal.html, which does not exist})
end

it 'succeeds for working internal-root-links pointing to other folder' do
broken_root_link_internal_filepath = "#{FIXTURES_DIR}/links/working_root_link_internal.html"
proofer = run_proofer(broken_root_link_internal_filepath, :file, root_dir: 'spec/html-proofer/fixtures')
expect(proofer.failed_tests).to eq []
end

it 'fails for link with no href' do
missing_link_href_filepath = "#{FIXTURES_DIR}/links/missing_link_href.html"
proofer = run_proofer(missing_link_href_filepath, :file)
Expand Down

0 comments on commit 4a0b789

Please sign in to comment.