Skip to content

Commit

Permalink
[Fix #301] Add new Minitest/NoFocus cop
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredmoody committed Feb 14, 2024
1 parent eb196db commit 11abb1e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_add_new_minitest_no_focus_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#301](https://github.com/rubocop/rubocop-minitest/issues/301): Add new Minitest/NoFocus cop. ([@jaredmoody][])
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ Minitest/NoAssertions:
Enabled: false
VersionAdded: '0.12'

Minitest/NoFocus:
Description: 'Checks for focused tests.'
Enabled: pending
VersionAdded: '<<next>>'

Minitest/NoTestCases:
Description: 'Checks if test class contains any test cases.'
Enabled: false
Expand Down
34 changes: 34 additions & 0 deletions lib/rubocop/cop/minitest/no_focus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Minitest
# Enforces tests are not focused
#
# @example
# # bad
# focus
# test 'foo' do
# end
#
# # good
# test 'foo' do
# end
#
class NoFocus < Base
MSG = 'Remove `focus` from tests.'
RESTRICT_ON_SEND = [:focus].freeze

def_node_matcher :focused?, <<~PATTERN
(send nil? :focus ...)
PATTERN

def on_send(node)
return unless focused?(node)

add_offense node
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 @@ -30,6 +30,7 @@
require_relative 'minitest/assert_truthy'
require_relative 'minitest/duplicate_test_run'
require_relative 'minitest/empty_line_before_assertion_methods'
require_relative 'minitest/no_focus'
require_relative 'minitest/non_executable_test_method'
require_relative 'minitest/redundant_message_argument'
require_relative 'minitest/return_in_test_method'
Expand Down
35 changes: 35 additions & 0 deletions test/rubocop/cop/minitest/no_focus_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require_relative '../../../test_helper'

class NoFocusTest < Minitest::Test
def test_registers_offense_when_using_direct_focus
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
focus test 'foo' do
^^^^^^^^^^^^^^^^ Remove `focus` from tests.
end
end
RUBY
end

def test_registers_offense_when_using_indirect_focus
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
focus
^^^^^ Remove `focus` from tests.
test 'foo' do
end
end
RUBY
end

def test_does_not_register_offense_when_no_focus
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
test 'foo' do
end
end
RUBY
end
end

0 comments on commit 11abb1e

Please sign in to comment.