diff options
Diffstat (limited to 'spec/tooling/danger')
-rw-r--r-- | spec/tooling/danger/config_files_spec.rb | 30 | ||||
-rw-r--r-- | spec/tooling/danger/feature_flag_spec.rb | 70 | ||||
-rw-r--r-- | spec/tooling/danger/product_intelligence_spec.rb | 47 | ||||
-rw-r--r-- | spec/tooling/danger/project_helper_spec.rb | 10 | ||||
-rw-r--r-- | spec/tooling/danger/specs_spec.rb | 71 | ||||
-rw-r--r-- | spec/tooling/danger/stable_branch_spec.rb | 137 |
6 files changed, 264 insertions, 101 deletions
diff --git a/spec/tooling/danger/config_files_spec.rb b/spec/tooling/danger/config_files_spec.rb index 88b327df63f..65edcabb817 100644 --- a/spec/tooling/danger/config_files_spec.rb +++ b/spec/tooling/danger/config_files_spec.rb @@ -13,7 +13,6 @@ RSpec.describe Tooling::Danger::ConfigFiles do let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) } let(:fake_project_helper) { instance_double(Tooling::Danger::ProjectHelper) } - let(:matching_line) { "+ introduced_by_url:" } subject(:config_file) { fake_danger.new(helper: fake_helper) } @@ -22,29 +21,42 @@ RSpec.describe Tooling::Danger::ConfigFiles do end describe '#add_suggestion_for_missing_introduced_by_url' do - let(:file_lines) do + let(:file_diff) do [ - "---", - "name: about_some_new_flow", - "introduced_by_url: #{url}", - "rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/355909", - "milestone: '14.10'" + "+---", + "+name: about_some_new_flow", + "+introduced_by_url: #{url}", + "+rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/355909", + "+milestone: '14.10'" ] end + let(:file_lines) do + file_diff.map { |line| line.delete_prefix('+') } + end + let(:filename) { 'config/feature_flags/new_ff.yml' } + let(:mr_url) { 'https://gitlab.com/gitlab-org/gitlab/-/merge_requests/1' } before do allow(config_file.project_helper).to receive(:file_lines).and_return(file_lines) allow(config_file.helper).to receive(:added_files).and_return([filename]) - allow(config_file.helper).to receive(:mr_web_url).and_return(url) + allow(config_file.helper).to receive(:changed_lines).with(filename).and_return(file_diff) + allow(config_file.helper).to receive(:mr_web_url).and_return(mr_url) end context 'when config file has an empty introduced_by_url line' do let(:url) { '' } it 'adds suggestions at the correct line' do - expected_format = format(described_class::SUGGEST_INTRODUCED_BY_COMMENT, url: url) + template = <<~SUGGEST_COMMENT + ```suggestion + introduced_by_url: %<mr_url>s + ``` + SUGGEST_COMMENT + + expected_format = format(template, mr_url: mr_url) + expect(config_file).to receive(:markdown).with(expected_format, file: filename, line: 3) config_file.add_suggestion_for_missing_introduced_by_url diff --git a/spec/tooling/danger/feature_flag_spec.rb b/spec/tooling/danger/feature_flag_spec.rb index 0e9eda54510..4575d8ca981 100644 --- a/spec/tooling/danger/feature_flag_spec.rb +++ b/spec/tooling/danger/feature_flag_spec.rb @@ -86,14 +86,20 @@ RSpec.describe Tooling::Danger::FeatureFlag do describe described_class::Found do let(:feature_flag_path) { 'config/feature_flags/development/entry.yml' } let(:group) { 'group::source code' } - let(:raw_yaml) do - YAML.dump( + let(:yaml) do + { 'group' => group, 'default_enabled' => true, - 'rollout_issue_url' => 'https://gitlab.com/gitlab-org/gitlab/-/issues/1' - ) + 'rollout_issue_url' => 'https://gitlab.com/gitlab-org/gitlab/-/issues/1', + 'introduced_by_url' => 'https://gitlab.com/gitlab-org/gitlab/-/issues/2', + 'milestone' => '15.9', + 'type' => 'development', + 'name' => 'entry' + } end + let(:raw_yaml) { YAML.dump(yaml) } + subject(:found) { described_class.new(feature_flag_path) } before do @@ -101,58 +107,34 @@ RSpec.describe Tooling::Danger::FeatureFlag do expect(File).to receive(:read).with(feature_flag_path).and_return(raw_yaml) end - describe '#raw' do - it 'returns the raw YAML' do - expect(found.raw).to eq(raw_yaml) - end - end - - describe '#group' do - it 'returns the group found in the YAML' do - expect(found.group).to eq(group) - end - end - - describe '#default_enabled' do - it 'returns the default_enabled found in the YAML' do - expect(found.default_enabled).to eq(true) + described_class::ATTRIBUTES.each do |attribute| + describe "##{attribute}" do + it 'returns value from the YAML' do + expect(found.public_send(attribute)).to eq(yaml[attribute]) + end end end - describe '#rollout_issue_url' do - it 'returns the rollout_issue_url found in the YAML' do - expect(found.rollout_issue_url).to eq('https://gitlab.com/gitlab-org/gitlab/-/issues/1') + describe '#raw' do + it 'returns the raw YAML' do + expect(found.raw).to eq(raw_yaml) end end describe '#group_match_mr_label?' do - subject(:result) { found.group_match_mr_label?(mr_group_label) } - - context 'when MR labels match FF group' do - let(:mr_group_label) { 'group::source code' } - - specify { expect(result).to eq(true) } - end - - context 'when MR labels does not match FF group' do - let(:mr_group_label) { 'group::authentication and authorization' } - - specify { expect(result).to eq(false) } - end - context 'when group is nil' do let(:group) { nil } - context 'and MR has no group label' do - let(:mr_group_label) { nil } - - specify { expect(result).to eq(true) } + it 'is true only if MR has no group label' do + expect(found.group_match_mr_label?(nil)).to eq true + expect(found.group_match_mr_label?('group::source code')).to eq false end + end - context 'and MR has a group label' do - let(:mr_group_label) { 'group::source code' } - - specify { expect(result).to eq(false) } + context 'when group is not nil' do + it 'is true only if MR has the same group label' do + expect(found.group_match_mr_label?(group)).to eq true + expect(found.group_match_mr_label?('group::authentication and authorization')).to eq false end end end diff --git a/spec/tooling/danger/product_intelligence_spec.rb b/spec/tooling/danger/product_intelligence_spec.rb index fab8b0c61fa..c4cd0e5bfb6 100644 --- a/spec/tooling/danger/product_intelligence_spec.rb +++ b/spec/tooling/danger/product_intelligence_spec.rb @@ -18,7 +18,7 @@ RSpec.describe Tooling::Danger::ProductIntelligence do let(:has_product_intelligence_label) { true } before do - allow(fake_helper).to receive(:changed_lines).and_return(changed_lines) + allow(fake_helper).to receive(:changed_lines).and_return(changed_lines) if defined?(changed_lines) allow(fake_helper).to receive(:labels_to_add).and_return(labels_to_add) allow(fake_helper).to receive(:ci?).and_return(ci_env) allow(fake_helper).to receive(:mr_has_labels?).with('product intelligence').and_return(has_product_intelligence_label) @@ -175,4 +175,49 @@ RSpec.describe Tooling::Danger::ProductIntelligence do end end end + + 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 + expect(product_intelligence).to receive(:warn).with(/usage_data\.rb has been deprecated/) + + product_intelligence.check_usage_data_insertions! + end + end + + context 'and changes are not insertions' do + let(:changed_lines) { ['- ci_runners: count(::Ci::CiRunner),'] } + + it 'doesnt do anything' do + expect(product_intelligence).not_to receive(:warn) + + product_intelligence.check_usage_data_insertions! + end + end + end + + context 'when usage_data.rb is not modified' do + context 'and another file has insertions' do + let(:modified_files) { ['tooling/danger/product_intelligence.rb'] } + + it 'doesnt do anything' do + expect(fake_helper).to receive(:changed_lines).with("lib/gitlab/usage_data.rb").and_return([]) + allow(fake_helper).to receive(:changed_lines).with("tooling/danger/product_intelligence.rb").and_return(["+ Inserting"]) + + expect(product_intelligence).not_to receive(:warn) + + product_intelligence.check_usage_data_insertions! + end + end + end + end end diff --git a/spec/tooling/danger/project_helper_spec.rb b/spec/tooling/danger/project_helper_spec.rb index 669867ffb4f..48050649f54 100644 --- a/spec/tooling/danger/project_helper_spec.rb +++ b/spec/tooling/danger/project_helper_spec.rb @@ -48,8 +48,8 @@ RSpec.describe Tooling::Danger::ProjectHelper do 'PROCESS.md' | [:docs] 'README.md' | [:docs] - 'ee/doc/foo' | [:unknown] - 'ee/README' | [:unknown] + 'ee/doc/foo' | [:none] + 'ee/README' | [:none] 'app/assets/foo' | [:frontend] 'app/views/foo' | [:frontend, :backend] @@ -139,7 +139,7 @@ RSpec.describe Tooling::Danger::ProjectHelper do 'lib/gitlab/ci/templates/Security/SAST.gitlab-ci.yml' | [:ci_template] 'lib/gitlab/ci/templates/dotNET-Core.yml' | [:ci_template] - 'ee/FOO_VERSION' | [:unknown] + 'ee/FOO_VERSION' | [:none] 'db/schema.rb' | [:database] 'db/structure.sql' | [:database] @@ -170,8 +170,8 @@ RSpec.describe Tooling::Danger::ProjectHelper do 'locale/gitlab.pot' | [:none] - 'FOO' | [:unknown] - 'foo' | [:unknown] + 'FOO' | [:none] + 'foo' | [:none] 'foo/bar.rb' | [:backend] 'foo/bar.js' | [:frontend] diff --git a/spec/tooling/danger/specs_spec.rb b/spec/tooling/danger/specs_spec.rb index 422923827a8..cdac5954f92 100644 --- a/spec/tooling/danger/specs_spec.rb +++ b/spec/tooling/danger/specs_spec.rb @@ -19,14 +19,16 @@ RSpec.describe Tooling::Danger::Specs, feature_category: :tooling do let(:file_lines) do [ " describe 'foo' do", - " expect(foo).to match(['bar'])", + " expect(foo).to match(['bar', 'baz'])", " end", - " expect(foo).to match(['bar'])", # same line as line 1 above, we expect two different suggestions + " expect(foo).to match(['bar', 'baz'])", # same line as line 1 above, we expect two different suggestions " ", - " expect(foo).to match ['bar']", - " expect(foo).to eq(['bar'])", - " expect(foo).to eq ['bar']", - " expect(foo).to(match(['bar']))", + " expect(foo).to match ['bar', 'baz']", + " expect(foo).to eq(['bar', 'baz'])", + " expect(foo).to eq ['bar', 'baz']", + " expect(foo).to(match(['bar', 'baz']))", + " expect(foo).to(eq(['bar', 'baz']))", + " expect(foo).to(eq([bar, baz]))", " expect(foo).to(eq(['bar']))", " foo.eq(['bar'])" ] @@ -35,28 +37,30 @@ RSpec.describe Tooling::Danger::Specs, feature_category: :tooling do let(:matching_lines) do [ "+ expect(foo).to match(['should not error'])", - "+ expect(foo).to match(['bar'])", - "+ expect(foo).to match(['bar'])", - "+ expect(foo).to match ['bar']", - "+ expect(foo).to eq(['bar'])", - "+ expect(foo).to eq ['bar']", - "+ expect(foo).to(match(['bar']))", - "+ expect(foo).to(eq(['bar']))" + "+ expect(foo).to match(['bar', 'baz'])", + "+ expect(foo).to match(['bar', 'baz'])", + "+ expect(foo).to match ['bar', 'baz']", + "+ expect(foo).to eq(['bar', 'baz'])", + "+ expect(foo).to eq ['bar', 'baz']", + "+ expect(foo).to(match(['bar', 'baz']))", + "+ expect(foo).to(eq(['bar', 'baz']))", + "+ expect(foo).to(eq([bar, baz]))" ] end let(:changed_lines) do [ - " expect(foo).to match(['bar'])", - " expect(foo).to match(['bar'])", - " expect(foo).to match ['bar']", - " expect(foo).to eq(['bar'])", - " expect(foo).to eq ['bar']", - "- expect(foo).to match(['bar'])", - "- expect(foo).to match(['bar'])", - "- expect(foo).to match ['bar']", - "- expect(foo).to eq(['bar'])", - "- expect(foo).to eq ['bar']", + " expect(foo).to match(['bar', 'baz'])", + " expect(foo).to match(['bar', 'baz'])", + " expect(foo).to match ['bar', 'baz']", + " expect(foo).to eq(['bar', 'baz'])", + " expect(foo).to eq ['bar', 'baz']", + "- expect(foo).to match(['bar', 'baz'])", + "- expect(foo).to match(['bar', 'baz'])", + "- expect(foo).to match ['bar', 'baz']", + "- expect(foo).to eq(['bar', 'baz'])", + "- expect(foo).to eq ['bar', 'baz']", + "- expect(foo).to eq [bar, foo]", "+ expect(foo).to eq([])" ] + matching_lines end @@ -107,7 +111,7 @@ RSpec.describe Tooling::Danger::Specs, feature_category: :tooling do describe '#add_suggestions_for_match_with_array' do let(:template) do - <<~MARKDOWN + <<~MARKDOWN.chomp ```suggestion %<suggested_line>s ``` @@ -118,13 +122,14 @@ RSpec.describe Tooling::Danger::Specs, feature_category: :tooling do it 'adds suggestions at the correct lines' do [ - { suggested_line: " expect(foo).to match_array(['bar'])", number: 2 }, - { suggested_line: " expect(foo).to match_array(['bar'])", number: 4 }, - { suggested_line: " expect(foo).to match_array ['bar']", number: 6 }, - { suggested_line: " expect(foo).to match_array(['bar'])", number: 7 }, - { suggested_line: " expect(foo).to match_array ['bar']", number: 8 }, - { suggested_line: " expect(foo).to(match_array(['bar']))", number: 9 }, - { suggested_line: " expect(foo).to(match_array(['bar']))", number: 10 } + { suggested_line: " expect(foo).to match_array(['bar', 'baz'])", number: 2 }, + { suggested_line: " expect(foo).to match_array(['bar', 'baz'])", number: 4 }, + { suggested_line: " expect(foo).to match_array ['bar', 'baz']", number: 6 }, + { suggested_line: " expect(foo).to match_array(['bar', 'baz'])", number: 7 }, + { suggested_line: " expect(foo).to match_array ['bar', 'baz']", number: 8 }, + { suggested_line: " expect(foo).to(match_array(['bar', 'baz']))", number: 9 }, + { suggested_line: " expect(foo).to(match_array(['bar', 'baz']))", number: 10 }, + { suggested_line: " expect(foo).to(match_array([bar, baz]))", number: 11 } ].each do |test_case| comment = format(template, suggested_line: test_case[:suggested_line]) expect(specs).to receive(:markdown).with(comment, file: filename, line: test_case[:number]) @@ -136,7 +141,7 @@ RSpec.describe Tooling::Danger::Specs, feature_category: :tooling do describe '#add_suggestions_for_project_factory_usage' do let(:template) do - <<~MARKDOWN + <<~MARKDOWN.chomp ```suggestion %<suggested_line>s ``` @@ -220,7 +225,7 @@ RSpec.describe Tooling::Danger::Specs, feature_category: :tooling do describe '#add_suggestions_for_feature_category' do let(:template) do - <<~SUGGESTION_MARKDOWN + <<~SUGGESTION_MARKDOWN.chomp ```suggestion %<suggested_line>s ``` diff --git a/spec/tooling/danger/stable_branch_spec.rb b/spec/tooling/danger/stable_branch_spec.rb index 08fd25b30e0..9eee077d493 100644 --- a/spec/tooling/danger/stable_branch_spec.rb +++ b/spec/tooling/danger/stable_branch_spec.rb @@ -12,6 +12,8 @@ RSpec.describe Tooling::Danger::StableBranch, feature_category: :delivery do include_context 'with dangerfile' let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) } + let(:fake_api) { double('Api') } # rubocop:disable RSpec/VerifiedDoubles + let(:gitlab_gem_client) { double('gitlab') } # rubocop:disable RSpec/VerifiedDoubles let(:stable_branch) { fake_danger.new(helper: fake_helper) } @@ -34,6 +36,28 @@ RSpec.describe Tooling::Danger::StableBranch, feature_category: :delivery do end end + shared_examples 'with a warning' do |warning_message| + it 'warns' do + expect(stable_branch).to receive(:warn).with(warning_message) + + subject + end + end + + shared_examples 'bypassing when flaky test or docs only' do + context 'when failure::flaky-test label is present' do + let(:flaky_test_label_present) { true } + + it_behaves_like 'without a failure' + end + + context 'with only docs changes' do + let(:changes_by_category_response) { { docs: ['foo.md'] } } + + it_behaves_like 'without a failure' + end + end + context 'when not applicable' do where(:stable_branch?, :security_mr?) do true | true @@ -47,15 +71,32 @@ RSpec.describe Tooling::Danger::StableBranch, feature_category: :delivery do allow(fake_helper).to receive(:security_mr?).and_return(security_mr?) end - it_behaves_like "without a failure" + it_behaves_like 'without a failure' end end context 'when applicable' do let(:target_branch) { '15-1-stable-ee' } + let(:source_branch) { 'my_bug_branch' } let(:feature_label_present) { false } let(:bug_label_present) { true } + let(:pipeline_expedite_label_present) { false } + let(:flaky_test_label_present) { false } let(:response_success) { true } + + let(:changes_by_category_response) do + { + graphql: ['bar.rb'] + } + end + + let(:pipeline_bridges_response) do + [ + { 'name' => 'e2e:package-and-test', + 'status' => 'success' } + ] + end + let(:parsed_response) do [ { 'version' => '15.1.1' }, @@ -79,14 +120,27 @@ RSpec.describe Tooling::Danger::StableBranch, feature_category: :delivery do before do allow(fake_helper).to receive(:mr_target_branch).and_return(target_branch) + allow(fake_helper).to receive(:mr_source_branch).and_return(source_branch) allow(fake_helper).to receive(:security_mr?).and_return(false) + allow(fake_helper).to receive(:mr_target_project_id).and_return(1) allow(fake_helper).to receive(:mr_has_labels?).with('type::feature').and_return(feature_label_present) allow(fake_helper).to receive(:mr_has_labels?).with('type::bug').and_return(bug_label_present) + allow(fake_helper).to receive(:mr_has_labels?).with('pipeline:expedite') + .and_return(pipeline_expedite_label_present) + allow(fake_helper).to receive(:mr_has_labels?).with('failure::flaky-test') + .and_return(flaky_test_label_present) + allow(fake_helper).to receive(:changes_by_category).and_return(changes_by_category_response) allow(HTTParty).to receive(:get).with(/page=1/).and_return(version_response) + allow(fake_helper).to receive(:api).and_return(fake_api) + allow(stable_branch).to receive(:gitlab).and_return(gitlab_gem_client) + allow(gitlab_gem_client).to receive(:mr_json).and_return({ 'head_pipeline' => { 'id' => '1' } }) + allow(gitlab_gem_client).to receive(:api).and_return(fake_api) + allow(fake_api).to receive(:pipeline_bridges).with(1, '1') + .and_return(pipeline_bridges_response) end # the stubbed behavior above is the success path - it_behaves_like "without a failure" + it_behaves_like 'without a failure' context 'with a feature label' do let(:feature_label_present) { true } @@ -100,20 +154,65 @@ RSpec.describe Tooling::Danger::StableBranch, feature_category: :delivery do it_behaves_like 'with a failure', described_class::BUG_ERROR_MESSAGE end + context 'with a pipeline:expedite label' do + let(:pipeline_expedite_label_present) { true } + + it_behaves_like 'with a failure', described_class::PIPELINE_EXPEDITE_ERROR_MESSAGE + it_behaves_like 'bypassing when flaky test or docs only' + end + + context 'when no package-and-test job is found' do + let(:pipeline_bridges_response) { nil } + + it_behaves_like 'with a failure', described_class::NEEDS_PACKAGE_AND_TEST_MESSAGE + it_behaves_like 'bypassing when flaky test or docs only' + end + + context 'when package-and-test job is in manual state' do + described_class::FAILING_PACKAGE_AND_TEST_STATUSES.each do |status| + let(:pipeline_bridges_response) do + [ + { 'name' => 'e2e:package-and-test', + 'status' => status } + ] + end + + it_behaves_like 'with a failure', described_class::NEEDS_PACKAGE_AND_TEST_MESSAGE + it_behaves_like 'bypassing when flaky test or docs only' + end + end + + context 'when package-and-test job is in a non-successful state' do + let(:pipeline_bridges_response) do + [ + { 'name' => 'e2e:package-and-test', + 'status' => 'running' } + ] + end + + it_behaves_like 'with a warning', described_class::WARN_PACKAGE_AND_TEST_MESSAGE + it_behaves_like 'bypassing when flaky test or docs only' + end + + context 'when no pipeline is found' do + before do + allow(gitlab_gem_client).to receive(:mr_json).and_return({}) + end + + it_behaves_like 'with a failure', described_class::NEEDS_PACKAGE_AND_TEST_MESSAGE + it_behaves_like 'bypassing when flaky test or docs only' + end + context 'when not an applicable version' do let(:target_branch) { '14-9-stable-ee' } - it_behaves_like 'with a failure', described_class::VERSION_ERROR_MESSAGE + it_behaves_like 'with a warning', described_class::VERSION_WARNING_MESSAGE end context 'when the version API request fails' do let(:response_success) { false } - it 'adds a warning' do - expect(stable_branch).to receive(:warn).with(described_class::FAILED_VERSION_REQUEST_MESSAGE) - - subject - end + it_behaves_like 'with a warning', described_class::FAILED_VERSION_REQUEST_MESSAGE end context 'when more than one page of versions is needed' do @@ -151,7 +250,7 @@ RSpec.describe Tooling::Danger::StableBranch, feature_category: :delivery do allow(HTTParty).to receive(:get).with(/page=2/).and_return(second_version_response) end - it_behaves_like "without a failure" + it_behaves_like 'without a failure' end context 'when too many version API requests are made' do @@ -166,4 +265,24 @@ RSpec.describe Tooling::Danger::StableBranch, feature_category: :delivery do end end end + + describe '#non_security_stable_branch?' do + subject { stable_branch.non_security_stable_branch? } + + where(:stable_branch?, :security_mr?, :expected_result) do + true | true | false + false | true | false + true | false | true + false | false | false + end + + with_them do + before do + allow(fake_helper).to receive(:mr_target_branch).and_return(stable_branch? ? '15-1-stable-ee' : 'main') + allow(fake_helper).to receive(:security_mr?).and_return(security_mr?) + end + + it { is_expected.to eq(expected_result) } + end + end end |