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

expect_gitlab_tracking.rb « rspec « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ba65855870555f5861325fbb1fa8f111bff10338 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

require 'rack/utils'

module RuboCop
  module Cop
    module RSpec
      # This cop checks for `expect(Gitlab::Tracking).to receive(:event)` usage in specs.
      # See /spec/support/helpers/snowplow_helpers.rb for details on the replacement.
      #
      # @example
      #
      # # bad
      # it 'expects a snowplow event' do
      #   expect(Gitlab::Tracking).to receive(:event).with("Category", "action", ...)
      # end
      #
      # # good
      # it 'expects a snowplow event', :snowplow do
      #   expect_snowplow_event(category: "Category", action: "action", ...)
      # end
      #
      # # bad
      # it 'does not expect a snowplow event' do
      #   expect(Gitlab::Tracking).not_to receive(:event)
      # end
      #
      # # good
      # it 'does not expect a snowplow event', :snowplow do
      #   expect_no_snowplow_event
      # end
      class ExpectGitlabTracking < RuboCop::Cop::Cop
        MSG = 'Do not expect directly on `Gitlab::Tracking#event`, add the `snowplow` annotation and use ' \
              '`expect_snowplow_event` instead. ' \
              'See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#test-snowplow-events'

        def_node_matcher :expect_gitlab_tracking?, <<~PATTERN
          (send
            (send nil? {:expect :allow}
              (const (const nil? :Gitlab) :Tracking)
            )
            ${:to :to_not :not_to}
            {
              (
                send nil? {:receive :have_received} (sym :event) ...
              )

              (send
                (send nil? {:receive :have_received} (sym :event)) ...
              )
            }
            ...
          )
        PATTERN

        RESTRICT_ON_SEND = [:expect, :allow].freeze

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

          add_offense(node)
        end
      end
    end
  end
end