Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #301] Add new Minitest/Focus cop #302

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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