Skip to content

Commit

Permalink
Merge pull request #229 from fatkodima/hidden_test_method-cop
Browse files Browse the repository at this point in the history
Add new `Minitest/NonPublicTestMethod` cop
  • Loading branch information
koic authored Jan 22, 2023
2 parents d0ce98e + 80fd3da commit 94238e3
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_non_public_test_method_cop.md
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][])
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ Minitest/NoAssertions:
Enabled: false
VersionAdded: '0.12'

Minitest/NonPublicTestMethod:
Description: 'Detects non `public` (marked as `private` or `protected`) test methods.'
Enabled: pending
Severity: warning
VersionAdded: '<<next>>'

Minitest/RefuteEmpty:
Description: 'This cop enforces to use `refute_empty` instead of using `refute(object.empty?)`.'
StyleGuide: 'https://minitest.rubystyle.guide#refute-empty'
Expand Down
55 changes: 55 additions & 0 deletions lib/rubocop/cop/minitest/non_public_test_method.rb
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
1 change: 1 addition & 0 deletions lib/rubocop/cop/minitest_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
require_relative 'minitest/literal_as_actual_argument'
require_relative 'minitest/multiple_assertions'
require_relative 'minitest/no_assertions'
require_relative 'minitest/non_public_test_method'
require_relative 'minitest/refute_empty'
require_relative 'minitest/refute_false'
require_relative 'minitest/refute_equal'
Expand Down
75 changes: 75 additions & 0 deletions test/rubocop/cop/minitest/non_public_test_method_test.rb
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

0 comments on commit 94238e3

Please sign in to comment.