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

Accept regexp values for ignore-files via CLI #776

Merged
merged 3 commits into from Nov 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
@@ -1,4 +1,4 @@
name: Linting
name: Lint

on:
pull_request:
Expand Down
18 changes: 3 additions & 15 deletions .github/workflows/ci.yml → .github/workflows/test.yml
@@ -1,4 +1,4 @@
name: Ruby CI
name: Test

on:
pull_request:
Expand All @@ -15,26 +15,14 @@ jobs:
NOKOGIRI_USE_SYSTEM_LIBRARIES: true
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
ruby-version:
- 3.1
experimental: [false]
include:
- ruby-version: head
experimental: true

continue-on-error: ${{ matrix.experimental }}

steps:
- uses: actions/checkout@v3
- name: Install libxslt and libxml2
run: sudo apt-get install libxslt-dev libxml2-dev
- name: Set up Ruby ${{ matrix.ruby-version }}
- name: Set up Ruby 3.1
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
ruby-version: 3.1
bundler-cache: true
- name: Install dependencies
run: bundle install
Expand Down
14 changes: 12 additions & 2 deletions lib/html_proofer/configuration.rb
Expand Up @@ -134,7 +134,17 @@ def parse_cli_options(args)

section(opts, "Ignore Configuration") do
set_option(opts, "--ignore-files [FILE1,FILE2,...]") do |long_opt_symbol, list|
@options[long_opt_symbol] = list.nil? ? [] : list.split(",")
@options[long_opt_symbol] = if list.nil?
[]
else
list.split(",").map.each do |l|
if l.start_with?("/") && l.end_with?("/")
Regexp.new(l[1...-1])
else
l
end
end
end
end

set_option(opts, "--[no-]ignore-empty-alt") do |long_opt_symbol, arg|
Expand Down Expand Up @@ -285,7 +295,7 @@ module ConfigurationHelp
check_internal_hash: ["Checks whether internal hashes exist (even if the webpage exists) (default: `true`)."],
check_sri: ["Check that `<link>` and `<script>` external resources use SRI (default: `false`)."],
disable_external: ["If `true`, does not run the external link checker (default: `false`)."],
enforce_https: ["Fails a link if it\'s not marked as `https` (default: `true`)."],
enforce_https: ["Fails a link if it's not marked as `https` (default: `true`)."],
root_dir: ["The absolute path to the directory serving your html-files."],

ignore_empty_alt: ["If `true`, ignores images with empty/missing ",
Expand Down
10 changes: 8 additions & 2 deletions lib/html_proofer/runner.rb
Expand Up @@ -166,8 +166,14 @@ def files

def ignore_file?(file)
@options[:ignore_files].each do |pattern|
return true if pattern.is_a?(String) && pattern == file
return true if pattern.is_a?(Regexp) && pattern =~ file
if pattern.is_a?(String) && pattern == file
@logger.log(:debug, "Ignoring #{file} because it matches #{pattern}")
return true
end
next unless pattern.is_a?(Regexp) && pattern.match(file)

@logger.log(:debug, "Ignoring #{file} because it matches regexp #{pattern}")
return true
end

false
Expand Down
10 changes: 8 additions & 2 deletions spec/html-proofer/command_spec.rb
Expand Up @@ -134,12 +134,18 @@
expect(output).to(match("failure"))
end

it "works with ignore-files" do
it "works with ignore-files (string)" do
external = File.join(FIXTURES_DIR, "links", "broken_hash_internal.html")
output = make_bin("--ignore-files #{external} #{external}")
expect(output).to(match("successfully"))
end

it "works with ignore-files (regexp)" do
external = File.join(FIXTURES_DIR, "links", "broken_hash_internal.html")
output = make_bin("--ignore-files /_hash_/ #{external}")
expect(output).to(match("successfully"))
end

it "works with ignore-missing-alt" do
broken = File.join(FIXTURES_DIR, "images", "missing_image_alt.html")
output = make_bin("--ignore-missing-alt #{broken}")
Expand All @@ -160,7 +166,7 @@

it "works with ignore-urls" do
ignorable_links = File.join(FIXTURES_DIR, "links", "ignorable_links_via_options.html")
output = make_bin("--ignore-urls /^http:\/\//,/sdadsad/,../whaadadt.html #{ignorable_links}")
output = make_bin("--ignore-urls /^http:///,/sdadsad/,../whaadadt.html #{ignorable_links}")
expect(output).to(match("successfully"))
end

Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -70,6 +70,7 @@ def capture_proofer_http(item, type, opts = {})
end

def make_bin(args)
ap("#{RbConfig.ruby} exe/htmlproofer #{args}") if debug?
stdout, stderr = Open3.capture3("#{RbConfig.ruby} exe/htmlproofer #{args}")
"#{stdout}\n#{stderr}".encode("UTF-8", invalid: :replace, undef: :replace)
end
Expand All @@ -84,6 +85,10 @@ def make_cassette_name(file, opts)
filename
end

def debug?
ENV.fetch("DEBUG", false)
end

def ci?
ENV.fetch("CI", false)
end
Expand Down