Skip to content

Commit

Permalink
Add new Minitest/HiddenTestMethod cop
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jan 17, 2023
1 parent 3f13db2 commit 3c58541
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_hidden_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/HiddenTestMethod` cop. ([@fatkodima][])
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ Minitest/GlobalExpectations:
VersionAdded: '0.7'
VersionChanged: '0.26'

Minitest/HiddenTestMethod:
Description: 'Detects hidden (marked as `private` or `protected`) test methods.'
Enabled: pending
VersionAdded: '<<next>>'

Minitest/LiteralAsActualArgument:
Description: 'This cop enforces correct order of `expected` and `actual` arguments for `assert_equal`.'
StyleGuide: 'https://minitest.rubystyle.guide/#assert-equal-arguments-order'
Expand Down
55 changes: 55 additions & 0 deletions lib/rubocop/cop/minitest/hidden_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 hidden (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 HiddenTestMethod < Base
include MinitestExplorationHelpers
include DefNode

MSG = 'Hidden 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 @@ -28,6 +28,7 @@
require_relative 'minitest/assert_truthy'
require_relative 'minitest/duplicate_test_run'
require_relative 'minitest/empty_line_before_assertion_methods'
require_relative 'minitest/hidden_test_method'
require_relative 'minitest/test_file_name'
require_relative 'minitest/global_expectations'
require_relative 'minitest/literal_as_actual_argument'
Expand Down
75 changes: 75 additions & 0 deletions test/rubocop/cop/minitest/hidden_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 HiddenTestMethodTest < Minitest::Test
def test_registers_offense_when_using_private_test_method
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
private
def test_does_something
^^^^^^^^^^^^^^^^^^^^^^^ Hidden 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
^^^^^^^^^^^^^^^^^^^^^^^ Hidden 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
^^^^^^^^^^^^^^^^^^^^^^^ Hidden 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
^^^^^^^^^^^^^^^^^^^^^^^^^ Hidden 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 3c58541

Please sign in to comment.