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/gitlab | |
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/gitlab')
-rw-r--r-- | spec/rubocop/cop/gitlab/avoid_uploaded_file_from_params_spec.rb | 9 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/bulk_insert_spec.rb | 11 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/change_timezone_spec.rb | 5 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/const_get_inherit_false_spec.rb | 47 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/duplicate_spec_location_spec.rb | 2 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/except_spec.rb | 3 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/finder_with_find_by_spec.rb | 49 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/httparty_spec.rb | 34 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/intersect_spec.rb | 3 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/json_spec.rb | 33 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/module_with_instance_variables_spec.rb | 35 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/namespaced_class_spec.rb | 73 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/policy_rule_boolean_spec.rb | 3 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/predicate_memoization_spec.rb | 74 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/rails_logger_spec.rb | 22 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/union_spec.rb | 3 |
16 files changed, 191 insertions, 215 deletions
diff --git a/spec/rubocop/cop/gitlab/avoid_uploaded_file_from_params_spec.rb b/spec/rubocop/cop/gitlab/avoid_uploaded_file_from_params_spec.rb index 2db03898e01..f96e25c59e7 100644 --- a/spec/rubocop/cop/gitlab/avoid_uploaded_file_from_params_spec.rb +++ b/spec/rubocop/cop/gitlab/avoid_uploaded_file_from_params_spec.rb @@ -2,19 +2,16 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/avoid_uploaded_file_from_params' RSpec.describe RuboCop::Cop::Gitlab::AvoidUploadedFileFromParams do - include CopHelper - subject(:cop) { described_class.new } - context 'UploadedFile.from_params' do + context 'when using UploadedFile.from_params' do it 'flags its call' do expect_offense(<<~SOURCE) - UploadedFile.from_params(params) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the `UploadedFile` set by `multipart.rb` instead of calling `UploadedFile.from_params` directly. See https://docs.gitlab.com/ee/development/uploads.html#how-to-add-a-new-upload-route + UploadedFile.from_params(params) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the `UploadedFile` set by `multipart.rb` instead of calling [...] SOURCE end end diff --git a/spec/rubocop/cop/gitlab/bulk_insert_spec.rb b/spec/rubocop/cop/gitlab/bulk_insert_spec.rb index ad7e685e505..c280ab8fa8b 100644 --- a/spec/rubocop/cop/gitlab/bulk_insert_spec.rb +++ b/spec/rubocop/cop/gitlab/bulk_insert_spec.rb @@ -2,25 +2,22 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/bulk_insert' RSpec.describe RuboCop::Cop::Gitlab::BulkInsert do - include CopHelper - subject(:cop) { described_class.new } it 'flags the use of Gitlab::Database.bulk_insert' do expect_offense(<<~SOURCE) - Gitlab::Database.bulk_insert('merge_request_diff_files', rows) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{RuboCop::Cop::Gitlab::BulkInsert::MSG} + Gitlab::Database.bulk_insert('merge_request_diff_files', rows) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the `BulkInsertSafe` concern, [...] SOURCE end it 'flags the use of ::Gitlab::Database.bulk_insert' do expect_offense(<<~SOURCE) - ::Gitlab::Database.bulk_insert('merge_request_diff_files', rows) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{RuboCop::Cop::Gitlab::BulkInsert::MSG} + ::Gitlab::Database.bulk_insert('merge_request_diff_files', rows) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the `BulkInsertSafe` concern, [...] SOURCE end end diff --git a/spec/rubocop/cop/gitlab/change_timezone_spec.rb b/spec/rubocop/cop/gitlab/change_timezone_spec.rb index 6abbc06bb1a..9cb822ec4f2 100644 --- a/spec/rubocop/cop/gitlab/change_timezone_spec.rb +++ b/spec/rubocop/cop/gitlab/change_timezone_spec.rb @@ -2,19 +2,16 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/change_timzone' RSpec.describe RuboCop::Cop::Gitlab::ChangeTimezone do - include CopHelper - subject(:cop) { described_class.new } context 'Time.zone=' do it 'registers an offense with no 2nd argument' do expect_offense(<<~PATTERN) Time.zone = 'Awkland' - ^^^^^^^^^^^^^^^^^^^^^ Do not change timezone in the runtime (application or rspec), it could result in silently modifying other behavior. + ^^^^^^^^^^^^^^^^^^^^^ Do not change timezone in the runtime (application or rspec), it could result [...] PATTERN end end diff --git a/spec/rubocop/cop/gitlab/const_get_inherit_false_spec.rb b/spec/rubocop/cop/gitlab/const_get_inherit_false_spec.rb index bed06ab2b17..19e5fe946be 100644 --- a/spec/rubocop/cop/gitlab/const_get_inherit_false_spec.rb +++ b/spec/rubocop/cop/gitlab/const_get_inherit_false_spec.rb @@ -2,78 +2,75 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/const_get_inherit_false' RSpec.describe RuboCop::Cop::Gitlab::ConstGetInheritFalse do - include CopHelper - subject(:cop) { described_class.new } context 'Object.const_get' do - it 'registers an offense with no 2nd argument' do + it 'registers an offense with no 2nd argument and corrects' do expect_offense(<<~PATTERN) Object.const_get(:CONSTANT) ^^^^^^^^^ Use inherit=false when using const_get. PATTERN - end - it 'autocorrects' do - expect(autocorrect_source('Object.const_get(:CONSTANT)')).to eq('Object.const_get(:CONSTANT, false)') + expect_correction(<<~PATTERN) + Object.const_get(:CONSTANT, false) + PATTERN end context 'inherit=false' do it 'does not register an offense' do expect_no_offenses(<<~PATTERN) - Object.const_get(:CONSTANT, false) + Object.const_get(:CONSTANT, false) PATTERN end end context 'inherit=true' do - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<~PATTERN) - Object.const_get(:CONSTANT, true) - ^^^^^^^^^ Use inherit=false when using const_get. + Object.const_get(:CONSTANT, true) + ^^^^^^^^^ Use inherit=false when using const_get. PATTERN - end - it 'autocorrects' do - expect(autocorrect_source('Object.const_get(:CONSTANT, true)')).to eq('Object.const_get(:CONSTANT, false)') + expect_correction(<<~PATTERN) + Object.const_get(:CONSTANT, false) + PATTERN end end end context 'const_get for a nested class' do - it 'registers an offense on reload usage' do + it 'registers an offense on reload usage and corrects' do expect_offense(<<~PATTERN) Nested::Blog.const_get(:CONSTANT) ^^^^^^^^^ Use inherit=false when using const_get. PATTERN - end - it 'autocorrects' do - expect(autocorrect_source('Nested::Blag.const_get(:CONSTANT)')).to eq('Nested::Blag.const_get(:CONSTANT, false)') + expect_correction(<<~PATTERN) + Nested::Blog.const_get(:CONSTANT, false) + PATTERN end context 'inherit=false' do it 'does not register an offense' do expect_no_offenses(<<~PATTERN) - Nested::Blog.const_get(:CONSTANT, false) + Nested::Blog.const_get(:CONSTANT, false) PATTERN end end context 'inherit=true' do - it 'registers an offense if inherit is true' do + it 'registers an offense if inherit is true and corrects' do expect_offense(<<~PATTERN) - Nested::Blog.const_get(:CONSTANT, true) - ^^^^^^^^^ Use inherit=false when using const_get. + Nested::Blog.const_get(:CONSTANT, true) + ^^^^^^^^^ Use inherit=false when using const_get. PATTERN - end - it 'autocorrects' do - expect(autocorrect_source('Nested::Blag.const_get(:CONSTANT, true)')).to eq('Nested::Blag.const_get(:CONSTANT, false)') + expect_correction(<<~PATTERN) + Nested::Blog.const_get(:CONSTANT, false) + PATTERN end end end diff --git a/spec/rubocop/cop/gitlab/duplicate_spec_location_spec.rb b/spec/rubocop/cop/gitlab/duplicate_spec_location_spec.rb index 5804b03b641..a207155f432 100644 --- a/spec/rubocop/cop/gitlab/duplicate_spec_location_spec.rb +++ b/spec/rubocop/cop/gitlab/duplicate_spec_location_spec.rb @@ -6,8 +6,6 @@ require 'rubocop' require_relative '../../../../rubocop/cop/gitlab/duplicate_spec_location' RSpec.describe RuboCop::Cop::Gitlab::DuplicateSpecLocation do - include CopHelper - subject(:cop) { described_class.new } let(:rails_root) { '../../../../' } diff --git a/spec/rubocop/cop/gitlab/except_spec.rb b/spec/rubocop/cop/gitlab/except_spec.rb index 173e5943da5..7a122e3cf53 100644 --- a/spec/rubocop/cop/gitlab/except_spec.rb +++ b/spec/rubocop/cop/gitlab/except_spec.rb @@ -2,12 +2,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/except' RSpec.describe RuboCop::Cop::Gitlab::Except do - include CopHelper - subject(:cop) { described_class.new } it 'flags the use of Gitlab::SQL::Except.new' do diff --git a/spec/rubocop/cop/gitlab/finder_with_find_by_spec.rb b/spec/rubocop/cop/gitlab/finder_with_find_by_spec.rb index db3bcf1dfdb..03d7fc5e8b1 100644 --- a/spec/rubocop/cop/gitlab/finder_with_find_by_spec.rb +++ b/spec/rubocop/cop/gitlab/finder_with_find_by_spec.rb @@ -1,46 +1,31 @@ # frozen_string_literal: true require 'fast_spec_helper' - require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/finder_with_find_by' RSpec.describe RuboCop::Cop::Gitlab::FinderWithFindBy do - include CopHelper - subject(:cop) { described_class.new } context 'when calling execute.find' do - let(:source) do - <<~SRC - DummyFinder.new(some_args) - .execute - .find_by!(1) - SRC - end - - let(:corrected_source) do - <<~SRC - DummyFinder.new(some_args) - .find_by!(1) - 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' do + expect_offense(<<~CODE) + DummyFinder.new(some_args) + .execute + .find_by!(1) + ^^^^^^^^ Don't chain finders `#execute` method with [...] + CODE + + expect_correction(<<~CODE) + DummyFinder.new(some_args) + .find_by!(1) + CODE end context 'when called within the `FinderMethods` module' do - let(:source) do - <<~SRC + it 'does not register an offense' do + expect_no_offenses(<<~SRC) module FinderMethods def find_by!(*args) execute.find_by!(args) @@ -48,12 +33,6 @@ RSpec.describe RuboCop::Cop::Gitlab::FinderWithFindBy do end SRC end - - it 'does not register an offence' do - inspect_source(source) - - expect(cop.offenses).to be_empty - end end end end diff --git a/spec/rubocop/cop/gitlab/httparty_spec.rb b/spec/rubocop/cop/gitlab/httparty_spec.rb index b112ac84bff..fcd18b0eb9b 100644 --- a/spec/rubocop/cop/gitlab/httparty_spec.rb +++ b/spec/rubocop/cop/gitlab/httparty_spec.rb @@ -2,46 +2,30 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/httparty' RSpec.describe RuboCop::Cop::Gitlab::HTTParty do # rubocop:disable RSpec/FilePath - include CopHelper - subject(:cop) { described_class.new } - shared_examples('registering include offense') do |options| - let(:offending_lines) { options[:offending_lines] } - + shared_examples('registering include offense') do it 'registers an offense when the class includes HTTParty' do - inspect_source(source) - - aggregate_failures do - expect(cop.offenses.size).to eq(offending_lines.size) - expect(cop.offenses.map(&:line)).to eq(offending_lines) - end + expect_offense(source) end end - shared_examples('registering call offense') do |options| - let(:offending_lines) { options[:offending_lines] } - + shared_examples('registering call offense') do it 'registers an offense when the class calls HTTParty' do - inspect_source(source) - - aggregate_failures do - expect(cop.offenses.size).to eq(offending_lines.size) - expect(cop.offenses.map(&:line)).to eq(offending_lines) - end + expect_offense(source) end end context 'when source is a regular module' do - it_behaves_like 'registering include offense', offending_lines: [2] do + it_behaves_like 'registering include offense' do let(:source) do <<~RUBY module M include HTTParty + ^^^^^^^^^^^^^^^^ Avoid including `HTTParty` directly. [...] end RUBY end @@ -49,11 +33,12 @@ RSpec.describe RuboCop::Cop::Gitlab::HTTParty do # rubocop:disable RSpec/FilePat end context 'when source is a regular class' do - it_behaves_like 'registering include offense', offending_lines: [2] do + it_behaves_like 'registering include offense' do let(:source) do <<~RUBY class Foo include HTTParty + ^^^^^^^^^^^^^^^^ Avoid including `HTTParty` directly. [...] end RUBY end @@ -61,12 +46,13 @@ RSpec.describe RuboCop::Cop::Gitlab::HTTParty do # rubocop:disable RSpec/FilePat end context 'when HTTParty is called' do - it_behaves_like 'registering call offense', offending_lines: [3] do + it_behaves_like 'registering call offense' do let(:source) do <<~RUBY class Foo def bar HTTParty.get('http://example.com') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid calling `HTTParty` directly. [...] end end RUBY diff --git a/spec/rubocop/cop/gitlab/intersect_spec.rb b/spec/rubocop/cop/gitlab/intersect_spec.rb index e724f47029c..6f0367591cd 100644 --- a/spec/rubocop/cop/gitlab/intersect_spec.rb +++ b/spec/rubocop/cop/gitlab/intersect_spec.rb @@ -2,12 +2,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/intersect' RSpec.describe RuboCop::Cop::Gitlab::Intersect do - include CopHelper - subject(:cop) { described_class.new } it 'flags the use of Gitlab::SQL::Intersect.new' do diff --git a/spec/rubocop/cop/gitlab/json_spec.rb b/spec/rubocop/cop/gitlab/json_spec.rb index fc25f69a244..29c3b96cc1a 100644 --- a/spec/rubocop/cop/gitlab/json_spec.rb +++ b/spec/rubocop/cop/gitlab/json_spec.rb @@ -2,38 +2,21 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/json' RSpec.describe RuboCop::Cop::Gitlab::Json do - include CopHelper - subject(:cop) { described_class.new } - shared_examples('registering call offense') do |options| - let(:offending_lines) { options[:offending_lines] } - - it 'registers an offense when the class calls JSON' do - inspect_source(source) - - aggregate_failures do - expect(cop.offenses.size).to eq(offending_lines.size) - expect(cop.offenses.map(&:line)).to eq(offending_lines) - end - end - end - context 'when JSON is called' do - it_behaves_like 'registering call offense', offending_lines: [3] do - let(:source) do - <<~RUBY - class Foo - def bar - JSON.parse('{ "foo": "bar" }') - end + it 'registers an offense' do + expect_offense(<<~RUBY) + class Foo + def bar + JSON.parse('{ "foo": "bar" }') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid calling `JSON` directly. [...] end - RUBY - end + end + RUBY end end end diff --git a/spec/rubocop/cop/gitlab/module_with_instance_variables_spec.rb b/spec/rubocop/cop/gitlab/module_with_instance_variables_spec.rb index 1d09c720bf7..08634d5753a 100644 --- a/spec/rubocop/cop/gitlab/module_with_instance_variables_spec.rb +++ b/spec/rubocop/cop/gitlab/module_with_instance_variables_spec.rb @@ -2,42 +2,33 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/module_with_instance_variables' RSpec.describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do - include CopHelper + let(:msg) { "Do not use instance variables in a module. [...]" } subject(:cop) { described_class.new } - shared_examples('registering offense') do |options| - let(:offending_lines) { options[:offending_lines] } - + shared_examples('registering offense') do it 'registers an offense when instance variable is used in a module' do - inspect_source(source) - - aggregate_failures do - expect(cop.offenses.size).to eq(offending_lines.size) - expect(cop.offenses.map(&:line)).to eq(offending_lines) - end + expect_offense(source) end end shared_examples('not registering offense') do it 'does not register offenses' do - inspect_source(source) - - expect(cop.offenses).to be_empty + expect_no_offenses(source) end end context 'when source is a regular module' do - it_behaves_like 'registering offense', offending_lines: [3] do + it_behaves_like 'registering offense' do let(:source) do <<~RUBY module M def f @f = true + ^^^^^^^^^ #{msg} end end RUBY @@ -46,13 +37,14 @@ RSpec.describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do end context 'when source is a nested module' do - it_behaves_like 'registering offense', offending_lines: [4] do + it_behaves_like 'registering offense' do let(:source) do <<~RUBY module N module M def f @f = true + ^^^^^^^^^ #{msg} end end end @@ -62,13 +54,14 @@ RSpec.describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do end context 'when source is a nested module with multiple offenses' do - it_behaves_like 'registering offense', offending_lines: [4, 12] do + it_behaves_like 'registering offense' do let(:source) do <<~RUBY module N module M def f @f = true + ^^^^^^^^^ #{msg} end def g @@ -77,6 +70,7 @@ RSpec.describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do def h @h = true + ^^^^^^^^^ #{msg} end end end @@ -129,12 +123,13 @@ RSpec.describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do end context 'when source is using simple or ivar assignment with other ivar' do - it_behaves_like 'registering offense', offending_lines: [3] do + it_behaves_like 'registering offense' do let(:source) do <<~RUBY module M def f @f ||= g(@g) + ^^ #{msg} end end RUBY @@ -143,13 +138,15 @@ RSpec.describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do end context 'when source is using or ivar assignment with something else' do - it_behaves_like 'registering offense', offending_lines: [3, 4] do + it_behaves_like 'registering offense' do let(:source) do <<~RUBY module M def f @f ||= true + ^^ #{msg} @f.to_s + ^^ #{msg} end end RUBY diff --git a/spec/rubocop/cop/gitlab/namespaced_class_spec.rb b/spec/rubocop/cop/gitlab/namespaced_class_spec.rb new file mode 100644 index 00000000000..d1f61aa5afb --- /dev/null +++ b/spec/rubocop/cop/gitlab/namespaced_class_spec.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'rubocop' +require 'rubocop/rspec/support' +require_relative '../../../../rubocop/cop/gitlab/namespaced_class' + +RSpec.describe RuboCop::Cop::Gitlab::NamespacedClass do + subject(:cop) { described_class.new } + + it 'flags a class definition without namespace' do + expect_offense(<<~SOURCE) + class MyClass + ^^^^^^^^^^^^^ #{described_class::MSG} + end + SOURCE + end + + it 'flags a class definition with inheritance without namespace' do + expect_offense(<<~SOURCE) + class MyClass < ApplicationRecord + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG} + def some_method + true + end + end + SOURCE + end + + it 'does not flag the class definition with namespace in separate lines' do + expect_no_offenses(<<~SOURCE) + module MyModule + class MyClass < ApplicationRecord + end + + class MyOtherClass + def other_method + 1 + 1 + end + end + end + SOURCE + end + + it 'does not flag the class definition with nested namespace in separate lines' do + expect_no_offenses(<<~SOURCE) + module TopLevelModule + module NestedModule + class MyClass + end + end + end + SOURCE + end + + it 'does not flag the class definition nested inside namespaced class' do + expect_no_offenses(<<~SOURCE) + module TopLevelModule + class TopLevelClass + class MyClass + end + end + end + SOURCE + end + + it 'does not flag a compact namespaced class definition' do + expect_no_offenses(<<~SOURCE) + class MyModule::MyClass < ApplicationRecord + end + SOURCE + end +end diff --git a/spec/rubocop/cop/gitlab/policy_rule_boolean_spec.rb b/spec/rubocop/cop/gitlab/policy_rule_boolean_spec.rb index e6fb9ab9d57..6dbbcdd8324 100644 --- a/spec/rubocop/cop/gitlab/policy_rule_boolean_spec.rb +++ b/spec/rubocop/cop/gitlab/policy_rule_boolean_spec.rb @@ -2,12 +2,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/policy_rule_boolean' RSpec.describe RuboCop::Cop::Gitlab::PolicyRuleBoolean do - include CopHelper - subject(:cop) { described_class.new } it 'registers offense for &&' do diff --git a/spec/rubocop/cop/gitlab/predicate_memoization_spec.rb b/spec/rubocop/cop/gitlab/predicate_memoization_spec.rb index 322c7c82968..071ddcf8b7d 100644 --- a/spec/rubocop/cop/gitlab/predicate_memoization_spec.rb +++ b/spec/rubocop/cop/gitlab/predicate_memoization_spec.rb @@ -2,55 +2,38 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/predicate_memoization' RSpec.describe RuboCop::Cop::Gitlab::PredicateMemoization do - include CopHelper - subject(:cop) { described_class.new } - shared_examples('registering offense') do |options| - let(:offending_lines) { options[:offending_lines] } - - it 'registers an offense when a predicate method is memoizing via ivar' do - inspect_source(source) - - aggregate_failures do - expect(cop.offenses.size).to eq(offending_lines.size) - expect(cop.offenses.map(&:line)).to eq(offending_lines) - end - end - end - shared_examples('not registering offense') do it 'does not register offenses' do - inspect_source(source) - - expect(cop.offenses).to be_empty + expect_no_offenses(source) end end - context 'when source is a predicate method memoizing via ivar' do - it_behaves_like 'registering offense', offending_lines: [3] do + context 'when source is a predicate method using ivar with assignment' do + it_behaves_like 'not registering offense' do let(:source) do <<~RUBY class C def really? - @really ||= true + @really = true end end RUBY end end + end - it_behaves_like 'registering offense', offending_lines: [4] do + context 'when source is a predicate method using local with ||=' do + it_behaves_like 'not registering offense' do let(:source) do <<~RUBY class C def really? - value = true - @really ||= value + really ||= true end end RUBY @@ -58,13 +41,13 @@ RSpec.describe RuboCop::Cop::Gitlab::PredicateMemoization do end end - context 'when source is a predicate method using ivar with assignment' do + context 'when source is a regular method memoizing via ivar' do it_behaves_like 'not registering offense' do let(:source) do <<~RUBY class C - def really? - @really = true + def really + @really ||= true end end RUBY @@ -72,30 +55,37 @@ RSpec.describe RuboCop::Cop::Gitlab::PredicateMemoization do end end - context 'when source is a predicate method using local with ||=' do - it_behaves_like 'not registering offense' do - let(:source) do - <<~RUBY + context 'when source is a predicate method memoizing via ivar' do + let(:msg) { "Avoid using `@value ||= query` [...]" } + + context 'when assigning to boolean' do + it 'registers an offense' do + node = "@really ||= true" + + expect_offense(<<~CODE, node: node, msg: msg) class C def really? - really ||= true + %{node} + ^{node} %{msg} end end - RUBY + CODE end end - end - context 'when source is a regular method memoizing via ivar' do - it_behaves_like 'not registering offense' do - let(:source) do - <<~RUBY + context 'when assigning to another variable that is a boolean' do + it 'registers an offense' do + node = "@really ||= value" + + expect_offense(<<~CODE, node: node, msg: msg) class C - def really - @really ||= true + def really? + value = true + %{node} + ^{node} %{msg} end end - RUBY + CODE end end end diff --git a/spec/rubocop/cop/gitlab/rails_logger_spec.rb b/spec/rubocop/cop/gitlab/rails_logger_spec.rb index 768da243b02..7258b047191 100644 --- a/spec/rubocop/cop/gitlab/rails_logger_spec.rb +++ b/spec/rubocop/cop/gitlab/rails_logger_spec.rb @@ -2,37 +2,31 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/rails_logger' RSpec.describe RuboCop::Cop::Gitlab::RailsLogger do - include CopHelper - subject(:cop) { described_class.new } described_class::LOG_METHODS.each do |method| it "flags the use of Rails.logger.#{method} with a constant receiver" do - inspect_source("Rails.logger.#{method}('some error')") + node = "Rails.logger.#{method}('some error')" - expect(cop.offenses.size).to eq(1) + expect_offense(<<~CODE, node: node, msg: "Use a structured JSON logger instead of `Rails.logger`. [...]") + %{node} + ^{node} %{msg} + CODE end end it 'does not flag the use of Rails.logger with a constant that is not Rails' do - inspect_source("AppLogger.error('some error')") - - expect(cop.offenses.size).to eq(0) + expect_no_offenses("AppLogger.error('some error')") end it 'does not flag the use of logger with a send receiver' do - inspect_source("file_logger.info('important info')") - - expect(cop.offenses.size).to eq(0) + expect_no_offenses("file_logger.info('important info')") end it 'does not flag the use of Rails.logger.level' do - inspect_source("Rails.logger.level") - - expect(cop.offenses.size).to eq(0) + expect_no_offenses("Rails.logger.level") end end diff --git a/spec/rubocop/cop/gitlab/union_spec.rb b/spec/rubocop/cop/gitlab/union_spec.rb index 20364b1b901..04a3db8e7dd 100644 --- a/spec/rubocop/cop/gitlab/union_spec.rb +++ b/spec/rubocop/cop/gitlab/union_spec.rb @@ -2,12 +2,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/union' RSpec.describe RuboCop::Cop::Gitlab::Union do - include CopHelper - subject(:cop) { described_class.new } it 'flags the use of Gitlab::SQL::Union.new' do |