diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
commit | 48aff82709769b098321c738f3444b9bdaa694c6 (patch) | |
tree | e00c7c43e2d9b603a5a6af576b1685e400410dee /rubocop/cop/rspec/expect_gitlab_tracking.rb | |
parent | 879f5329ee916a948223f8f43d77fba4da6cd028 (diff) |
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'rubocop/cop/rspec/expect_gitlab_tracking.rb')
-rw-r--r-- | rubocop/cop/rspec/expect_gitlab_tracking.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/rubocop/cop/rspec/expect_gitlab_tracking.rb b/rubocop/cop/rspec/expect_gitlab_tracking.rb new file mode 100644 index 00000000000..ba658558705 --- /dev/null +++ b/rubocop/cop/rspec/expect_gitlab_tracking.rb @@ -0,0 +1,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 |