Skip to content

Commit

Permalink
Merge pull request #594 from gjtorikian/root-folder-update
Browse files Browse the repository at this point in the history
Let `root_dir` work when running on directories
  • Loading branch information
gjtorikian authored Nov 22, 2020
2 parents 5badc4e + a016f01 commit e5d4fb5
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,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. | "" |
| `root_dir` | The absolute path to the directory serving your html-files. | "" |
| `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
2 changes: 1 addition & 1 deletion bin/htmlproofer
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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.option 'root_dir', '--root-dir PATH', String, 'The absolute path to the directory serving your html-files.'

p.action do |args, opts|
args = ['.'] if args.empty?
Expand Down
37 changes: 18 additions & 19 deletions lib/html-proofer/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,27 @@ def param_link
url.start_with?('?')
end

def absolute_path?(path)
path.start_with?('/')
end

def file_path
return if path.nil? || path.empty?

path_dot_ext = ''

path_dot_ext = path + @check.options[:extension] if @check.options[:assume_extension]

if %r{^/}.match?(path) # path relative to root
if File.directory?(@check.src)
base = @check.src
else
root_dir = @check.options[:root_dir]
base = 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)) # rubocop:disable Lint/DuplicateBranch; relative links in nested dir, path is a file
base = File.dirname @check.path
else # relative link, path is a directory
base = @check.path
end

file = File.join base, path
base = if absolute_path?(path) # path relative to root
@check.options[:root_dir] || File.dirname(@check.src)
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
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)) # rubocop:disable Lint/DuplicateBranch; relative links in nested dir, path is a file
File.dirname(@check.path)
else # relative link, path is a directory
@check.path
end
file = File.join(base, path)
if @check.options[:assume_extension] && File.file?("#{file}#{@check.options[:extension]}")
file = "#{file}#{@check.options[:extension]}"
elsif File.directory?(file) && !unslashed_directory?(file) # implicit index support
Expand All @@ -203,14 +201,15 @@ def file_path

# checks if a file exists relative to the current pwd
def exists?
return @checked_paths[absolute_path] if @checked_paths.key? absolute_path
return @checked_paths[absolute_path] if @checked_paths.key?(absolute_path)

@checked_paths[absolute_path] = File.exist? absolute_path
@checked_paths[absolute_path] = File.exist?(absolute_path)
end

def absolute_path
path = file_path || @check.path
File.expand_path path, Dir.pwd

File.expand_path(path, Dir.pwd)
end

def ignores_pattern_check(links)
Expand Down
2 changes: 1 addition & 1 deletion lib/html-proofer/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def print_failed_tests
# @return [ Array<Block> ] All before_request blocks.
def before_request(&block)
@before_request ||= []
@before_request << block if block_given?
@before_request << block if block
@before_request
end

Expand Down
6 changes: 6 additions & 0 deletions spec/html-proofer/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
expect(output).to match('successfully')
end

it 'navigates above itself in a subdirectory' do
real_link = "#{FIXTURES_DIR}/links/root_folder/documentation-from-my-project/"
output = make_bin("--root-dir #{FIXTURES_DIR}/links/root_folder/", real_link)
expect(output).to match('successfully')
end

it 'has every option for proofer defaults' do
match_command_help(HTMLProofer::Configuration::PROOFER_DEFAULTS)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="/other-project/a.html"/>
Empty file.

0 comments on commit e5d4fb5

Please sign in to comment.