diff options
author | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 13:34:23 -0600 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 13:34:23 -0600 |
commit | 6438df3a1e0fb944485cebf07976160184697d72 (patch) | |
tree | 00b09bfd170e77ae9391b1a2f5a93ef6839f2597 /rubocop/cop/rspec/web_mock_enable.rb | |
parent | 42bcd54d971da7ef2854b896a7b34f4ef8601067 (diff) | |
download | gitlab-ce-6438df3a1e0fb944485cebf07976160184697d72.tar.gz |
Add latest changes from gitlab-org/gitlab@13-8-stable-eev13.8.0-rc42
Diffstat (limited to 'rubocop/cop/rspec/web_mock_enable.rb')
-rw-r--r-- | rubocop/cop/rspec/web_mock_enable.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/rubocop/cop/rspec/web_mock_enable.rb b/rubocop/cop/rspec/web_mock_enable.rb new file mode 100644 index 00000000000..bcf7f95dbbd --- /dev/null +++ b/rubocop/cop/rspec/web_mock_enable.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module RSpec + class WebMockEnable < RuboCop::Cop::Cop + # This cop checks for `WebMock.disable_net_connect!` usage in specs and + # replaces it with `webmock_enable!` + # + # @example + # + # # bad + # WebMock.disable_net_connect! + # WebMock.disable_net_connect!(allow_localhost: true) + # + # # good + # webmock_enable! + + MESSAGE = 'Use webmock_enable! instead of calling WebMock.disable_net_connect! directly.' + + def_node_matcher :webmock_disable_net_connect?, <<~PATTERN + (send (const nil? :WebMock) :disable_net_connect! ...) + PATTERN + + 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!') + end + end + end + end + end +end |