Commit b4b7d577 authored by Stan Hu's avatar Stan Hu

Merge branch 'dallas-nicolas-snowplow-cop' into 'master'

Add rubocop for expectations on Gitlab::Tracking#event

See merge request gitlab-org/gitlab!43615
parents ffdf2f23 8101581f
......@@ -183,6 +183,35 @@ RSpec/ContextWording:
RSpec/ExpectChange:
Enabled: false
# Offense count: 47
RSpec/ExpectGitlabTracking:
Exclude:
- 'ee/spec/controllers/groups/analytics/coverage_reports_controller_spec.rb'
- 'ee/spec/controllers/projects/settings/operations_controller_spec.rb'
- 'ee/spec/controllers/registrations_controller_spec.rb'
- 'ee/spec/requests/api/visual_review_discussions_spec.rb'
- 'ee/spec/services/epics/issue_promote_service_spec.rb'
- 'spec/controllers/groups/registry/repositories_controller_spec.rb'
- 'spec/controllers/groups_controller_spec.rb'
- 'spec/controllers/projects/registry/repositories_controller_spec.rb'
- 'spec/controllers/projects/registry/tags_controller_spec.rb'
- 'spec/controllers/projects/settings/operations_controller_spec.rb'
- 'spec/controllers/registrations_controller_spec.rb'
- 'spec/lib/api/helpers_spec.rb'
- 'spec/lib/gitlab/experimentation_spec.rb'
- 'spec/mailers/notify_spec.rb'
- 'spec/models/project_services/prometheus_service_spec.rb'
- 'spec/requests/api/project_container_repositories_spec.rb'
- 'spec/services/clusters/applications/check_installation_progress_service_spec.rb'
- 'spec/services/issues/zoom_link_service_spec.rb'
- 'spec/support/helpers/snowplow_helpers.rb'
- 'spec/support/shared_examples/controllers/trackable_shared_examples.rb'
- 'spec/support/shared_examples/requests/api/container_repositories_shared_examples.rb'
- 'spec/support/shared_examples/requests/api/discussions_shared_examples.rb'
- 'spec/support/shared_examples/requests/api/packages_shared_examples.rb'
- 'spec/support/shared_examples/requests/api/tracking_shared_examples.rb'
- 'spec/support/snowplow.rb'
# Offense count: 751
RSpec/ExpectInHook:
Enabled: false
......
# 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
# frozen_string_literal: true
require 'fast_spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/rspec/expect_gitlab_tracking'
RSpec.describe RuboCop::Cop::RSpec::ExpectGitlabTracking do
include CopHelper
let(:source_file) { 'spec/foo_spec.rb' }
subject(:cop) { described_class.new }
good_samples = [
'expect_snowplow_event(category: nil, action: nil)',
'expect_snowplow_event(category: "EventCategory", action: "event_action")',
'expect_snowplow_event(category: "EventCategory", action: "event_action", label: "label", property: "property")',
'expect_no_snowplow_event'
]
bad_samples = [
'expect(Gitlab::Tracking).to receive(:event)',
'expect(Gitlab::Tracking).to_not receive(:event)',
'expect(Gitlab::Tracking).not_to receive(:event)',
'expect(Gitlab::Tracking).to_not receive(:event).with("EventCategory", "event_action")',
'expect(Gitlab::Tracking).not_to receive(:event).with("EventCategory", "event_action")',
'expect(Gitlab::Tracking).to receive(:event).with("EventCategory", "event_action", label: "label", property: "property")',
'expect(Gitlab::Tracking).to have_received(:event).with("EventCategory", "event_action")',
'expect(Gitlab::Tracking).to_not have_received(:event).with("EventCategory", "event_action")',
'expect(Gitlab::Tracking).not_to have_received(:event).with("EventCategory", "event_action")',
'allow(Gitlab::Tracking).to receive(:event).and_call_original'
]
good_samples.each do |good|
context "good: #{good}" do
it 'does not register an offense' do
inspect_source(good)
expect(cop.offenses).to be_empty
end
end
end
bad_samples.each do |bad|
context "bad: #{bad}" do
it 'registers an offense', :aggregate_failures do
inspect_source(bad, source_file)
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
expect(cop.highlights).to eq([bad])
msg = cop.offenses.first.message
expect(msg).to match(
/Do not expect directly on `Gitlab::Tracking#event`/
)
expect(msg).to match(/add the `snowplow` annotation/)
expect(msg).to match(/use `expect_snowplow_event` instead/)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment