diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-08 00:08:21 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-08 00:08:21 +0300 |
commit | a87ae2e97ee1eb3af2338e33bc3b2fb0e1ae1d42 (patch) | |
tree | 698e1cefd12d12f9230b985988d91e6820f40040 /rubocop | |
parent | 1850d48925997ccc467fe0cbe6144d1d67fbdb55 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-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 |