diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-01 09:09:45 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-01 09:09:45 +0300 |
commit | 533ef6532ae5499b8ff2f886cbc677844c7b82e1 (patch) | |
tree | 26fdc7ca683c23bc2bc940b23fc3f11ded05fcae /spec/tooling/danger | |
parent | 7658b09b49ce4abfc402682d446ce263017f3a57 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/tooling/danger')
-rw-r--r-- | spec/tooling/danger/project_helper_spec.rb | 13 | ||||
-rw-r--r-- | spec/tooling/danger/specs_spec.rb | 133 |
2 files changed, 146 insertions, 0 deletions
diff --git a/spec/tooling/danger/project_helper_spec.rb b/spec/tooling/danger/project_helper_spec.rb index 8a73c484403..5edd9e54cc5 100644 --- a/spec/tooling/danger/project_helper_spec.rb +++ b/spec/tooling/danger/project_helper_spec.rb @@ -280,4 +280,17 @@ RSpec.describe Tooling::Danger::ProjectHelper do is_expected.to eq('gitlab-foss') end end + + describe '#file_lines' do + let(:filename) { 'spec/foo_spec.rb' } + let(:file_spy) { spy } + + it 'returns the chomped file lines' do + expect(project_helper).to receive(:read_file).with(filename).and_return(file_spy) + + project_helper.file_lines(filename) + + expect(file_spy).to have_received(:lines).with(chomp: true) + end + end end diff --git a/spec/tooling/danger/specs_spec.rb b/spec/tooling/danger/specs_spec.rb new file mode 100644 index 00000000000..a5978020c9d --- /dev/null +++ b/spec/tooling/danger/specs_spec.rb @@ -0,0 +1,133 @@ +# frozen_string_literal: true + +require 'rspec-parameterized' +require 'gitlab-dangerfiles' +require 'danger' +require 'danger/plugins/helper' +require 'gitlab/dangerfiles/spec_helper' + +require_relative '../../../tooling/danger/specs' +require_relative '../../../tooling/danger/project_helper' + +RSpec.describe Tooling::Danger::Specs do + include_context "with dangerfile" + + let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) } + let(:fake_project_helper) { double('fake-project-helper', helper: fake_helper).tap { |h| h.class.include(Tooling::Danger::ProjectHelper) } } + let(:file_lines) do + [ + " describe 'foo' do", + " expect(foo).to match(['bar'])", + " end", + " expect(foo).to match(['bar'])", # 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(eq(['bar']))", + " foo.eq(['bar'])" + ] + end + + let(:matching_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(eq(['bar']))" + ] + end + + subject(:specs) { fake_danger.new(helper: fake_helper) } + + before do + allow(specs).to receive(:project_helper).and_return(fake_project_helper) + end + + describe '#add_suggestions_for_match_with_array' do + let(:filename) { 'spec/foo_spec.rb' } + + before do + expect(specs).to receive(:added_line_matching_match_with_array).and_return(matching_lines) + allow(specs.project_helper).to receive(:file_lines).and_return(file_lines) + end + + it 'adds suggestions at the correct lines' do + expect(specs).to receive(:markdown).with(format(described_class::SUGGEST_MR_COMMENT, suggested_line: " expect(foo).to match_array(['bar'])"), file: filename, line: 2) + expect(specs).to receive(:markdown).with(format(described_class::SUGGEST_MR_COMMENT, suggested_line: " expect(foo).to match_array(['bar'])"), file: filename, line: 4) + expect(specs).to receive(:markdown).with(format(described_class::SUGGEST_MR_COMMENT, suggested_line: " expect(foo).to match_array ['bar']"), file: filename, line: 6) + expect(specs).to receive(:markdown).with(format(described_class::SUGGEST_MR_COMMENT, suggested_line: " expect(foo).to match_array(['bar'])"), file: filename, line: 7) + expect(specs).to receive(:markdown).with(format(described_class::SUGGEST_MR_COMMENT, suggested_line: " expect(foo).to match_array ['bar']"), file: filename, line: 8) + expect(specs).to receive(:markdown).with(format(described_class::SUGGEST_MR_COMMENT, suggested_line: " expect(foo).to(match_array(['bar']))"), file: filename, line: 9) + expect(specs).to receive(:markdown).with(format(described_class::SUGGEST_MR_COMMENT, suggested_line: " expect(foo).to(match_array(['bar']))"), file: filename, line: 10) + + specs.add_suggestions_for_match_with_array(filename) + end + end + + describe '#changed_specs_files' do + let(:base_expected_files) { %w[spec/foo_spec.rb ee/spec/foo_spec.rb spec/bar_spec.rb ee/spec/bar_spec.rb spec/zab_spec.rb ee/spec/zab_spec.rb] } + + before do + all_changed_files = %w[ + app/workers/a.rb + app/workers/b.rb + app/workers/e.rb + spec/foo_spec.rb + ee/spec/foo_spec.rb + spec/bar_spec.rb + ee/spec/bar_spec.rb + spec/zab_spec.rb + ee/spec/zab_spec.rb + ] + + allow(specs.helper).to receive(:all_changed_files).and_return(all_changed_files) + end + + it 'returns added, modified, and renamed_after files by default' do + expect(specs.changed_specs_files).to match_array(base_expected_files) + end + + context 'with include_ee: :exclude' do + it 'returns spec files without EE-specific files' do + expect(specs.changed_specs_files(ee: :exclude)).not_to include(%w[ee/spec/foo_spec.rb ee/spec/bar_spec.rb ee/spec/zab_spec.rb]) + end + end + + context 'with include_ee: :only' do + it 'returns EE-specific spec files only' do + expect(specs.changed_specs_files(ee: :only)).to match_array(%w[ee/spec/foo_spec.rb ee/spec/bar_spec.rb ee/spec/zab_spec.rb]) + end + end + end + + describe '#added_line_matching_match_with_array' do + let(:filename) { 'spec/foo_spec.rb' } + 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']" + ] + matching_lines + end + + before do + allow(specs.helper).to receive(:changed_lines).with(filename).and_return(changed_lines) + end + + it 'returns added, modified, and renamed_after files by default' do + expect(specs.added_line_matching_match_with_array(filename)).to match_array(matching_lines) + end + end +end |