Skip to content
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
2 changes: 1 addition & 1 deletion lib/erblint-github/linters/custom_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def rule_disabled?(processed_source)
indicator_node, _, code_node, = *node
indicator = indicator_node&.loc&.source
comment = code_node&.loc&.source&.strip
rule_name = self.class.name.match(/:?:?(\w+)\Z/)[1]
rule_name = self.class.name.gsub("ERBLint::Linters::", "")

if indicator == "#" && comment.start_with?("erblint:disable") && comment.match(rule_name)
if @offenses.any?
Expand Down
97 changes: 97 additions & 0 deletions test/custom_helpers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# frozen_string_literal: true

require "test_helper"
require "erblint-github/linters/custom_helpers"

class CustomHelpersTest < LinterTestCase
include ERBLint::Linters::CustomHelpers

class FakeLinter < ERBLint::Linter
attr_accessor :offenses

MESSAGE = "Please fix your code."
end

def linter_class
CustomHelpersTest::FakeLinter
end

def extended_linter
@linter.extend(ERBLint::Linters::CustomHelpers)
end

def test_rule_disabled_clears_offenses_if_rule_is_disabled
@file = <<~HTML
<%# erblint:disable CustomHelpersTest::FakeLinter %>
HTML
@linter.offenses = ["fake offense"]
assert_equal @linter.offenses.length, 1

extended_linter.rule_disabled?(processed_source)
assert_empty @linter.offenses
end

def test_rule_disabled_adds_offense_if_disable_comment_is_present_but_no_offense
@file = <<~HTML
<%# erblint:disable CustomHelpersTest::FakeLinter %>
HTML
assert_empty @linter.offenses

extended_linter.rule_disabled?(processed_source)

assert_equal @linter.offenses.length, 1
end

def test_generate_offense_with_message_defined_in_linter_class
@file = <<~HTML
<%# erblint:disable CustomHelpersTest::FakeLinter %>
<div id="fake-div-id">Hello.</div>
HTML
assert_empty @linter.offenses

tag = extended_linter.tags(processed_source).first
extended_linter.generate_offense(CustomHelpersTest::FakeLinter, processed_source, tag)

assert_equal @linter.offenses.length, 1
assert_match CustomHelpersTest::FakeLinter::MESSAGE, @linter.offenses.first.message
end

def test_generate_offense_with_message_passed_in_as_parameter
@file = <<~HTML
<div id="fake-div-id">Hello.</div>
HTML
assert_empty @linter.offenses

tag = extended_linter.tags(processed_source).first
extended_linter.generate_offense(CustomHelpersTest::FakeLinter, processed_source, tag, "bad!")

assert_equal @linter.offenses.length, 1
assert_match(/bad!/, @linter.offenses.first.message)
end

def test_possible_attribute_values_returns_attribute_value_matches
@file = <<~HTML
<img alt="mona lisa">
<button type="button"></button>
<a></a>
HTML

image_tag = extended_linter.tags(processed_source).first
image_tag_alt_values = extended_linter.possible_attribute_values(image_tag, "alt")
assert_equal image_tag_alt_values, ["mona lisa"]

button_tag = extended_linter.tags(processed_source)[1]
button_tag_type_values = extended_linter.possible_attribute_values(button_tag, "type")
assert_equal button_tag_type_values, ["button"]
end

def test_possible_attribute_values_returns_empty_array_if_no_attribute_value_matches
@file = <<~HTML
<a></a>
HTML

anchor_tag = extended_linter.tags(processed_source).first
possible_attribute_values = extended_linter.possible_attribute_values(anchor_tag, "href")
assert_equal possible_attribute_values, []
end
end
11 changes: 10 additions & 1 deletion test/linters/accessibility/no_redundant_image_alt_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "test_helper"
require "erblint-github/linters/github/accessibility/no_redundant_image_alt"

class NoRedundantImageAltTest < LinterTestCase
def linter_class
Expand All @@ -28,4 +27,14 @@ def test_does_not_warn_if_alt_contains_no_redundant_text

assert_empty @linter.offenses
end

def test_does_not_warn_if_linter_is_disabled_in_file
@file = <<~HTML
<%# erblint:disable GitHub::Accessibility::NoRedundantImageAlt %>
<img alt='image of an octopus'></img>
HTML
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our linters have logic that allows a linter to be disabled/ignored in a file. This comes from: rule_disabled?

Right now, we only check the demodulized name so:

<%# erblint:disable NoRedundantImageAlt %>

and a totally invalid name like:

<%# erblint:disable BadModule::NoRedundantImageAlt %>

can be used to disable a linter rule in a file.

I updated this line, to require the fuller linter name (not including ERBLint::Linters bcuz that is redundant).

Our linter names are namespaced and this seems more conflict proof!


@linter.run(processed_source)
assert_empty @linter.offenses
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
require "minitest/autorun"
require "mocha/minitest"
require "linter_test_case"
require "erblint-github/linters"