diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-05 15:10:17 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-05 15:10:17 +0300 |
commit | 1c359370b3970c41dd8aa0c69fe177f39cb58298 (patch) | |
tree | 2ca6226176233e0340dcbf01d7134d5e0fa2faf5 /spec/rubocop/cop | |
parent | 26d28ba1597a9ce3018206475fa3dfdb42829656 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop/cop')
6 files changed, 231 insertions, 157 deletions
diff --git a/spec/rubocop/cop/qa/element_with_pattern_spec.rb b/spec/rubocop/cop/qa/element_with_pattern_spec.rb index 1febdaf9c3b..ccc03d7f5ae 100644 --- a/spec/rubocop/cop/qa/element_with_pattern_spec.rb +++ b/spec/rubocop/cop/qa/element_with_pattern_spec.rb @@ -40,7 +40,7 @@ RSpec.describe RuboCop::Cop::QA::ElementWithPattern do end end - context 'outside of a migration spec file' do + context 'when outside of a QA spec file' do it "does not register an offense" do expect_no_offenses(<<-RUBY) describe 'foo' do diff --git a/spec/rubocop/cop/rake/require_spec.rb b/spec/rubocop/cop/rake/require_spec.rb index bb8c6a1f063..de484643d6e 100644 --- a/spec/rubocop/cop/rake/require_spec.rb +++ b/spec/rubocop/cop/rake/require_spec.rb @@ -7,54 +7,84 @@ require_relative '../../../../rubocop/cop/rake/require' RSpec.describe RuboCop::Cop::Rake::Require do let(:msg) { described_class::MSG } - it 'registers an offenses for require methods' do - expect_offense(<<~RUBY) - require 'json' - ^^^^^^^^^^^^^^ #{msg} - require_relative 'gitlab/json' - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} - RUBY + describe '#in_rake_file?' do + context 'in a Rake file' do + let(:node) { double(location: double(expression: double(source_buffer: double(name: 'foo/bar.rake')))) } # rubocop:disable RSpec/VerifiedDoubles + + it { expect(subject.__send__(:in_rake_file?, node)).to be(true) } + end + + context 'when outside of a Rake file' do + let(:node) { double(location: double(expression: double(source_buffer: double(name: 'foo/bar.rb')))) } # rubocop:disable RSpec/VerifiedDoubles + + it { expect(subject.__send__(:in_rake_file?, node)).to be(false) } + end end - it 'does not register offense inside `task` definition' do - expect_no_offenses(<<~RUBY) - task :parse do + context 'in a Rake file' do + before do + allow(cop).to receive(:in_rake_file?).and_return(true) + end + + it 'registers an offenses for require methods' do + expect_offense(<<~RUBY) require 'json' - end + ^^^^^^^^^^^^^^ #{msg} + require_relative 'gitlab/json' + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + RUBY + end - namespace :some do - task parse: :env do - require_relative 'gitlab/json' + it 'does not register offense inside `task` definition' do + expect_no_offenses(<<~RUBY) + task :parse do + require 'json' end - end - RUBY - end - it 'does not register offense inside a block definition' do - expect_no_offenses(<<~RUBY) - RSpec::Core::RakeTask.new(:parse_json) do |t, args| - require 'json' - end - RUBY - end + namespace :some do + task parse: :env do + require_relative 'gitlab/json' + end + end + RUBY + end - it 'does not register offense inside a method definition' do - expect_no_offenses(<<~RUBY) - def load_deps - require 'json' - end + it 'does not register offense inside a block definition' do + expect_no_offenses(<<~RUBY) + RSpec::Core::RakeTask.new(:parse_json) do |t, args| + require 'json' + end + RUBY + end + + it 'does not register offense inside a method definition' do + expect_no_offenses(<<~RUBY) + def load_deps + require 'json' + end - task :parse do - load_deps - end - RUBY + task :parse do + load_deps + end + RUBY + end + + it 'does not register offense when require task related files' do + expect_no_offenses(<<~RUBY) + require 'rubocop/rake_tasks' + require 'gettext_i18n_rails/tasks' + require_relative '../../rubocop/check_graceful_task' + RUBY + end end - it 'does not register offense when require task related files' do - expect_no_offenses(<<~RUBY) - require 'rubocop/rake_tasks' - require 'gettext_i18n_rails/tasks' - require_relative '../../rubocop/check_graceful_task' - RUBY + context 'when outside of a Rake file' do + before do + allow(cop).to receive(:in_rake_file?).and_return(false) + end + + it 'registers an offenses for require methods' do + expect_no_offenses("require 'json'") + end end end diff --git a/spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb b/spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb index b4d113a9bcc..53bf5de6243 100644 --- a/spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb +++ b/spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb @@ -13,33 +13,45 @@ RSpec.describe RuboCop::Cop::UsageData::DistinctCountByLargeForeignKey do }) end - context 'when counting by disallowed key' do - it 'registers an offense' do - expect_offense(<<~CODE) - distinct_count(Issue, :creator_id) - ^^^^^^^^^^^^^^ #{msg} - CODE + context 'in an usage data file' do + before do + allow(cop).to receive(:in_usage_data_file?).and_return(true) end - it 'does not register an offense when batch is false' do - expect_no_offenses('distinct_count(Issue, :creator_id, batch: false)') + context 'when counting by disallowed key' do + it 'registers an offense' do + expect_offense(<<~CODE) + distinct_count(Issue, :creator_id) + ^^^^^^^^^^^^^^ #{msg} + CODE + end + + it 'does not register an offense when batch is false' do + expect_no_offenses('distinct_count(Issue, :creator_id, batch: false)') + end + + it 'registers an offense when batch is true' do + expect_offense(<<~CODE) + distinct_count(Issue, :creator_id, batch: true) + ^^^^^^^^^^^^^^ #{msg} + CODE + end end - it 'registers an offense when batch is true' do - expect_offense(<<~CODE) - distinct_count(Issue, :creator_id, batch: true) - ^^^^^^^^^^^^^^ #{msg} - CODE - end - end + context 'when calling by allowed key' do + it 'does not register an offense with symbol' do + expect_no_offenses('distinct_count(Issue, :author_id)') + end - context 'when calling by allowed key' do - it 'does not register an offense with symbol' do - expect_no_offenses('distinct_count(Issue, :author_id)') + it 'does not register an offense with string' do + expect_no_offenses("distinct_count(Issue, 'merge_requests.target_project_id')") + end end + end - it 'does not register an offense with string' do - expect_no_offenses("distinct_count(Issue, 'merge_requests.target_project_id')") + context 'when outside of an usage data file' do + it 'does not register an offense' do + expect_no_offenses('distinct_count(Issue, :creator_id)') end end end diff --git a/spec/rubocop/cop/usage_data/histogram_with_large_table_spec.rb b/spec/rubocop/cop/usage_data/histogram_with_large_table_spec.rb index efa4e27dc9c..0de14310e13 100644 --- a/spec/rubocop/cop/usage_data/histogram_with_large_table_spec.rb +++ b/spec/rubocop/cop/usage_data/histogram_with_large_table_spec.rb @@ -14,93 +14,105 @@ RSpec.describe RuboCop::Cop::UsageData::HistogramWithLargeTable do }) end - context 'with large tables' do - context 'with one-level constants' do - context 'when calling histogram(Issue)' do - it 'registers an offense' do - expect_offense(<<~CODE) - histogram(Issue, :project_id, buckets: 1..100) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Issue - CODE + context 'in an usage data file' do + before do + allow(cop).to receive(:in_usage_data_file?).and_return(true) + end + + context 'with large tables' do + context 'with one-level constants' do + context 'when calling histogram(Issue)' do + it 'registers an offense' do + expect_offense(<<~CODE) + histogram(Issue, :project_id, buckets: 1..100) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Issue + CODE + end end - end - context 'when calling histogram(::Issue)' do - it 'registers an offense' do - expect_offense(<<~CODE) - histogram(::Issue, :project_id, buckets: 1..100) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Issue - CODE + context 'when calling histogram(::Issue)' do + it 'registers an offense' do + expect_offense(<<~CODE) + histogram(::Issue, :project_id, buckets: 1..100) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Issue + CODE + end end - end - context 'when calling histogram(Issue.closed)' do - it 'registers an offense' do - expect_offense(<<~CODE) - histogram(Issue.closed, :project_id, buckets: 1..100) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Issue - CODE + context 'when calling histogram(Issue.closed)' do + it 'registers an offense' do + expect_offense(<<~CODE) + histogram(Issue.closed, :project_id, buckets: 1..100) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Issue + CODE + end end - end - context 'when calling histogram(::Issue.closed)' do - it 'registers an offense' do - expect_offense(<<~CODE) - histogram(::Issue.closed, :project_id, buckets: 1..100) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Issue - CODE + context 'when calling histogram(::Issue.closed)' do + it 'registers an offense' do + expect_offense(<<~CODE) + histogram(::Issue.closed, :project_id, buckets: 1..100) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Issue + CODE + end end end - end - context 'with two-level constants' do - context 'when calling histogram(::Ci::Build)' do - it 'registers an offense' do - expect_offense(<<~CODE) - histogram(::Ci::Build, buckets: 1..100) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Ci::Build - CODE + context 'with two-level constants' do + context 'when calling histogram(::Ci::Build)' do + it 'registers an offense' do + expect_offense(<<~CODE) + histogram(::Ci::Build, buckets: 1..100) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Ci::Build + CODE + end end - end - context 'when calling histogram(::Ci::Build.active)' do - it 'registers an offense' do - expect_offense(<<~CODE) - histogram(::Ci::Build.active, buckets: 1..100) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Ci::Build - CODE + context 'when calling histogram(::Ci::Build.active)' do + it 'registers an offense' do + expect_offense(<<~CODE) + histogram(::Ci::Build.active, buckets: 1..100) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Ci::Build + CODE + end end - end - context 'when calling histogram(Ci::Build)' do - it 'registers an offense' do - expect_offense(<<~CODE) - histogram(Ci::Build, buckets: 1..100) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Ci::Build - CODE + context 'when calling histogram(Ci::Build)' do + it 'registers an offense' do + expect_offense(<<~CODE) + histogram(Ci::Build, buckets: 1..100) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Ci::Build + CODE + end end - end - context 'when calling histogram(Ci::Build.active)' do - it 'registers an offense' do - expect_offense(<<~CODE) - histogram(Ci::Build.active, buckets: 1..100) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Ci::Build - CODE + context 'when calling histogram(Ci::Build.active)' do + it 'registers an offense' do + expect_offense(<<~CODE) + histogram(Ci::Build.active, buckets: 1..100) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} Ci::Build + CODE + end end end end - end - context 'with non related class' do - it 'does not register an offense' do - expect_no_offenses('histogram(MergeRequest, buckets: 1..100)') + context 'with non related class' do + it 'does not register an offense' do + expect_no_offenses('histogram(MergeRequest, buckets: 1..100)') + end + end + + context 'with non related method' do + it 'does not register an offense' do + expect_no_offenses('count(Issue, buckets: 1..100)') + end end end - context 'with non related method' do + context 'when outside of an usage data file' do it 'does not register an offense' do - expect_no_offenses('count(Issue, buckets: 1..100)') + expect_no_offenses('histogram(Issue, :project_id, buckets: 1..100)') end end end diff --git a/spec/rubocop/cop/usage_data/instrumentation_superclass_spec.rb b/spec/rubocop/cop/usage_data/instrumentation_superclass_spec.rb index a55f0852f35..f208a5451cb 100644 --- a/spec/rubocop/cop/usage_data/instrumentation_superclass_spec.rb +++ b/spec/rubocop/cop/usage_data/instrumentation_superclass_spec.rb @@ -14,49 +14,63 @@ RSpec.describe RuboCop::Cop::UsageData::InstrumentationSuperclass do }) end - context 'with class definition' do - context 'when inheriting from allowed superclass' do - it 'does not register an offense' do - expect_no_offenses('class NewMetric < GenericMetric; end') - end + context 'when in an instrumentation file' do + before do + allow(cop).to receive(:in_instrumentation_file?).and_return(true) end - context 'when inheriting from some other superclass' do - it 'registers an offense' do - expect_offense(<<~CODE) - class NewMetric < BaseMetric; end - ^^^^^^^^^^ #{msg} - CODE + context 'with class definition' do + context 'when inheriting from allowed superclass' do + it 'does not register an offense' do + expect_no_offenses('class NewMetric < GenericMetric; end') + end end - end - context 'when not inheriting' do - it 'does not register an offense' do - expect_no_offenses('class NewMetric; end') + context 'when inheriting from some other superclass' do + it 'registers an offense' do + expect_offense(<<~CODE) + class NewMetric < BaseMetric; end + ^^^^^^^^^^ #{msg} + CODE + end end - end - end - context 'with dynamic class definition' do - context 'when inheriting from allowed superclass' do - it 'does not register an offense' do - expect_no_offenses('NewMetric = Class.new(GenericMetric)') + context 'when not inheriting' do + it 'does not register an offense' do + expect_no_offenses('class NewMetric; end') + end end end - context 'when inheriting from some other superclass' do - it 'registers an offense' do - expect_offense(<<~CODE) - NewMetric = Class.new(BaseMetric) - ^^^^^^^^^^ #{msg} - CODE + context 'with dynamic class definition' do + context 'when inheriting from allowed superclass' do + it 'does not register an offense' do + expect_no_offenses('NewMetric = Class.new(GenericMetric)') + end end - end - context 'when not inheriting' do - it 'does not register an offense' do - expect_no_offenses('NewMetric = Class.new') + context 'when inheriting from some other superclass' do + it 'registers an offense' do + expect_offense(<<~CODE) + NewMetric = Class.new(BaseMetric) + ^^^^^^^^^^ #{msg} + CODE + end end + + context 'when not inheriting' do + it 'does not register an offense' do + expect_no_offenses('NewMetric = Class.new') + end + end + end + end + + context 'when outside of an instrumentation file' do + it "does not register an offense" do + expect_no_offenses(<<-RUBY) + class NewMetric < BaseMetric; end + RUBY end end end diff --git a/spec/rubocop/cop/usage_data/large_table_spec.rb b/spec/rubocop/cop/usage_data/large_table_spec.rb index fa94f878cea..ceeb1143690 100644 --- a/spec/rubocop/cop/usage_data/large_table_spec.rb +++ b/spec/rubocop/cop/usage_data/large_table_spec.rb @@ -18,9 +18,9 @@ RSpec.describe RuboCop::Cop::UsageData::LargeTable do }) end - context 'when in usage_data files' do + context 'in an usage data file' do before do - allow(cop).to receive(:usage_data_files?).and_return(true) + allow(cop).to receive(:in_usage_data_file?).and_return(true) end context 'with large tables' do @@ -76,4 +76,10 @@ RSpec.describe RuboCop::Cop::UsageData::LargeTable do end end end + + context 'when outside of an usage data file' do + it 'does not register an offense' do + expect_no_offenses('Issue.active.count') + end + end end |