Skip to content

Commit

Permalink
[Fix rubocop#301] Add new Minitest/Focus cop
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredmoody committed Feb 16, 2024
1 parent eb196db commit 05e5605
Show file tree
Hide file tree
Showing 5 changed files with 105 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/Focus cop. ([@jaredmoody][])
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ Minitest/EmptyLineBeforeAssertionMethods:
Enabled: pending
VersionAdded: '0.23'

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

Minitest/GlobalExpectations:
Description: 'This cop checks for deprecated global expectations.'
StyleGuide: 'https://minitest.rubystyle.guide#global-expectations'
Expand Down
49 changes: 49 additions & 0 deletions lib/rubocop/cop/minitest/focus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Minitest
# Enforces tests are not focused
#
# @example
# # bad
# focus test 'foo' do
# end
#
# # bad
# focus
# test 'foo' do
# end
#
# # good
# test 'foo' do
# end
#
class Focus < Base
extend AutoCorrector
include RangeHelp

MSG = 'Remove `focus` from tests.'
RESTRICT_ON_SEND = [:focus].freeze

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

def on_send(node)
return if node.receiver

add_offense(node.loc.selector) do |corrector|
range = if node.arguments.none?
range_by_whole_lines(node.source_range, include_final_newline: true)
else
node.loc.selector.join(node.first_argument.source_range.begin)
end

corrector.remove(range)
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 @@ -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/focus'
require_relative 'minitest/non_executable_test_method'
require_relative 'minitest/redundant_message_argument'
require_relative 'minitest/return_in_test_method'
Expand Down
49 changes: 49 additions & 0 deletions test/rubocop/cop/minitest/focus_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

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

class FocusTest < 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

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
test 'foo' do
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

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
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 05e5605

Please sign in to comment.