diff options
Diffstat (limited to 'rubocop/cop/rspec')
-rw-r--r-- | rubocop/cop/rspec/any_instance_of.rb | 29 | ||||
-rw-r--r-- | rubocop/cop/rspec/be_success_matcher.rb | 10 | ||||
-rw-r--r-- | rubocop/cop/rspec/env_assignment.rb | 10 | ||||
-rw-r--r-- | rubocop/cop/rspec/expect_gitlab_tracking.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/factories_in_migration_specs.rb | 4 | ||||
-rw-r--r-- | rubocop/cop/rspec/factory_bot/inline_association.rb | 10 | ||||
-rw-r--r-- | rubocop/cop/rspec/have_gitlab_http_status.rb | 22 | ||||
-rw-r--r-- | rubocop/cop/rspec/httparty_basic_auth.rb | 12 | ||||
-rw-r--r-- | rubocop/cop/rspec/modify_sidekiq_middleware.rb | 10 | ||||
-rw-r--r-- | rubocop/cop/rspec/timecop_freeze.rb | 10 | ||||
-rw-r--r-- | rubocop/cop/rspec/timecop_travel.rb | 10 | ||||
-rw-r--r-- | rubocop/cop/rspec/web_mock_enable.rb | 14 |
12 files changed, 64 insertions, 79 deletions
diff --git a/rubocop/cop/rspec/any_instance_of.rb b/rubocop/cop/rspec/any_instance_of.rb index a939af36c13..e1cacfebfd3 100644 --- a/rubocop/cop/rspec/any_instance_of.rb +++ b/rubocop/cop/rspec/any_instance_of.rb @@ -24,7 +24,9 @@ module RuboCop # expect(instance).to receive(:invalidate_issue_cache_counts) # end # - class AnyInstanceOf < RuboCop::Cop::Cop + class AnyInstanceOf < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MESSAGE_EXPECT = 'Do not use `expect_any_instance_of` method, use `expect_next_instance_of` instead.' MESSAGE_ALLOW = 'Do not use `allow_any_instance_of` method, use `allow_next_instance_of` instead.' @@ -37,22 +39,19 @@ module RuboCop def on_send(node) if expect_any_instance_of?(node) - add_offense(node, location: :expression, message: MESSAGE_EXPECT) + add_offense(node, message: MESSAGE_EXPECT) do |corrector| + corrector.replace( + node.loc.expression, + replacement_any_instance_of(node, 'expect') + ) + end elsif allow_any_instance_of?(node) - add_offense(node, location: :expression, message: MESSAGE_ALLOW) - end - end - - def autocorrect(node) - replacement = - if expect_any_instance_of?(node) - replacement_any_instance_of(node, 'expect') - elsif allow_any_instance_of?(node) - replacement_any_instance_of(node, 'allow') + add_offense(node, message: MESSAGE_ALLOW) do |corrector| + corrector.replace( + node.loc.expression, + replacement_any_instance_of(node, 'allow') + ) end - - lambda do |corrector| - corrector.replace(node.loc.expression, replacement) end end diff --git a/rubocop/cop/rspec/be_success_matcher.rb b/rubocop/cop/rspec/be_success_matcher.rb index dce9604b3d7..5a011845075 100644 --- a/rubocop/cop/rspec/be_success_matcher.rb +++ b/rubocop/cop/rspec/be_success_matcher.rb @@ -21,7 +21,9 @@ module RuboCop # # it { is_expected.to be_successful } # - class BeSuccessMatcher < RuboCop::Cop::Cop + class BeSuccessMatcher < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MESSAGE = 'Do not use deprecated `success?` method, use `successful?` instead.' def_node_search :expect_to_be_success?, <<~PATTERN @@ -39,11 +41,7 @@ module RuboCop def on_send(node) return unless be_success_usage?(node) - add_offense(node, location: :expression, message: MESSAGE) - end - - def autocorrect(node) - lambda do |corrector| + add_offense(node, message: MESSAGE) do |corrector| corrector.insert_after(node.loc.expression, 'ful') end end diff --git a/rubocop/cop/rspec/env_assignment.rb b/rubocop/cop/rspec/env_assignment.rb index e3075e7bd90..add7897c624 100644 --- a/rubocop/cop/rspec/env_assignment.rb +++ b/rubocop/cop/rspec/env_assignment.rb @@ -16,7 +16,9 @@ module RuboCop # before do # stub_env('FOO', 'bar') # end - class EnvAssignment < RuboCop::Cop::Cop + class EnvAssignment < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MESSAGE = "Don't assign to ENV, use `stub_env` instead." def_node_search :env_assignment?, <<~PATTERN @@ -28,11 +30,7 @@ module RuboCop def on_send(node) return unless env_assignment?(node) - add_offense(node, location: :expression, message: MESSAGE) - end - - def autocorrect(node) - lambda do |corrector| + add_offense(node, message: MESSAGE) do |corrector| corrector.replace(node.loc.expression, stub_env(env_key(node), env_value(node))) end end diff --git a/rubocop/cop/rspec/expect_gitlab_tracking.rb b/rubocop/cop/rspec/expect_gitlab_tracking.rb index e3f790f851c..4f92980baa4 100644 --- a/rubocop/cop/rspec/expect_gitlab_tracking.rb +++ b/rubocop/cop/rspec/expect_gitlab_tracking.rb @@ -29,7 +29,7 @@ module RuboCop # it 'does not expect a snowplow event', :snowplow do # expect_no_snowplow_event # end - class ExpectGitlabTracking < RuboCop::Cop::Cop + class ExpectGitlabTracking < RuboCop::Cop::Base MSG = 'Do not expect directly on `Gitlab::Tracking#event`, add the `snowplow` annotation and use ' \ '`expect_snowplow_event` instead. ' \ 'See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#test-snowplow-events' diff --git a/rubocop/cop/rspec/factories_in_migration_specs.rb b/rubocop/cop/rspec/factories_in_migration_specs.rb index f29bbf68cdc..6dde3d4524c 100644 --- a/rubocop/cop/rspec/factories_in_migration_specs.rb +++ b/rubocop/cop/rspec/factories_in_migration_specs.rb @@ -13,7 +13,7 @@ module RuboCop # # good # let(:users) { table(:users) } # let(:user) { users.create!(name: 'User 1', username: 'user1') } - class FactoriesInMigrationSpecs < RuboCop::Cop::Cop + class FactoriesInMigrationSpecs < RuboCop::Cop::Base MESSAGE = "Don't use FactoryBot.%s in migration specs, use `table` instead." FORBIDDEN_METHODS = %i[build build_list create create_list attributes_for].freeze @@ -29,7 +29,7 @@ module RuboCop method = node.children[1] - add_offense(node, location: :expression, message: MESSAGE % method) + add_offense(node, message: MESSAGE % method) end end end diff --git a/rubocop/cop/rspec/factory_bot/inline_association.rb b/rubocop/cop/rspec/factory_bot/inline_association.rb index 1c2b8b55b46..ccc6364fb73 100644 --- a/rubocop/cop/rspec/factory_bot/inline_association.rb +++ b/rubocop/cop/rspec/factory_bot/inline_association.rb @@ -43,7 +43,9 @@ module RuboCop # # creator_id { create(:user).id } # - class InlineAssociation < RuboCop::Cop::Cop + class InlineAssociation < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MSG = 'Prefer inline `association` over `%{type}`. ' \ 'See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories' @@ -81,11 +83,7 @@ module RuboCop return if chained_call?(node.parent) return unless inside_assocation_definition?(node) - add_offense(node, message: format(MSG, type: type)) - end - - def autocorrect(node) - lambda do |corrector| + add_offense(node, message: format(MSG, type: type)) do |corrector| receiver, type = create_or_build(node) receiver = "#{receiver.source}." if receiver expression = "#{receiver}#{type}" diff --git a/rubocop/cop/rspec/have_gitlab_http_status.rb b/rubocop/cop/rspec/have_gitlab_http_status.rb index d61fb9f2368..86ece72b4f5 100644 --- a/rubocop/cop/rspec/have_gitlab_http_status.rb +++ b/rubocop/cop/rspec/have_gitlab_http_status.rb @@ -22,7 +22,9 @@ module RuboCop # expect(response).to have_gitlab_http_status(:ok) # expect(response).not_to have_gitlab_http_status(:ok) # - class HaveGitlabHttpStatus < RuboCop::Cop::Cop + class HaveGitlabHttpStatus < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + CODE_TO_SYMBOL = Rack::Utils::SYMBOL_TO_STATUS_CODE.invert MSG_MATCHER_NAME = @@ -66,13 +68,6 @@ module RuboCop offense_for_matcher(node) || offense_for_response_status(node) end - def autocorrect(node) - lambda do |corrector| - replacement = replace_matcher(node) || replace_response_status(node) - corrector.replace(node.source_range, replacement) - end - end - private def offense_for_matcher(node) @@ -85,13 +80,20 @@ module RuboCop return if offenses.empty? - add_offense(node, message: message_for(*offenses)) + add_offense(node, message: message_for(*offenses), &corrector(node)) end def offense_for_response_status(node) return unless response_status_eq?(node) - add_offense(node, message: message_for(MSG_RESPONSE_STATUS)) + add_offense(node, message: message_for(MSG_RESPONSE_STATUS), &corrector(node)) + end + + def corrector(node) + lambda do |corrector| + replacement = replace_matcher(node) || replace_response_status(node) + corrector.replace(node.source_range, replacement) + end end def replace_matcher(node) diff --git a/rubocop/cop/rspec/httparty_basic_auth.rb b/rubocop/cop/rspec/httparty_basic_auth.rb index c6b52ac9781..1e0f7ae7af0 100644 --- a/rubocop/cop/rspec/httparty_basic_auth.rb +++ b/rubocop/cop/rspec/httparty_basic_auth.rb @@ -12,7 +12,9 @@ module RuboCop # # # good # HTTParty.get(url, basic_auth: { username: 'foo' }) - class HTTPartyBasicAuth < RuboCop::Cop::Cop + class HTTPartyBasicAuth < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MESSAGE = "`basic_auth: { user: ... }` does not work - replace `user:` with `username:`" RESTRICT_ON_SEND = %i(get put post delete).freeze @@ -35,12 +37,8 @@ module RuboCop def on_send(node) return unless m = httparty_basic_auth?(node) - add_offense(m, location: :expression, message: MESSAGE) - end - - def autocorrect(node) - lambda do |corrector| - corrector.replace(node.loc.expression, 'username') + add_offense(m, message: MESSAGE) do |corrector| + corrector.replace(m, 'username') end end end diff --git a/rubocop/cop/rspec/modify_sidekiq_middleware.rb b/rubocop/cop/rspec/modify_sidekiq_middleware.rb index c38f074eb3a..78e3ba223b0 100644 --- a/rubocop/cop/rspec/modify_sidekiq_middleware.rb +++ b/rubocop/cop/rspec/modify_sidekiq_middleware.rb @@ -20,7 +20,9 @@ module RuboCop # end # # - class ModifySidekiqMiddleware < RuboCop::Cop::Cop + class ModifySidekiqMiddleware < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MSG = <<~MSG Don't modify global sidekiq middleware, use the `#with_sidekiq_server_middleware` helper instead @@ -35,11 +37,7 @@ module RuboCop def on_send(node) return unless modifies_sidekiq_middleware?(node) - add_offense(node, location: :expression) - end - - def autocorrect(node) - -> (corrector) do + add_offense(node) do |corrector| corrector.replace(node.loc.expression, 'with_sidekiq_server_middleware') end diff --git a/rubocop/cop/rspec/timecop_freeze.rb b/rubocop/cop/rspec/timecop_freeze.rb index 508b5df7c7f..70e37ecfa55 100644 --- a/rubocop/cop/rspec/timecop_freeze.rb +++ b/rubocop/cop/rspec/timecop_freeze.rb @@ -13,7 +13,9 @@ module RuboCop # # good # freeze_time(Time.current) { example.run } # - class TimecopFreeze < RuboCop::Cop::Cop + class TimecopFreeze < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + include MatchRange MESSAGE = 'Do not use `Timecop.freeze`, use `freeze_time` instead. ' \ 'See https://gitlab.com/gitlab-org/gitlab/-/issues/214432 for more info.' @@ -25,11 +27,7 @@ module RuboCop def on_send(node) return unless timecop_freeze?(node) - add_offense(node, location: :expression, message: MESSAGE) - end - - def autocorrect(node) - -> (corrector) do + add_offense(node, message: MESSAGE) do |corrector| each_match_range(node.source_range, /^(Timecop\.freeze)/) do |match_range| corrector.replace(match_range, 'freeze_time') end diff --git a/rubocop/cop/rspec/timecop_travel.rb b/rubocop/cop/rspec/timecop_travel.rb index e5416953af7..586567fa0cd 100644 --- a/rubocop/cop/rspec/timecop_travel.rb +++ b/rubocop/cop/rspec/timecop_travel.rb @@ -13,7 +13,9 @@ module RuboCop # # good # travel_to(1.day.ago) { create(:issue) } # - class TimecopTravel < RuboCop::Cop::Cop + class TimecopTravel < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + include MatchRange MESSAGE = 'Do not use `Timecop.travel`, use `travel_to` instead. ' \ 'See https://gitlab.com/gitlab-org/gitlab/-/issues/214432 for more info.' @@ -25,11 +27,7 @@ module RuboCop def on_send(node) return unless timecop_travel?(node) - add_offense(node, location: :expression, message: MESSAGE) - end - - def autocorrect(node) - -> (corrector) do + add_offense(node, message: MESSAGE) do |corrector| each_match_range(node.source_range, /^(Timecop\.travel)/) do |match_range| corrector.replace(match_range, 'travel_to') end diff --git a/rubocop/cop/rspec/web_mock_enable.rb b/rubocop/cop/rspec/web_mock_enable.rb index bcf7f95dbbd..0bef16a16b0 100644 --- a/rubocop/cop/rspec/web_mock_enable.rb +++ b/rubocop/cop/rspec/web_mock_enable.rb @@ -3,7 +3,9 @@ module RuboCop module Cop module RSpec - class WebMockEnable < RuboCop::Cop::Cop + class WebMockEnable < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + # This cop checks for `WebMock.disable_net_connect!` usage in specs and # replaces it with `webmock_enable!` # @@ -24,13 +26,9 @@ module RuboCop def on_send(node) if webmock_disable_net_connect?(node) - add_offense(node, location: :expression, message: MESSAGE) - end - end - - def autocorrect(node) - lambda do |corrector| - corrector.replace(node, 'webmock_enable!') + add_offense(node, message: MESSAGE) do |corrector| + corrector.replace(node, 'webmock_enable!') + end end end end |