-
Notifications
You must be signed in to change notification settings - Fork 8
Add test coverage for custom helpers #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ | |
| require "minitest/autorun" | ||
| require "mocha/minitest" | ||
| require "linter_test_case" | ||
| require "erblint-github/linters" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
and a totally invalid name like:
can be used to disable a linter rule in a file.
I updated this line, to require the fuller linter name (not including
ERBLint::Lintersbcuz that is redundant).Our linter names are namespaced and this seems more conflict proof!