debian-mirror-gitlab/spec/tooling/danger/analytics_instrumentation_spec.rb

235 lines
7.8 KiB
Ruby
Raw Permalink Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
require 'gitlab-dangerfiles'
require 'gitlab/dangerfiles/spec_helper'
2023-07-09 08:55:56 +05:30
require_relative '../../../tooling/danger/analytics_instrumentation'
2021-09-04 01:27:46 +05:30
require_relative '../../../tooling/danger/project_helper'
2023-07-09 08:55:56 +05:30
RSpec.describe Tooling::Danger::AnalyticsInstrumentation, feature_category: :service_ping do
2021-09-04 01:27:46 +05:30
include_context "with dangerfile"
2023-07-09 08:55:56 +05:30
subject(:analytics_instrumentation) { fake_danger.new(helper: fake_helper) }
2021-09-04 01:27:46 +05:30
let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) }
2023-03-04 22:38:38 +05:30
let(:previous_label_to_add) { 'label_to_add' }
let(:labels_to_add) { [previous_label_to_add] }
let(:ci_env) { true }
2023-07-09 08:55:56 +05:30
let(:has_analytics_instrumentation_label) { true }
2021-09-04 01:27:46 +05:30
before do
2023-04-23 21:23:45 +05:30
allow(fake_helper).to receive(:changed_lines).and_return(changed_lines) if defined?(changed_lines)
2023-03-04 22:38:38 +05:30
allow(fake_helper).to receive(:labels_to_add).and_return(labels_to_add)
allow(fake_helper).to receive(:ci?).and_return(ci_env)
2023-07-09 08:55:56 +05:30
allow(fake_helper).to receive(:mr_has_labels?).with('analytics instrumentation').and_return(has_analytics_instrumentation_label)
2021-09-04 01:27:46 +05:30
end
2022-06-21 17:19:12 +05:30
describe '#check!' do
2023-07-09 08:55:56 +05:30
subject { analytics_instrumentation.check! }
2021-09-04 01:27:46 +05:30
2022-06-21 17:19:12 +05:30
let(:markdown_formatted_list) { 'markdown formatted list' }
2023-07-09 08:55:56 +05:30
let(:review_pending_label) { 'analytics instrumentation::review pending' }
let(:approved_label) { 'analytics instrumentation::approved' }
2023-03-04 22:38:38 +05:30
let(:changed_files) { ['metrics/counts_7d/test_metric.yml'] }
let(:changed_lines) { ['+tier: ee'] }
2023-07-09 08:55:56 +05:30
let(:fake_changes) { instance_double(Gitlab::Dangerfiles::Changes, files: changed_files) }
2021-09-04 01:27:46 +05:30
before do
2023-07-09 08:55:56 +05:30
allow(fake_changes).to receive(:by_category).with(:analytics_instrumentation).and_return(fake_changes)
allow(fake_helper).to receive(:changes).and_return(fake_changes)
2023-03-04 22:38:38 +05:30
allow(fake_helper).to receive(:all_changed_files).and_return(changed_files)
2022-06-21 17:19:12 +05:30
allow(fake_helper).to receive(:markdown_list).with(changed_files).and_return(markdown_formatted_list)
2021-09-04 01:27:46 +05:30
end
2022-06-21 17:19:12 +05:30
shared_examples "doesn't add new labels" do
it "doesn't add new labels" do
subject
2021-09-04 01:27:46 +05:30
2022-06-21 17:19:12 +05:30
expect(labels_to_add).to match_array [previous_label_to_add]
end
2021-09-04 01:27:46 +05:30
end
2022-06-21 17:19:12 +05:30
shared_examples "doesn't add new warnings" do
it "doesn't add new warnings" do
2023-07-09 08:55:56 +05:30
expect(analytics_instrumentation).not_to receive(:warn)
2021-09-04 01:27:46 +05:30
2022-06-21 17:19:12 +05:30
subject
end
2021-09-04 01:27:46 +05:30
end
2022-06-21 17:19:12 +05:30
shared_examples 'adds new labels' do
it 'adds new labels' do
subject
expect(labels_to_add).to match_array [previous_label_to_add, review_pending_label]
end
2023-07-09 08:55:56 +05:30
it 'receives all the changed files by calling the correct helper method', :aggregate_failures do
expect(fake_helper).not_to receive(:changes_by_category)
expect(fake_helper).to receive(:changes)
expect(fake_changes).to receive(:by_category).with(:analytics_instrumentation)
expect(fake_changes).to receive(:files)
subject
end
2022-06-21 17:19:12 +05:30
end
2021-09-04 01:27:46 +05:30
2022-06-21 17:19:12 +05:30
context 'with growth experiment label' do
2021-09-04 01:27:46 +05:30
before do
2022-06-21 17:19:12 +05:30
allow(fake_helper).to receive(:mr_has_labels?).with('growth experiment').and_return(true)
2021-09-04 01:27:46 +05:30
end
2022-06-21 17:19:12 +05:30
include_examples "doesn't add new labels"
include_examples "doesn't add new warnings"
end
context 'without growth experiment label' do
before do
allow(fake_helper).to receive(:mr_has_labels?).with('growth experiment').and_return(false)
end
2021-09-04 01:27:46 +05:30
2022-06-21 17:19:12 +05:30
context 'with approved label' do
let(:mr_labels) { [approved_label] }
2021-12-11 22:18:48 +05:30
2022-06-21 17:19:12 +05:30
include_examples "doesn't add new labels"
include_examples "doesn't add new warnings"
2021-09-04 01:27:46 +05:30
end
2022-06-21 17:19:12 +05:30
context 'without approved label' do
include_examples 'adds new labels'
it 'warns with proper message' do
2023-07-09 08:55:56 +05:30
expect(analytics_instrumentation).to receive(:warn).with(%r{#{markdown_formatted_list}})
2021-12-11 22:18:48 +05:30
2022-06-21 17:19:12 +05:30
subject
end
2021-12-11 22:18:48 +05:30
end
2022-01-26 12:08:38 +05:30
2023-07-09 08:55:56 +05:30
context 'with analytics instrumentation::review pending label' do
let(:mr_labels) { ['analytics instrumentation::review pending'] }
2021-11-18 22:05:49 +05:30
2022-06-21 17:19:12 +05:30
include_examples "doesn't add new labels"
2021-11-18 22:05:49 +05:30
end
2023-07-09 08:55:56 +05:30
context 'with analytics instrumentation::approved label' do
let(:mr_labels) { ['analytics instrumentation::approved'] }
2022-01-26 12:08:38 +05:30
2022-06-21 17:19:12 +05:30
include_examples "doesn't add new labels"
2022-01-26 12:08:38 +05:30
end
2023-07-09 08:55:56 +05:30
context 'with the analytics instrumentation label' do
let(:has_analytics_instrumentation_label) { true }
2022-06-21 17:19:12 +05:30
context 'with ci? false' do
let(:ci_env) { false }
include_examples "doesn't add new labels"
end
context 'with ci? true' do
include_examples 'adds new labels'
end
end
2021-11-18 22:05:49 +05:30
end
2021-09-04 01:27:46 +05:30
end
2023-03-04 22:38:38 +05:30
describe '#check_affected_scopes!' do
let(:fixture_dir_glob) { Dir.glob(File.join('spec', 'tooling', 'fixtures', 'metrics', '*.rb')) }
let(:changed_lines) { ['+ scope :active, -> { iwhere(email: Array(emails)) }'] }
before do
allow(Dir).to receive(:glob).and_return(fixture_dir_glob)
allow(fake_helper).to receive(:markdown_list).with({ 'active' => fixture_dir_glob }).and_return('a')
end
context 'when a model was modified' do
let(:modified_files) { ['app/models/super_user.rb'] }
context 'when a scope is changed' do
context 'and a metrics uses the affected scope' do
it 'producing warning' do
2023-07-09 08:55:56 +05:30
expect(analytics_instrumentation).to receive(:warn).with(%r{#{modified_files}})
2023-03-04 22:38:38 +05:30
2023-07-09 08:55:56 +05:30
analytics_instrumentation.check_affected_scopes!
2023-03-04 22:38:38 +05:30
end
end
context 'when no metrics using the affected scope' do
let(:changed_lines) { ['+scope :foo, -> { iwhere(email: Array(emails)) }'] }
it 'doesnt do anything' do
2023-07-09 08:55:56 +05:30
expect(analytics_instrumentation).not_to receive(:warn)
2023-03-04 22:38:38 +05:30
2023-07-09 08:55:56 +05:30
analytics_instrumentation.check_affected_scopes!
2023-03-04 22:38:38 +05:30
end
end
end
end
context 'when an unrelated model with matching scope was modified' do
let(:modified_files) { ['app/models/post_box.rb'] }
it 'doesnt do anything' do
2023-07-09 08:55:56 +05:30
expect(analytics_instrumentation).not_to receive(:warn)
2023-03-04 22:38:38 +05:30
2023-07-09 08:55:56 +05:30
analytics_instrumentation.check_affected_scopes!
2023-03-04 22:38:38 +05:30
end
end
context 'when models arent modified' do
let(:modified_files) { ['spec/app/models/user_spec.rb'] }
it 'doesnt do anything' do
2023-07-09 08:55:56 +05:30
expect(analytics_instrumentation).not_to receive(:warn)
2023-03-04 22:38:38 +05:30
2023-07-09 08:55:56 +05:30
analytics_instrumentation.check_affected_scopes!
2023-03-04 22:38:38 +05:30
end
end
end
2023-04-23 21:23:45 +05:30
describe '#check_usage_data_insertions!' do
context 'when usage_data.rb is modified' do
let(:modified_files) { ['lib/gitlab/usage_data.rb'] }
before do
allow(fake_helper).to receive(:changed_lines).with("lib/gitlab/usage_data.rb").and_return(changed_lines)
end
context 'and has insertions' do
let(:changed_lines) { ['+ ci_runners: count(::Ci::CiRunner),'] }
it 'produces warning' do
2023-07-09 08:55:56 +05:30
expect(analytics_instrumentation).to receive(:warn).with(/usage_data\.rb has been deprecated/)
2023-04-23 21:23:45 +05:30
2023-07-09 08:55:56 +05:30
analytics_instrumentation.check_usage_data_insertions!
2023-04-23 21:23:45 +05:30
end
end
context 'and changes are not insertions' do
let(:changed_lines) { ['- ci_runners: count(::Ci::CiRunner),'] }
it 'doesnt do anything' do
2023-07-09 08:55:56 +05:30
expect(analytics_instrumentation).not_to receive(:warn)
2023-04-23 21:23:45 +05:30
2023-07-09 08:55:56 +05:30
analytics_instrumentation.check_usage_data_insertions!
2023-04-23 21:23:45 +05:30
end
end
end
context 'when usage_data.rb is not modified' do
context 'and another file has insertions' do
2023-07-09 08:55:56 +05:30
let(:modified_files) { ['tooling/danger/analytics_instrumentation.rb'] }
2023-04-23 21:23:45 +05:30
it 'doesnt do anything' do
expect(fake_helper).to receive(:changed_lines).with("lib/gitlab/usage_data.rb").and_return([])
2023-07-09 08:55:56 +05:30
allow(fake_helper).to receive(:changed_lines).with("tooling/danger/analytics_instrumentation.rb").and_return(["+ Inserting"])
2023-04-23 21:23:45 +05:30
2023-07-09 08:55:56 +05:30
expect(analytics_instrumentation).not_to receive(:warn)
2023-04-23 21:23:45 +05:30
2023-07-09 08:55:56 +05:30
analytics_instrumentation.check_usage_data_insertions!
2023-04-23 21:23:45 +05:30
end
end
end
end
2021-09-04 01:27:46 +05:30
end