diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 10:34:06 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 10:34:06 +0000 |
commit | 859a6fb938bb9ee2a317c46dfa4fcc1af49608f0 (patch) | |
tree | d7f2700abe6b4ffcb2dcfc80631b2d87d0609239 /spec/rubocop/cop/rspec | |
parent | 446d496a6d000c73a304be52587cd9bbc7493136 (diff) | |
download | gitlab-ce-859a6fb938bb9ee2a317c46dfa4fcc1af49608f0.tar.gz |
Add latest changes from gitlab-org/gitlab@13-9-stable-eev13.9.0-rc42
Diffstat (limited to 'spec/rubocop/cop/rspec')
12 files changed, 127 insertions, 240 deletions
diff --git a/spec/rubocop/cop/rspec/any_instance_of_spec.rb b/spec/rubocop/cop/rspec/any_instance_of_spec.rb index 42bb7d196a1..e7675ded25e 100644 --- a/spec/rubocop/cop/rspec/any_instance_of_spec.rb +++ b/spec/rubocop/cop/rspec/any_instance_of_spec.rb @@ -5,59 +5,51 @@ require 'fast_spec_helper' require_relative '../../../../rubocop/cop/rspec/any_instance_of' RSpec.describe RuboCop::Cop::RSpec::AnyInstanceOf do - include CopHelper - subject(:cop) { described_class.new } context 'when calling allow_any_instance_of' do let(:source) do <<~SRC - allow_any_instance_of(User).to receive(:invalidate_issue_cache_counts) + allow_any_instance_of(User).to receive(:invalidate_issue_cache_counts) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `allow_any_instance_of` [...] SRC end let(:corrected_source) do <<~SRC - allow_next_instance_of(User) do |instance| - allow(instance).to receive(:invalidate_issue_cache_counts) - end + allow_next_instance_of(User) do |instance| + allow(instance).to receive(:invalidate_issue_cache_counts) + end SRC end - it 'registers an offence' do - inspect_source(source) - - expect(cop.offenses.size).to eq(1) - end + it 'registers an offense and corrects', :aggregate_failures do + expect_offense(source) - it 'can autocorrect the source' do - expect(autocorrect_source(source)).to eq(corrected_source) + expect_correction(corrected_source) end end context 'when calling expect_any_instance_of' do let(:source) do <<~SRC - expect_any_instance_of(User).to receive(:invalidate_issue_cache_counts).with(args).and_return(double) + expect_any_instance_of(User).to receive(:invalidate_issue_cache_counts).with(args).and_return(double) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `expect_any_instance_of` [...] SRC end let(:corrected_source) do <<~SRC - expect_next_instance_of(User) do |instance| - expect(instance).to receive(:invalidate_issue_cache_counts).with(args).and_return(double) - end + expect_next_instance_of(User) do |instance| + expect(instance).to receive(:invalidate_issue_cache_counts).with(args).and_return(double) + end SRC end - it 'registers an offence' do - inspect_source(source) - - expect(cop.offenses.size).to eq(1) - end + it 'registers an offense and corrects', :aggregate_failures do + expect_offense(source) - it 'can autocorrect the source' do - expect(autocorrect_source(source)).to eq(corrected_source) + expect_correction(corrected_source) end end end diff --git a/spec/rubocop/cop/rspec/be_success_matcher_spec.rb b/spec/rubocop/cop/rspec/be_success_matcher_spec.rb index d49507c89b1..050f0396fac 100644 --- a/spec/rubocop/cop/rspec/be_success_matcher_spec.rb +++ b/spec/rubocop/cop/rspec/be_success_matcher_spec.rb @@ -5,34 +5,27 @@ require 'rubocop' require_relative '../../../../rubocop/cop/rspec/be_success_matcher' RSpec.describe RuboCop::Cop::RSpec::BeSuccessMatcher do - include CopHelper - let(:source_file) { 'spec/foo_spec.rb' } subject(:cop) { described_class.new } shared_examples 'cop' do |good:, bad:| context "using #{bad} call" do - it 'registers an offense' 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]) - end - - it "autocorrects it to `#{good}`" do - autocorrected = autocorrect_source(bad, source_file) - - expect(autocorrected).to eql(good) + it 'registers an offense and corrects', :aggregate_failures do + expect_offense(<<~CODE, node: bad) + %{node} + ^{node} Do not use deprecated `success?` method, use `successful?` instead. + CODE + + expect_correction(<<~CODE) + #{good} + CODE end end context "using #{good} call" do it 'does not register an offense' do - inspect_source(good) - - expect(cop.offenses).to be_empty + expect_no_offenses(good) end end end diff --git a/spec/rubocop/cop/rspec/env_assignment_spec.rb b/spec/rubocop/cop/rspec/env_assignment_spec.rb index 07afd30fc90..cc132d1532a 100644 --- a/spec/rubocop/cop/rspec/env_assignment_spec.rb +++ b/spec/rubocop/cop/rspec/env_assignment_spec.rb @@ -3,13 +3,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' - require_relative '../../../../rubocop/cop/rspec/env_assignment' RSpec.describe RuboCop::Cop::RSpec::EnvAssignment do - include CopHelper - offense_call_single_quotes_key = %(ENV['FOO'] = 'bar').freeze offense_call_double_quotes_key = %(ENV["FOO"] = 'bar').freeze @@ -17,31 +13,24 @@ RSpec.describe RuboCop::Cop::RSpec::EnvAssignment do subject(:cop) { described_class.new } - shared_examples 'an offensive ENV#[]= call' do |content| - it "registers an offense for `#{content}`" do - inspect_source(content, source_file) - - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - expect(cop.highlights).to eq([content]) - end - end - - shared_examples 'an autocorrected ENV#[]= call' do |content, autocorrected_content| - it "registers an offense for `#{content}` and autocorrects it to `#{autocorrected_content}`" do - autocorrected = autocorrect_source(content, source_file) + shared_examples 'an offensive and correction ENV#[]= call' do |content, autocorrected_content| + it "registers an offense for `#{content}` and corrects", :aggregate_failures do + expect_offense(<<~CODE) + #{content} + ^^^^^^^^^^^^^^^^^^ Don't assign to ENV, use `stub_env` instead. + CODE - expect(autocorrected).to eql(autocorrected_content) + expect_correction(<<~CODE) + #{autocorrected_content} + CODE end end context 'with a key using single quotes' do - it_behaves_like 'an offensive ENV#[]= call', offense_call_single_quotes_key - it_behaves_like 'an autocorrected ENV#[]= call', offense_call_single_quotes_key, %(stub_env('FOO', 'bar')) + it_behaves_like 'an offensive and correction ENV#[]= call', offense_call_single_quotes_key, %(stub_env('FOO', 'bar')) end context 'with a key using double quotes' do - it_behaves_like 'an offensive ENV#[]= call', offense_call_double_quotes_key - it_behaves_like 'an autocorrected ENV#[]= call', offense_call_double_quotes_key, %(stub_env("FOO", 'bar')) + it_behaves_like 'an offensive and correction ENV#[]= call', offense_call_double_quotes_key, %(stub_env("FOO", 'bar')) end end diff --git a/spec/rubocop/cop/rspec/expect_gitlab_tracking_spec.rb b/spec/rubocop/cop/rspec/expect_gitlab_tracking_spec.rb index f7adc1373df..d1ce8d01e0b 100644 --- a/spec/rubocop/cop/rspec/expect_gitlab_tracking_spec.rb +++ b/spec/rubocop/cop/rspec/expect_gitlab_tracking_spec.rb @@ -2,13 +2,9 @@ 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 } @@ -36,29 +32,18 @@ RSpec.describe RuboCop::Cop::RSpec::ExpectGitlabTracking do 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 + expect_no_offenses(good) 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/) + it 'registers an offense' do + expect_offense(<<~CODE, node: bad) + %{node} + ^{node} Do not expect directly on `Gitlab::Tracking#event`[...] + CODE end end end diff --git a/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb b/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb index fe9cea47a43..8beec53375e 100644 --- a/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb +++ b/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb @@ -3,17 +3,13 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' - require_relative '../../../../rubocop/cop/rspec/factories_in_migration_specs' RSpec.describe RuboCop::Cop::RSpec::FactoriesInMigrationSpecs do - include CopHelper - subject(:cop) { described_class.new } shared_examples 'an offensive factory call' do |namespace| - %i[build build_list create create_list].each do |forbidden_method| + %i[build build_list create create_list attributes_for].each do |forbidden_method| namespaced_forbidden_method = "#{namespace}#{forbidden_method}(:user)" it "registers an offense for #{namespaced_forbidden_method}" do diff --git a/spec/rubocop/cop/rspec/factory_bot/inline_association_spec.rb b/spec/rubocop/cop/rspec/factory_bot/inline_association_spec.rb index 33fdaaee3c7..0e6af71ea3e 100644 --- a/spec/rubocop/cop/rspec/factory_bot/inline_association_spec.rb +++ b/spec/rubocop/cop/rspec/factory_bot/inline_association_spec.rb @@ -7,8 +7,6 @@ require 'rubocop' require_relative '../../../../../rubocop/cop/rspec/factory_bot/inline_association' RSpec.describe RuboCop::Cop::RSpec::FactoryBot::InlineAssociation do - include CopHelper - subject(:cop) { described_class.new } shared_examples 'offense' do |code_snippet, autocorrected| @@ -17,27 +15,31 @@ RSpec.describe RuboCop::Cop::RSpec::FactoryBot::InlineAssociation do let(:offense_marker) { '^' * code_snippet.size } let(:offense_msg) { msg(type) } let(:offense) { "#{offense_marker} #{offense_msg}" } - let(:pristine_source) { source.sub(offense, '') } let(:source) do <<~RUBY - FactoryBot.define do - factory :project do - attribute { #{code_snippet} } - #{offense} - end + FactoryBot.define do + factory :project do + attribute { #{code_snippet} } + #{offense} end + end RUBY end - it 'registers an offense' do - expect_offense(source) + let(:corrected_source) do + <<~RUBY + FactoryBot.define do + factory :project do + attribute { #{autocorrected} } + end + end + RUBY end - it 'autocorrects the source' do - corrected = autocorrect_source(pristine_source) + it 'registers an offense and corrects', :aggregate_failures do + expect_offense(source) - expect(corrected).not_to include(code_snippet) - expect(corrected).to include(autocorrected) + expect_correction(corrected_source) end end diff --git a/spec/rubocop/cop/rspec/have_gitlab_http_status_spec.rb b/spec/rubocop/cop/rspec/have_gitlab_http_status_spec.rb index f6040350dc0..c2d97c8992a 100644 --- a/spec/rubocop/cop/rspec/have_gitlab_http_status_spec.rb +++ b/spec/rubocop/cop/rspec/have_gitlab_http_status_spec.rb @@ -4,50 +4,42 @@ require 'fast_spec_helper' require 'rspec-parameterized' require 'rubocop' -require 'rubocop/rspec/support' - require_relative '../../../../rubocop/cop/rspec/have_gitlab_http_status' RSpec.describe RuboCop::Cop::RSpec::HaveGitlabHttpStatus do - include CopHelper - using RSpec::Parameterized::TableSyntax let(:source_file) { 'spec/foo_spec.rb' } subject(:cop) { described_class.new } - shared_examples 'offense' do |code| - it 'registers an offense' do - inspect_source(code, source_file) + shared_examples 'offense' do |bad, good| + it 'registers an offense', :aggregate_failures do + expect_offense(<<~CODE, node: bad) + %{node} + ^{node} [...] + CODE - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([1]) - expect(cop.highlights).to eq([code]) + expect_correction(<<~CODE) + #{good} + CODE end end shared_examples 'no offense' do |code| it 'does not register an offense' do - inspect_source(code) - - expect(cop.offenses).to be_empty - end - end - - shared_examples 'autocorrect' do |bad, good| - it 'autocorrects' do - autocorrected = autocorrect_source(bad, source_file) - - expect(autocorrected).to eql(good) + expect_no_offenses(code) end end - shared_examples 'no autocorrect' do |code| + shared_examples 'offense with no autocorrect' do |code| it 'does not autocorrect' do - autocorrected = autocorrect_source(code, source_file) + expect_offense(<<~CODE, node: code) + %{node} + ^{node} [...] + CODE - expect(autocorrected).to eql(code) + expect_no_corrections end end @@ -64,10 +56,8 @@ RSpec.describe RuboCop::Cop::RSpec::HaveGitlabHttpStatus do end with_them do - include_examples 'offense', params[:bad] + include_examples 'offense', params[:bad], params[:good] include_examples 'no offense', params[:good] - include_examples 'autocorrect', params[:bad], params[:good] - include_examples 'no autocorrect', params[:good] end end @@ -77,10 +67,8 @@ RSpec.describe RuboCop::Cop::RSpec::HaveGitlabHttpStatus do end with_them do - include_examples 'offense', params[:bad] - include_examples 'offense', params[:good] - include_examples 'autocorrect', params[:bad], params[:good] - include_examples 'no autocorrect', params[:good] + include_examples 'offense', params[:bad], params[:good] + include_examples 'offense with no autocorrect', params[:good] end end @@ -114,7 +102,6 @@ RSpec.describe RuboCop::Cop::RSpec::HaveGitlabHttpStatus do with_them do include_examples 'no offense', params[:code] - include_examples 'no autocorrect', params[:code] end end end diff --git a/spec/rubocop/cop/rspec/htt_party_basic_auth_spec.rb b/spec/rubocop/cop/rspec/htt_party_basic_auth_spec.rb index 6e9e436602c..eac6ceb3ddf 100644 --- a/spec/rubocop/cop/rspec/htt_party_basic_auth_spec.rb +++ b/spec/rubocop/cop/rspec/htt_party_basic_auth_spec.rb @@ -5,12 +5,10 @@ require 'fast_spec_helper' require_relative '../../../../rubocop/cop/rspec/httparty_basic_auth' RSpec.describe RuboCop::Cop::RSpec::HTTPartyBasicAuth do - include CopHelper - subject(:cop) { described_class.new } context 'when passing `basic_auth: { user: ... }`' do - it 'registers an offence' do + it 'registers an offense and corrects', :aggregate_failures do expect_offense(<<~SOURCE, 'spec/foo.rb') HTTParty.put( url, @@ -19,17 +17,19 @@ RSpec.describe RuboCop::Cop::RSpec::HTTPartyBasicAuth do body: body ) SOURCE - end - it 'can autocorrect the source' do - bad = 'HTTParty.put(url, basic_auth: { user: user, password: token })' - good = 'HTTParty.put(url, basic_auth: { username: user, password: token })' - expect(autocorrect_source(bad)).to eq(good) + expect_correction(<<~SOURCE) + HTTParty.put( + url, + basic_auth: { username: user, password: token }, + body: body + ) + SOURCE end end context 'when passing `basic_auth: { username: ... }`' do - it 'does not register an offence' do + it 'does not register an offense' do expect_no_offenses(<<~SOURCE, 'spec/frontend/fixtures/foo.rb') HTTParty.put( url, diff --git a/spec/rubocop/cop/rspec/modify_sidekiq_middleware_spec.rb b/spec/rubocop/cop/rspec/modify_sidekiq_middleware_spec.rb index d9e3ca5741c..ffabbae90dc 100644 --- a/spec/rubocop/cop/rspec/modify_sidekiq_middleware_spec.rb +++ b/spec/rubocop/cop/rspec/modify_sidekiq_middleware_spec.rb @@ -5,33 +5,20 @@ require 'rubocop' require_relative '../../../../rubocop/cop/rspec/modify_sidekiq_middleware' RSpec.describe RuboCop::Cop::RSpec::ModifySidekiqMiddleware do - include CopHelper - subject(:cop) { described_class.new } - let(:source) do - <<~SRC - Sidekiq::Testing.server_middleware do |chain| - chain.add(MyCustomMiddleware) - end - SRC - end - - let(:corrected) do - <<~SRC - with_sidekiq_server_middleware do |chain| - chain.add(MyCustomMiddleware) - end - SRC - end - - it 'registers an offence' do - inspect_source(source) - - expect(cop.offenses.size).to eq(1) - end - - it 'can autocorrect the source' do - expect(autocorrect_source(source)).to eq(corrected) + it 'registers an offense and corrects', :aggregate_failures do + expect_offense(<<~CODE) + Sidekiq::Testing.server_middleware do |chain| + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't modify global sidekiq middleware, [...] + chain.add(MyCustomMiddleware) + end + CODE + + expect_correction(<<~CODE) + with_sidekiq_server_middleware do |chain| + chain.add(MyCustomMiddleware) + end + CODE end end diff --git a/spec/rubocop/cop/rspec/timecop_freeze_spec.rb b/spec/rubocop/cop/rspec/timecop_freeze_spec.rb index b1cf82492e4..939623f8299 100644 --- a/spec/rubocop/cop/rspec/timecop_freeze_spec.rb +++ b/spec/rubocop/cop/rspec/timecop_freeze_spec.rb @@ -3,50 +3,29 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' - require_relative '../../../../rubocop/cop/rspec/timecop_freeze' RSpec.describe RuboCop::Cop::RSpec::TimecopFreeze do - include CopHelper - subject(:cop) { described_class.new } context 'when calling Timecop.freeze' do - let(:source) do - <<~SRC - Timecop.freeze(Time.current) { example.run } - SRC - end - - let(:corrected_source) do - <<~SRC - freeze_time(Time.current) { example.run } - SRC - end - - it 'registers an offence' do - inspect_source(source) - - expect(cop.offenses.size).to eq(1) - end - - it 'can autocorrect the source' do - expect(autocorrect_source(source)).to eq(corrected_source) + it 'registers an offense and corrects', :aggregate_failures do + expect_offense(<<~CODE) + Timecop.freeze(Time.current) { example.run } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `Timecop.freeze`, use `freeze_time` instead. [...] + CODE + + expect_correction(<<~CODE) + freeze_time(Time.current) { example.run } + CODE end end context 'when calling a different method on Timecop' do - let(:source) do - <<~SRC - Timecop.travel(Time.current) - SRC - end - - it 'does not register an offence' do - inspect_source(source) - - expect(cop.offenses).to be_empty + it 'does not register an offense' do + expect_no_offenses(<<~CODE) + Timecop.travel(Time.current) + CODE end end end diff --git a/spec/rubocop/cop/rspec/timecop_travel_spec.rb b/spec/rubocop/cop/rspec/timecop_travel_spec.rb index 2ee8bfe9ad7..476e45e69a6 100644 --- a/spec/rubocop/cop/rspec/timecop_travel_spec.rb +++ b/spec/rubocop/cop/rspec/timecop_travel_spec.rb @@ -3,50 +3,29 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' - require_relative '../../../../rubocop/cop/rspec/timecop_travel' RSpec.describe RuboCop::Cop::RSpec::TimecopTravel do - include CopHelper - subject(:cop) { described_class.new } context 'when calling Timecop.travel' do - let(:source) do - <<~SRC - Timecop.travel(1.day.ago) { create(:issue) } - SRC - end - - let(:corrected_source) do - <<~SRC - travel_to(1.day.ago) { create(:issue) } - SRC - end - - it 'registers an offence' do - inspect_source(source) - - expect(cop.offenses.size).to eq(1) - end - - it 'can autocorrect the source' do - expect(autocorrect_source(source)).to eq(corrected_source) + it 'registers an offense and corrects', :aggregate_failures do + expect_offense(<<~CODE) + Timecop.travel(1.day.ago) { create(:issue) } + ^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `Timecop.travel`, use `travel_to` instead. [...] + CODE + + expect_correction(<<~CODE) + travel_to(1.day.ago) { create(:issue) } + CODE end end context 'when calling a different method on Timecop' do - let(:source) do - <<~SRC - Timecop.freeze { create(:issue) } - SRC - end - - it 'does not register an offence' do - inspect_source(source) - - expect(cop.offenses).to be_empty + it 'does not register an offense' do + expect_no_offenses(<<~CODE) + Timecop.freeze { create(:issue) } + CODE end end end diff --git a/spec/rubocop/cop/rspec/top_level_describe_path_spec.rb b/spec/rubocop/cop/rspec/top_level_describe_path_spec.rb index 4936936836d..23531cd0201 100644 --- a/spec/rubocop/cop/rspec/top_level_describe_path_spec.rb +++ b/spec/rubocop/cop/rspec/top_level_describe_path_spec.rb @@ -5,8 +5,6 @@ require 'rubocop' require_relative '../../../../rubocop/cop/rspec/top_level_describe_path' RSpec.describe RuboCop::Cop::RSpec::TopLevelDescribePath do - include CopHelper - subject(:cop) { described_class.new } context 'when the file ends in _spec.rb' do |