-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from fatkodima/hidden_test_method-cop
Add new `Minitest/NonPublicTestMethod` cop
- Loading branch information
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
* [#220](https://github.com/rubocop/rubocop-minitest/issues/220): Add new `Minitest/NonPublicTestMethod` cop. ([@fatkodima][]) |
This file contains 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 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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# Detects non `public` (marked as `private` or `protected`) test methods. | ||
# Minitest runs only test methods which are `public`. | ||
# | ||
# @example | ||
# # bad | ||
# class FooTest | ||
# private # or protected | ||
# def test_does_something | ||
# assert_equal 42, do_something | ||
# end | ||
# end | ||
# | ||
# # good | ||
# class FooTest | ||
# def test_does_something | ||
# assert_equal 42, do_something | ||
# end | ||
# end | ||
# | ||
# # good (not a test case name) | ||
# class FooTest | ||
# private # or protected | ||
# def does_something | ||
# assert_equal 42, do_something | ||
# end | ||
# end | ||
# | ||
# # good (no assertions) | ||
# class FooTest | ||
# private # or protected | ||
# def test_does_something | ||
# do_something | ||
# end | ||
# end | ||
# | ||
class NonPublicTestMethod < Base | ||
include MinitestExplorationHelpers | ||
include DefNode | ||
|
||
MSG = 'Non `public` test method detected. Make it `public` for it to run.' | ||
|
||
def on_class(node) | ||
test_cases(node).each do |test_case| | ||
add_offense(test_case) if non_public?(test_case) && assertions(test_case).any? | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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,75 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class NonPublicTestMethodTest < Minitest::Test | ||
def test_registers_offense_when_using_private_test_method | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
private | ||
def test_does_something | ||
^^^^^^^^^^^^^^^^^^^^^^^ Non `public` test method detected. Make it `public` for it to run. | ||
assert_equal 42, do_something | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_protected_test_method | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
protected | ||
def test_does_something | ||
^^^^^^^^^^^^^^^^^^^^^^^ Non `public` test method detected. Make it `public` for it to run. | ||
assert_equal 42, do_something | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_private_test_method_outside_of_test_class | ||
assert_offense(<<~RUBY) | ||
class FooTest | ||
private | ||
def test_does_something | ||
^^^^^^^^^^^^^^^^^^^^^^^ Non `public` test method detected. Make it `public` for it to run. | ||
assert_equal 42, do_something | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_private_active_support_test_method | ||
assert_offense(<<~RUBY) | ||
class FooTest | ||
private | ||
test "does something" do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Non `public` test method detected. Make it `public` for it to run. | ||
assert_equal 42, do_something | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_private_non_test_method_with_assertions | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
private | ||
def does_something | ||
assert_equal 42, do_something | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_private_test_method_without_assertions | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
private | ||
def test_does_something | ||
do_something | ||
end | ||
end | ||
RUBY | ||
end | ||
end |