class HTMLProofer::Runner

Attributes

external_urls[R]
failures[R]
options[R]

Public Class Methods

new(src, opts = {}) click to toggle source
# File lib/html-proofer/runner.rb, line 8
def initialize(src, opts = {})
  @src = src

  @options = HTMLProofer::Configuration::PROOFER_DEFAULTS.merge(opts)

  @options[:typhoeus] = HTMLProofer::Configuration::TYPHOEUS_DEFAULTS.merge(opts[:typhoeus] || {})
  @options[:hydra] = HTMLProofer::Configuration::HYDRA_DEFAULTS.merge(opts[:hydra] || {})

  @options[:parallel] = HTMLProofer::Configuration::PARALLEL_DEFAULTS.merge(opts[:parallel] || {})
  @options[:validation] = HTMLProofer::Configuration::VALIDATION_DEFAULTS.merge(opts[:validation] || {})
  @options[:cache] = HTMLProofer::Configuration::CACHE_DEFAULTS.merge(opts[:cache] || {})

  @type = @options.delete(:type)
  @logger = HTMLProofer::Log.new(@options[:log_level])

  # Add swap patterns for internal domains
  unless @options[:internal_domains].empty?
    @options[:internal_domains].each do |dom|
      @options[:url_swap][Regexp.new("^http://#{dom}")] = ''
      @options[:url_swap][Regexp.new("^https://#{dom}")] = ''
      @options[:url_swap][Regexp.new("^//#{dom}")] = ''
    end
  end

  @failures = []
end

Public Instance Methods

check_files() click to toggle source

Collects any external URLs found in a directory of files. Also collectes every failed test from process_files. Sends the external URLs to Typhoeus for batch processing.

# File lib/html-proofer/runner.rb, line 66
def check_files
  @external_urls = {}

  process_files.each do |item|
    @external_urls.merge!(item[:external_urls])
    @failures.concat(item[:failures])
  end

  # TODO: lazy. if we're checking only external links,
  # we'll just trash all the failed tests. really, we should
  # just not run those other checks at all.
  if @options[:external_only]
    @failures = []
    validate_urls
  elsif !@options[:disable_external]
    validate_urls
  end
end
check_parsed(html, path) click to toggle source
# File lib/html-proofer/runner.rb, line 94
def check_parsed(html, path)
  result = { external_urls: {}, failures: [] }

  @src = [@src] if @type == :file

  @src.each do |src|
    checks.each do |klass|
      @logger.log :debug, "Checking #{klass.to_s.downcase} on #{path} ..."
      check = Object.const_get(klass).new(src, path, html, @logger, @options)
      check.run
      external_urls = check.external_urls
      external_urls = Hash[check.external_urls.map { |url, file| [swap(url, @options[:url_swap]), file] }] if @options[:url_swap]
      result[:external_urls].merge!(external_urls)
      result[:failures].concat(check.issues)
    end
  end
  result
end
check_path(path) click to toggle source
# File lib/html-proofer/runner.rb, line 113
def check_path(path)
  check_parsed create_nokogiri(path), path
end
checks() click to toggle source
# File lib/html-proofer/runner.rb, line 146
def checks
  return @checks if defined?(@checks) && !@checks.nil?

  return (@checks = ['LinkCheck']) if @type == :links

  @checks = HTMLProofer::Check.subchecks.map(&:name)
  @checks.delete('FaviconCheck') unless @options[:check_favicon]
  @checks.delete('HtmlCheck') unless @options[:check_html]
  @checks.delete('OpenGraphCheck') unless @options[:check_opengraph]
  @options[:checks_to_ignore].each { |ignored| @checks.delete(ignored) }
  @checks
end
failed_tests() click to toggle source
# File lib/html-proofer/runner.rb, line 159
def failed_tests
  result = []
  return result if @failures.empty?

  @failures.each { |f| result << f.to_s }
  result
end
files() click to toggle source
# File lib/html-proofer/runner.rb, line 123
def files
  @files ||= if @type == :directory
               @src.map do |src|
                 pattern = File.join(src, '**', "*#{@options[:extension]}")
                 files = Dir.glob(pattern).select { |fn| File.file? fn }
                 files.reject { |f| ignore_file?(f) }
               end.flatten
             elsif @type == :file && File.extname(@src) == @options[:extension]
               [@src].reject { |f| ignore_file?(f) }
             else
               []
             end
end
ignore_file?(file) click to toggle source
# File lib/html-proofer/runner.rb, line 137
def ignore_file?(file)
  @options[:file_ignore].each do |pattern|
    return true if pattern.is_a?(String) && pattern == file
    return true if pattern.is_a?(Regexp) && pattern =~ file
  end

  false
end
print_failed_tests() click to toggle source
process_files() click to toggle source

Walks over each implemented check and runs them on the files, in parallel.

# File lib/html-proofer/runner.rb, line 86
def process_files
  if @options[:parallel].empty?
    files.map { |path| check_path(path) }
  else
    Parallel.map(files, @options[:parallel]) { |path| check_path(path) }
  end
end
run() click to toggle source
# File lib/html-proofer/runner.rb, line 35
def run
  if @type == :links
    @logger.log :info, "Running #{checks} on #{@src}... \n\n"
    check_list_of_links unless @options[:disable_external]
  else
    @logger.log :info, "Running #{checks} on #{@src} on *#{@options[:extension]}... \n\n"
    check_files
    file_text = pluralize(files.length, 'file', 'files')
    @logger.log :info, "Ran on #{file_text}!\n\n"
  end

  if @failures.empty?
    @logger.log :info, 'HTML-Proofer finished successfully.'
  else
    print_failed_tests
  end
end
validate_urls() click to toggle source
# File lib/html-proofer/runner.rb, line 117
def validate_urls
  url_validator = HTMLProofer::UrlValidator.new(@logger, @external_urls, @options)
  @failures.concat(url_validator.run)
  @external_urls = url_validator.external_urls
end