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

before_all.rb « rspec « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3fbb9447bc6ff3fca87fcf5f802b97f8ec189884 (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
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

require 'rubocop-rspec'

module Rubocop
  module Cop
    module RSpec
      # This cop checks for `before(:all) in RSpec tests`
      #
      # @example
      #
      #  bad
      #
      #  before(:all) do
      #    project.repository.add_tag(user, 'v1.2.3', 'master')
      #  end
      #
      #  good
      #
      #  before_all do
      #    project.repository.add_tag(user, 'v1.2.3', 'master')
      #  end
      #
      class BeforeAll < RuboCop::Cop::Base
        extend RuboCop::Cop::AutoCorrector

        MSG = "Prefer using `before_all` over `before(:all)`. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#common-test-setup"

        RESTRICT_ON_SEND = %i[before].freeze

        def_node_matcher :before_all_block?, <<~PATTERN
          (send nil? :before (sym :all) ...)
        PATTERN

        def on_send(node)
          return unless before_all_block?(node)

          add_offense(node) do |corrector|
            replacement = 'before_all'
            corrector.replace(node.source_range, replacement)
          end
        end
      end
    end
  end
end