diff options
Diffstat (limited to 'spec/tooling')
-rw-r--r-- | spec/tooling/danger/config_files_spec.rb | 2 | ||||
-rw-r--r-- | spec/tooling/danger/project_helper_spec.rb | 9 | ||||
-rw-r--r-- | spec/tooling/danger/specs_spec.rb | 166 | ||||
-rw-r--r-- | spec/tooling/quality/test_level_spec.rb | 2 |
4 files changed, 133 insertions, 46 deletions
diff --git a/spec/tooling/danger/config_files_spec.rb b/spec/tooling/danger/config_files_spec.rb index 0e01908a1dd..88b327df63f 100644 --- a/spec/tooling/danger/config_files_spec.rb +++ b/spec/tooling/danger/config_files_spec.rb @@ -25,7 +25,7 @@ RSpec.describe Tooling::Danger::ConfigFiles do let(:file_lines) do [ "---", - "name: about_your_company_registration_flow", + "name: about_some_new_flow", "introduced_by_url: #{url}", "rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/355909", "milestone: '14.10'" diff --git a/spec/tooling/danger/project_helper_spec.rb b/spec/tooling/danger/project_helper_spec.rb index 4cc5df385a5..6793a4b8de3 100644 --- a/spec/tooling/danger/project_helper_spec.rb +++ b/spec/tooling/danger/project_helper_spec.rb @@ -98,10 +98,11 @@ RSpec.describe Tooling::Danger::ProjectHelper do 'vendor/languages.yml' | [:backend] 'file_hooks/examples/' | [:backend] - 'Gemfile' | [:backend] - 'Gemfile.lock' | [:backend] - 'Rakefile' | [:backend] - 'FOO_VERSION' | [:backend] + 'Gemfile' | [:backend] + 'Gemfile.lock' | [:backend] + 'Gemfile.checksum' | [:backend] + 'Rakefile' | [:backend] + 'FOO_VERSION' | [:backend] 'scripts/glfm/bar.rb' | [:backend] 'scripts/glfm/bar.js' | [:frontend] diff --git a/spec/tooling/danger/specs_spec.rb b/spec/tooling/danger/specs_spec.rb index 6c1fbbb903d..d6aed86e7dc 100644 --- a/spec/tooling/danger/specs_spec.rb +++ b/spec/tooling/danger/specs_spec.rb @@ -13,7 +13,9 @@ 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(:fake_project_helper) { instance_double('Tooling::Danger::ProjectHelper') } + let(:filename) { 'spec/foo_spec.rb' } + let(:file_lines) do [ " describe 'foo' do", @@ -32,6 +34,7 @@ RSpec.describe Tooling::Danger::Specs 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']", @@ -42,31 +45,28 @@ RSpec.describe Tooling::Danger::Specs do ] 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 eq([])" + ] + matching_lines + 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 + allow(specs.helper).to receive(:changed_lines).with(filename).and_return(matching_lines) + allow(specs.project_helper).to receive(:file_lines).and_return(file_lines) end describe '#changed_specs_files' do @@ -105,30 +105,116 @@ RSpec.describe Tooling::Danger::Specs do end end - describe '#added_line_matching_match_with_array' do - let(:filename) { 'spec/foo_spec.rb' } + describe '#add_suggestions_for_match_with_array' do + let(:template) do + <<~MARKDOWN + ```suggestion + %<suggested_line>s + ``` + + If order of the result is not important, please consider using `match_array` to avoid flakiness. + MARKDOWN + end + + 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 } + ].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]) + end + + specs.add_suggestions_for_match_with_array(filename) + end + end + + describe '#add_suggestions_for_project_factory_usage' do + let(:template) do + <<~MARKDOWN + ```suggestion + %<suggested_line>s + ``` + + Project creations are very slow. Use `let_it_be`, `build` or `build_stubbed` if possible. + See [testing best practices](https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#optimize-factory-usage) + for background information and alternative options. + MARKDOWN + end + + let(:file_lines) do + [ + " let(:project) { create(:project) }", + " let_it_be(:project) { create(:project, :repository)", + " let!(:project) { create(:project) }", + " let(:var) { create(:project) }", + " let(:merge_request) { create(:merge_request, project: project)", + " context 'when merge request exists' do", + " it { is_expected.to be_success }", + " end", + " let!(:var) { create(:project) }", + " let(:project) { create(:thing) }", + " let(:project) { build(:project) }", + " let(:project) do", + " create(:project)", + " end", + " let(:project) { create(:project, :repository) }", + " str = 'let(:project) { create(:project) }'", + " let(:project) { create(:project_empty_repo) }", + " let(:project) { create(:forked_project_with_submodules) }", + " let(:project) { create(:project_with_design) }", + " let(:authorization) { create(:project_authorization) }" + ] + end + + let(:matching_lines) do + [ + "+ let(:should_not_error) { create(:project) }", + "+ let(:project) { create(:project) }", + "+ let!(:project) { create(:project) }", + "+ let(:var) { create(:project) }", + "+ let!(:var) { create(:project) }", + "+ let(:project) { create(:project, :repository) }", + "+ let(:project) { create(:project_empty_repo) }", + "+ let(:project) { create(:forked_project_with_submodules) }", + "+ let(:project) { create(:project_with_design) }" + ] + 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 eq([])" + "+ line which doesn't exist in the file and should not cause an error", + "+ let_it_be(:project) { create(:project, :repository)", + "+ let(:project) { create(:thing) }", + "+ let(:project) do", + "+ create(:project)", + "+ end", + "+ str = 'let(:project) { create(:project) }'", + "+ let(:authorization) { create(:project_authorization) }" ] + matching_lines end - before do - allow(specs.helper).to receive(:changed_lines).with(filename).and_return(changed_lines) - end + it 'adds suggestions at the correct lines', :aggregate_failures do + [ + { suggested_line: " let_it_be(:project) { create(:project) }", number: 1 }, + { suggested_line: " let_it_be(:project) { create(:project) }", number: 3 }, + { suggested_line: " let_it_be(:var) { create(:project) }", number: 4 }, + { suggested_line: " let_it_be(:var) { create(:project) }", number: 9 }, + { suggested_line: " let_it_be(:project) { create(:project, :repository) }", number: 15 }, + { suggested_line: " let_it_be(:project) { create(:project_empty_repo) }", number: 17 }, + { suggested_line: " let_it_be(:project) { create(:forked_project_with_submodules) }", number: 18 }, + { suggested_line: " let_it_be(:project) { create(:project_with_design) }", number: 19 } + ].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]) + end - it 'returns all lines using an array equality matcher' do - expect(specs.added_line_matching_match_with_array(filename)).to match_array(matching_lines) + specs.add_suggestions_for_project_factory_usage(filename) end end end diff --git a/spec/tooling/quality/test_level_spec.rb b/spec/tooling/quality/test_level_spec.rb index f4eea28b66f..6084dc194da 100644 --- a/spec/tooling/quality/test_level_spec.rb +++ b/spec/tooling/quality/test_level_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' require_relative '../../../tooling/quality/test_level' |