Commit f73935af authored by Markus Koller's avatar Markus Koller

Merge branch 'caalberts-rubocop-webmock-enable' into 'master'

Add RuboCop check for WebMock.disable_net_connect!

See merge request gitlab-org/gitlab!51200
parents 5e2aa5ec f759aae3
......@@ -326,6 +326,14 @@ RSpec/TimecopTravel:
- 'ee/spec/**/*.rb'
- 'qa/spec/**/*.rb'
RSpec/WebMockEnable:
Enabled: true
Include:
- 'spec/**/*.rb'
- 'ee/spec/**/*.rb'
Exclude:
- 'spec/support/webmock.rb'
Naming/PredicateName:
Enabled: true
Exclude:
......
# 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
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/rspec/web_mock_enable'
RSpec.describe RuboCop::Cop::RSpec::WebMockEnable do
subject(:cop) { described_class.new }
context 'when calling WebMock.disable_net_connect!' do
it 'registers an offence and autocorrects it' do
expect_offense(<<~RUBY)
WebMock.disable_net_connect!(allow_localhost: true)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use webmock_enable! instead of calling WebMock.disable_net_connect! directly.
RUBY
expect_correction(<<~RUBY)
webmock_enable!
RUBY
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment