From 6e734c809b18a0470d81c78e1ecd9b3f8278de89 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Fri, 8 Jan 2021 15:10:26 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- rubocop/cop/rspec/web_mock_enable.rb | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 rubocop/cop/rspec/web_mock_enable.rb (limited to 'rubocop') 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 -- cgit v1.2.1