Skip to content

Commit

Permalink
Merge pull request #774 from gjtorikian/revert-767-feature/validate_o…
Browse files Browse the repository at this point in the history
…ptions

Revert "Validate options"
  • Loading branch information
gjtorikian authored Oct 8, 2022
2 parents c752f80 + 2abcf02 commit df573ea
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 58 deletions.
19 changes: 4 additions & 15 deletions lib/html_proofer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ def check_file(file, options = {})
raise ArgumentError unless file.is_a?(String)
raise ArgumentError, "#{file} does not exist" unless File.exist?(file)

options = prepare_options(options, :file)
options[:type] = :file
HTMLProofer::Runner.new(file, options)
end

def check_directory(directory, options = {})
raise ArgumentError unless directory.is_a?(String)
raise ArgumentError, "#{directory} does not exist" unless Dir.exist?(directory)

options = prepare_options(options, :directory)
options[:type] = :directory
HTMLProofer::Runner.new([directory], options)
end

def check_directories(directories, options = {})
raise ArgumentError unless directories.is_a?(Array)

options = prepare_options(options, :directory)
options[:type] = :directory
directories.each do |directory|
raise ArgumentError, "#{directory} does not exist" unless Dir.exist?(directory)
end
Expand All @@ -50,20 +50,9 @@ def check_directories(directories, options = {})
def check_links(links, options = {})
raise ArgumentError unless links.is_a?(Array)

options = prepare_options(options, :links)
options[:type] = :links
HTMLProofer::Runner.new(links, options)
end

private

def prepare_options(options, type)
options = {} if options.nil?

raise ArgumentError, "Options must be a Hash" unless options.is_a?(Hash)

options[:type] = type
options
end
end
end

Expand Down
20 changes: 0 additions & 20 deletions lib/html_proofer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ module Configuration

class << self
def generate_defaults(opts)
validate_options(default_options, opts)

options = PROOFER_DEFAULTS.merge(opts)

options[:typhoeus] = HTMLProofer::Configuration::TYPHOEUS_DEFAULTS.merge(opts[:typhoeus] || {})
Expand Down Expand Up @@ -88,24 +86,6 @@ def parse_json_option(option_name, config, symbolize_names: true)
raise ArgumentError, "Option '#{option_name} did not contain valid JSON."
end
end

private

def default_options
PROOFER_DEFAULTS.merge(typhoeus: TYPHOEUS_DEFAULTS).merge(hydra: HYDRA_DEFAULTS).merge(parallel: PARALLEL_DEFAULTS)
end

def validate_options(defaults, options)
defaults.each do |key, default_value|
next unless options.key?(key)

value = options[key]
raise TypeError, "Invalid value for '#{key}': '#{value}'. Expected #{default_value.class}." unless value.is_a?(default_value.class)

# Iterate over nested hashes
validate_options(default_value, value) if default_value.is_a?(Hash)
end
end
end
end
end
23 changes: 0 additions & 23 deletions spec/html-proofer/proofer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,6 @@
typhoeus: { verbose: true, headers: { "User-Agent" => "Mozilla/5.0 (compatible; My New User-Agent)" } })
expect(http["request"]["headers"]["User-Agent"]).to(eq(["Mozilla/5.0 (compatible; My New User-Agent)"]))
end

it "does not fail on nil options" do
github_hash = File.join(FIXTURES_DIR, "links", "github_hash.html")
HTMLProofer.check_file(github_hash, nil)
end

it "fails with friendly error on non-Hash options" do
links_dir = File.join(FIXTURES_DIR, "links")
expect { HTMLProofer.check_directory(links_dir, "abc") }.to(raise_error(ArgumentError, "Options must be a Hash"))
end

it "fails with friendly error on invalid option values" do
options = {
assume_extension: true,
typhoeus: "abc",
enforce_https: :yes_please,
hydra: { max_concurrency: false },
}
options.each do |key, value|
links_dir = File.join(FIXTURES_DIR, "links")
expect { HTMLProofer.check_directory(links_dir, key => value).run }.to(raise_error(TypeError, /^Invalid value for '.*': '.*'\. Expected .*\./))
end
end
end

describe "file ignores" do
Expand Down

0 comments on commit df573ea

Please sign in to comment.