debian-mirror-gitlab/rubocop/cop/rspec/web_mock_enable.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1 KiB
Ruby
Raw Normal View History

2021-03-08 18:12:59 +05:30
# frozen_string_literal: true
2022-11-25 23:54:43 +05:30
require 'rubocop-rspec'
2021-03-08 18:12:59 +05:30
module RuboCop
module Cop
module RSpec
2022-10-11 01:57:18 +05:30
class WebMockEnable < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
2021-03-08 18:12:59 +05:30
# 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)
2022-10-11 01:57:18 +05:30
add_offense(node, message: MESSAGE) do |corrector|
corrector.replace(node, 'webmock_enable!')
end
2021-03-08 18:12:59 +05:30
end
end
end
end
end
end