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

new cli opts #676

Merged
merged 1 commit into from
Dec 31, 2021
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,6 @@ The `HTMLProofer` constructor takes an optional hash of additional options:
| `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. | "" |
| `swap_urls` | A hash containing key-value pairs of `RegExp => String`. It transforms URLs that match `RegExp` into `String` via `gsub`. | `{}` |
| `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. | `{}` |


In addition, there are a few "namespaced" options. These are:

Expand Down Expand Up @@ -337,6 +335,8 @@ The default value is:
}
```

On the CLI, you can provide the `--typhoeus` or `hydra` arguments. These are parsed using `JSON.parse` and mapped on top of the default configuration values so that they can be overridden.

#### Setting `before-request` callback

You can provide a block to set some logic before an external link is checked. For example, say you want to provide an authentication token every time a GitHub URL is checked. You can do that like this:
Expand All @@ -363,6 +363,8 @@ In this example, `in_processes: 3` is passed into Parallel as a configuration op

Pass in `:parallel => { enable: false }` to disable parallel runs.

On the CLI, you can provide the `--parallel` argument. This is parsed using `JSON.parse` and mapped on top of the default configuration values so that they can be overridden.

## Configuring caching

Checking external URLs can slow your tests down. If you'd like to speed that up, you can enable caching for your external links. Caching simply means to skip links that are valid for a certain period of time.
Expand Down Expand Up @@ -398,6 +400,8 @@ The cache operates on external links only.

If caching is enabled, HTMLProofer writes to a log file called *tmp/.htmlproofer/cache.log*. You should probably ignore this folder in your version control system.

On the CLI, you can provide the `--timeframe` or `--storage-dir` arguments to control the cache behavior.

### Caching with continuous integration

Enable caching in your continuous integration process. It will make your builds faster.
Expand Down
15 changes: 9 additions & 6 deletions bin/htmlproofer
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ Mercenary.program(:htmlproofer) do |p|
p.option 'ignore_urls', '--ignore-urls link1,[link2,...]', Array, 'A comma-separated list of Strings or RegExps containing URLs that are safe to ignore. This affects all HTML attributes, such as `alt` tags on images.'
p.option 'log_level', '--log-level <level>', String, 'Sets the logging level, as determined by Yell. One of `:debug`, `:info`, `:warn`, `:error`, or `:fatal`. (default: `:info`)'
p.option 'only_4xx', '--only-4xx', 'Only reports errors for links that fall within the 4xx status code range'
p.option 'root_dir', '--root-dir PATH', String, 'The absolute path to the directory serving your html-files.'
p.option 'storage_dir', '--storage-dir PATH', String, 'Directory where to store the cache log (default: "tmp/.htmlproofer")'
p.option 'timeframe', '--timeframe <time>', String, 'A string representing the caching timeframe.'
p.option 'typhoeus_config', '--typhoeus-config CONFIG', String, 'JSON-formatted string of Typhoeus config. Will override the html-proofer defaults.'
p.option 'hydra_config', '--hydra-config CONFIG', String, 'JSON-formatted string of Hydra config. Will override the html-proofer defaults.'
p.option 'swap_urls', '--swap-urls 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-dir PATH', String, 'The absolute path to the directory serving your html-files.'
p.option 'timeframe', '--timeframe <time>', String, 'A string representing the caching timeframe.'

p.option 'typhoeus', '--typhoeus CONFIG', String, 'JSON-formatted string of Typhoeus config. Will override the html-proofer defaults.'
p.option 'hydra', '--hydra CONFIG', String, 'JSON-formatted string of Hydra config. Will override the html-proofer defaults.'
p.option 'parallel', '--parallel CONFIG', String, 'JSON-formatted string of Parallel config. Will override the html-proofer defaults.'

p.action do |args, opts|
args = ['.'] if args.empty?
Expand Down Expand Up @@ -66,8 +68,9 @@ Mercenary.program(:htmlproofer) do |p|

options[:log_level] = opts['log_level'].to_sym unless opts['log_level'].nil?

options[:typhoeus] = HTMLProofer::Configuration.parse_json_option('typhoeus_config', opts['typhoeus_config'], symbolize_names: false) unless opts['typhoeus_config'].nil?
options[:hydra] = HTMLProofer::Configuration.parse_json_option('hydra_config', opts['hydra_config']) unless opts['hydra_config'].nil?
options[:typhoeus] = HTMLProofer::Configuration.parse_json_option('typhoeus', opts['typhoeus'], symbolize_names: false) unless opts['typhoeus'].nil?
options[:hydra] = HTMLProofer::Configuration.parse_json_option('hydra', opts['hydra']) unless opts['hydra'].nil?
options[:parallel] = HTMLProofer::Configuration.parse_json_option('parallel', opts['parallel']) unless opts['parallel'].nil?

unless opts['timeframe'].nil?
options[:cache] ||= {}
Expand Down
30 changes: 19 additions & 11 deletions spec/html-proofer/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@
expect(output).to match('successfully')
end

it 'has only one UA' do
http = make_bin(%|--typhoeus-config='{"verbose":true,"headers":{"User-Agent":"Mozilla/5.0 (Macintosh; My New User-Agent)"}}' --as-links https://linkedin.com|)
expect(http.scan(/User-Agent: Typhoeus/).count).to eq 0
expect(http.scan(%r{User-Agent: Mozilla/5.0 \(Macintosh; My New User-Agent\)}i).count).to eq 2
end

it 'accepts hydra max_concurrency' do
http = make_bin(%(--hydra-config '{"max_concurrency": 5}' http://www.github.com --as-links))
expect(http.scan(/max_concurrency is invalid/).count).to eq 0
end

it 'navigates above itself in a subdirectory' do
real_link = File.join(FIXTURES_DIR, 'links', 'root_folder/documentation-from-my-project/')
output = make_bin("--root-dir #{File.join(FIXTURES_DIR, 'links', 'root_folder/')} #{real_link}")
Expand All @@ -108,6 +97,25 @@
it 'has every option for proofer defaults' do
match_command_help(HTMLProofer::Configuration::PROOFER_DEFAULTS)
end

context 'nested options' do
it 'supports typhoeus' do
link_with_redirect_filepath = File.join(FIXTURES_DIR, 'links', 'link_with_redirect.html')
output = make_bin("#{link_with_redirect_filepath} --typhoeus '{ \"followlocation\": false }'")
expect(output).to match(/failed: 301/)
end

it 'has only one UA' do
http = make_bin(%|--typhoeus='{"verbose":true,"headers":{"User-Agent":"Mozilla/5.0 (Macintosh; My New User-Agent)"}}' --as-links https://linkedin.com|)
expect(http.scan(/User-Agent: Typhoeus/).count).to eq 0
expect(http.scan(%r{User-Agent: Mozilla/5.0 \(Macintosh; My New User-Agent\)}i).count).to eq 2
end

it 'supports hydra' do
http = make_bin(%(--hydra '{"max_concurrency": 5}' http://www.github.com --as-links))
expect(http.scan(/max_concurrency is invalid/).count).to eq 0
end
end
end

def match_command_help(config)
Expand Down
2 changes: 1 addition & 1 deletion spec/html-proofer/fixtures/cache/.runner.log
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{"/somewhere.html":{"time":"2021-12-31 12:17:06 -0500","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","current_path":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://www.github.com":{"time":"2021-12-31 12:17:07 -0500","status":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/links/_site","line":4}]}}}
{"version":2,"internal":{},"external":{"https://www.github.com":{"time":"2021-12-31 14:52:56 -0500","status":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/links/_site","line":4}]}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{"/somewhere.html":{"time":"2021-12-31 12:18:59 -0500","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","current_path":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2021-12-31 12:18:59 -0500","status":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":7}]}}}
{"version":2,"internal":{"/somewhere.html":{"time":"2021-12-31 14:52:56 -0500","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","current_path":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2021-12-31 14:52:56 -0500","status":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":7}]}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{"/somewhere.html":{"time":"2021-12-31 12:18:59 -0500","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","current_path":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2021-12-31 12:18:59 -0500","status":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/external_example.html","line":7}]}}}
{"version":2,"internal":{"/somewhere.html":{"time":"2021-12-31 14:52:56 -0500","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","current_path":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2021-12-31 14:52:56 -0500","status":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":7}]}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{"/somewhere.html":{"time":"2021-12-31 12:18:59 -0500","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","current_path":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://github.com/gjtorikian":{"time":"2021-12-31 12:18:59 -0500","status":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/some_link.html","line":7}]}}}
{"version":2,"internal":{"/somewhere.html":{"time":"2021-12-31 14:52:56 -0500","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","current_path":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://github.com/gjtorikian":{"time":"2021-12-31 14:52:57 -0500","status":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/some_link.html","line":7}]}}}

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