Welcome to mirror list, hosted at ThFree Co, Russian Federation.

web_mock_enable.rb « rspec « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0bef16a16b09d13de992eb9b230bd07bccc74aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      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!`
        #
        # @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, message: MESSAGE) do |corrector|
              corrector.replace(node, 'webmock_enable!')
            end
          end
        end
      end
    end
  end
end