diff options
Diffstat (limited to 'spec/rubocop/cop')
11 files changed, 270 insertions, 29 deletions
diff --git a/spec/rubocop/cop/avoid_becomes_spec.rb b/spec/rubocop/cop/avoid_becomes_spec.rb new file mode 100644 index 00000000000..3e3e3abc27d --- /dev/null +++ b/spec/rubocop/cop/avoid_becomes_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'rubocop' +require 'rubocop/rspec/support' +require_relative '../../../rubocop/cop/avoid_becomes' + +RSpec.describe RuboCop::Cop::AvoidBecomes, type: :rubocop do + include CopHelper + + subject(:cop) { described_class.new } + + it 'flags the use of becomes with a constant parameter' do + inspect_source('foo.becomes(Project)') + + expect(cop.offenses.size).to eq(1) + end + + it 'flags the use of becomes with a namespaced constant parameter' do + inspect_source('foo.becomes(Namespace::Group)') + + expect(cop.offenses.size).to eq(1) + end + + it 'flags the use of becomes with a dynamic parameter' do + inspect_source(<<~RUBY) + model = Namespace + project = Project.first + project.becomes(model) + RUBY + + expect(cop.offenses.size).to eq(1) + end +end diff --git a/spec/rubocop/cop/avoid_break_from_strong_memoize_spec.rb b/spec/rubocop/cop/avoid_break_from_strong_memoize_spec.rb index bc9db9cafec..4fb47e758bb 100644 --- a/spec/rubocop/cop/avoid_break_from_strong_memoize_spec.rb +++ b/spec/rubocop/cop/avoid_break_from_strong_memoize_spec.rb @@ -1,11 +1,10 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../rubocop/cop/avoid_break_from_strong_memoize' -RSpec.describe RuboCop::Cop::AvoidBreakFromStrongMemoize do +RSpec.describe RuboCop::Cop::AvoidBreakFromStrongMemoize, type: :rubocop do include CopHelper subject(:cop) { described_class.new } @@ -62,7 +61,7 @@ RSpec.describe RuboCop::Cop::AvoidBreakFromStrongMemoize do end end RUBY - expect_next_instance_of(described_class) do |instance| + expect_any_instance_of(described_class) do |instance| expect(instance).to receive(:add_offense).once end diff --git a/spec/rubocop/cop/avoid_return_from_blocks_spec.rb b/spec/rubocop/cop/avoid_return_from_blocks_spec.rb index 9e571bf96b9..a157183646c 100644 --- a/spec/rubocop/cop/avoid_return_from_blocks_spec.rb +++ b/spec/rubocop/cop/avoid_return_from_blocks_spec.rb @@ -1,11 +1,10 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../rubocop/cop/avoid_return_from_blocks' -RSpec.describe RuboCop::Cop::AvoidReturnFromBlocks do +RSpec.describe RuboCop::Cop::AvoidReturnFromBlocks, type: :rubocop do include CopHelper subject(:cop) { described_class.new } @@ -29,7 +28,7 @@ RSpec.describe RuboCop::Cop::AvoidReturnFromBlocks do end end RUBY - expect_next_instance_of(described_class) do |instance| + expect_any_instance_of(described_class) do |instance| expect(instance).to receive(:add_offense).once end 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 30ee422f420..a47625d5dc1 100644 --- a/spec/rubocop/cop/gitlab/finder_with_find_by_spec.rb +++ b/spec/rubocop/cop/gitlab/finder_with_find_by_spec.rb @@ -20,6 +20,7 @@ RSpec.describe RuboCop::Cop::Gitlab::FinderWithFindBy, type: :rubocop do .find_by!(1) SRC end + let(:corrected_source) do <<~SRC DummyFinder.new(some_args) diff --git a/spec/rubocop/cop/graphql/json_type_spec.rb b/spec/rubocop/cop/graphql/json_type_spec.rb new file mode 100644 index 00000000000..ac25e0feb69 --- /dev/null +++ b/spec/rubocop/cop/graphql/json_type_spec.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'rubocop' +require_relative '../../../../rubocop/cop/graphql/json_type' + +RSpec.describe RuboCop::Cop::Graphql::JSONType, type: :rubocop do + include CopHelper + + subject(:cop) { described_class.new } + + context 'fields' do + it 'adds an offense when GraphQL::Types::JSON is used' do + inspect_source(<<~RUBY.strip) + class MyType + field :some_field, GraphQL::Types::JSON + end + RUBY + + expect(cop.offenses.size).to eq(1) + end + + it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do + inspect_source(<<~RUBY.strip) + class MyType + field :some_field, GraphQL::Types::JSON, null: true, description: 'My description' + end + RUBY + + expect(cop.offenses.size).to eq(1) + end + + it 'does not add an offense for other types' do + expect_no_offenses(<<~RUBY.strip) + class MyType + field :some_field, GraphQL::STRING_TYPE + end + RUBY + end + end + + context 'arguments' do + it 'adds an offense when GraphQL::Types::JSON is used' do + inspect_source(<<~RUBY.strip) + class MyType + argument :some_arg, GraphQL::Types::JSON + end + RUBY + + expect(cop.offenses.size).to eq(1) + end + + it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do + inspect_source(<<~RUBY.strip) + class MyType + argument :some_arg, GraphQL::Types::JSON, null: true, description: 'My description' + end + RUBY + + expect(cop.offenses.size).to eq(1) + end + + it 'does not add an offense for other types' do + expect_no_offenses(<<~RUBY.strip) + class MyType + argument :some_arg, GraphQL::STRING_TYPE + end + RUBY + end + end + + it 'does not add an offense for uses outside of field or argument' do + expect_no_offenses(<<~RUBY.strip) + class MyType + foo :some_field, GraphQL::Types::JSON + end + RUBY + end +end diff --git a/spec/rubocop/cop/migration/drop_table_spec.rb b/spec/rubocop/cop/migration/drop_table_spec.rb index 44a1106ba62..9ce5ee45b08 100644 --- a/spec/rubocop/cop/migration/drop_table_spec.rb +++ b/spec/rubocop/cop/migration/drop_table_spec.rb @@ -1,13 +1,10 @@ # frozen_string_literal: true -require 'spec_helper' - +require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' - require_relative '../../../../rubocop/cop/migration/drop_table' -RSpec.describe RuboCop::Cop::Migration::DropTable do +RSpec.describe RuboCop::Cop::Migration::DropTable, type: :rubocop do include CopHelper subject(:cop) { described_class.new } diff --git a/spec/rubocop/cop/put_group_routes_under_scope_spec.rb b/spec/rubocop/cop/put_group_routes_under_scope_spec.rb index 2e577c9c578..c55d9bf22d6 100644 --- a/spec/rubocop/cop/put_group_routes_under_scope_spec.rb +++ b/spec/rubocop/cop/put_group_routes_under_scope_spec.rb @@ -9,19 +9,20 @@ RSpec.describe RuboCop::Cop::PutGroupRoutesUnderScope, type: :rubocop do subject(:cop) { described_class.new } - before do - allow(cop).to receive(:in_group_routes?).and_return(true) - end + %w[resource resources get post put patch delete].each do |route_method| + it "registers an offense when route is outside scope for `#{route_method}`" do + offense = "#{route_method} :notes" + marker = '^' * offense.size - it 'registers an offense when route is outside scope' do - expect_offense(<<~PATTERN) + expect_offense(<<~PATTERN) scope(path: 'groups/*group_id/-', module: :groups) do resource :issues end - resource :notes - ^^^^^^^^^^^^^^^ Put new group routes under /-/ scope - PATTERN + #{offense} + #{marker} Put new group routes under /-/ scope + PATTERN + end end it 'does not register an offense when resource inside the scope' do diff --git a/spec/rubocop/cop/put_project_routes_under_scope_spec.rb b/spec/rubocop/cop/put_project_routes_under_scope_spec.rb index 66e9044c453..05e1cd7b693 100644 --- a/spec/rubocop/cop/put_project_routes_under_scope_spec.rb +++ b/spec/rubocop/cop/put_project_routes_under_scope_spec.rb @@ -9,19 +9,20 @@ RSpec.describe RuboCop::Cop::PutProjectRoutesUnderScope, type: :rubocop do subject(:cop) { described_class.new } - before do - allow(cop).to receive(:in_project_routes?).and_return(true) - end + %w[resource resources get post put patch delete].each do |route_method| + it "registers an offense when route is outside scope for `#{route_method}`" do + offense = "#{route_method} :notes" + marker = '^' * offense.size - it 'registers an offense when route is outside scope' do - expect_offense(<<~PATTERN) + expect_offense(<<~PATTERN) scope '-' do resource :issues end - resource :notes - ^^^^^^^^^^^^^^^ Put new project routes under /-/ scope - PATTERN + #{offense} + #{marker} Put new project routes under /-/ scope + PATTERN + end end it 'does not register an offense when resource inside the scope' do diff --git a/spec/rubocop/cop/rspec/any_instance_of_spec.rb b/spec/rubocop/cop/rspec/any_instance_of_spec.rb index 11c0f109850..971e28853a3 100644 --- a/spec/rubocop/cop/rspec/any_instance_of_spec.rb +++ b/spec/rubocop/cop/rspec/any_instance_of_spec.rb @@ -15,6 +15,7 @@ RSpec.describe RuboCop::Cop::RSpec::AnyInstanceOf, type: :rubocop do allow_any_instance_of(User).to receive(:invalidate_issue_cache_counts) SRC end + let(:corrected_source) do <<~SRC allow_next_instance_of(User) do |instance| @@ -40,6 +41,7 @@ RSpec.describe RuboCop::Cop::RSpec::AnyInstanceOf, type: :rubocop do expect_any_instance_of(User).to receive(:invalidate_issue_cache_counts).with(args).and_return(double) SRC end + let(:corrected_source) do <<~SRC expect_next_instance_of(User) do |instance| 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 new file mode 100644 index 00000000000..db931c50bdf --- /dev/null +++ b/spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' + +require_relative '../../../../rubocop/cop/usage_data/distinct_count_by_large_foreign_key' + +RSpec.describe RuboCop::Cop::UsageData::DistinctCountByLargeForeignKey, type: :rubocop do + include CopHelper + + let(:allowed_foreign_keys) { %i[author_id user_id] } + + let(:config) do + RuboCop::Config.new('UsageData/DistinctCountByLargeForeignKey' => { + 'AllowedForeignKeys' => allowed_foreign_keys + }) + end + + subject(:cop) { described_class.new(config) } + + context 'when counting by disallowed key' do + it 'register an offence' do + inspect_source('distinct_count(Issue, :creator_id)') + + expect(cop.offenses.size).to eq(1) + end + end + + context 'when calling by allowed key' do + it 'does not register an offence' do + inspect_source('distinct_count(Issue, :author_id)') + + expect(cop.offenses).to be_empty + 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 new file mode 100644 index 00000000000..de6fb9c17e2 --- /dev/null +++ b/spec/rubocop/cop/usage_data/large_table_spec.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' + +require_relative '../../../../rubocop/cop/usage_data/large_table' + +RSpec.describe RuboCop::Cop::UsageData::LargeTable, type: :rubocop do + include CopHelper + + let(:large_tables) { %i[Rails Time] } + let(:count_methods) { %i[count distinct_count] } + let(:allowed_methods) { %i[minimum maximum] } + + let(:config) do + RuboCop::Config.new('UsageData/LargeTable' => { + 'NonRelatedClasses' => large_tables, + 'CountMethods' => count_methods, + 'AllowedMethods' => allowed_methods + }) + end + + subject(:cop) { described_class.new(config) } + + context 'when in usage_data files' do + before do + allow(cop).to receive(:usage_data_files?).and_return(true) + end + + context 'with large tables' do + context 'when calling Issue.count' do + it 'register an offence' do + inspect_source('Issue.count') + + expect(cop.offenses.size).to eq(1) + end + end + + context 'when calling Issue.active.count' do + it 'register an offence' do + inspect_source('Issue.active.count') + + expect(cop.offenses.size).to eq(1) + end + end + + context 'when calling count(Issue)' do + it 'does not register an offence' do + inspect_source('count(Issue)') + + expect(cop.offenses).to be_empty + end + end + + context 'when calling count(Ci::Build.active)' do + it 'does not register an offence' do + inspect_source('count(Ci::Build.active)') + + expect(cop.offenses).to be_empty + end + end + + context 'when calling Ci::Build.active.count' do + it 'register an offence' do + inspect_source('Ci::Build.active.count') + + expect(cop.offenses.size).to eq(1) + end + end + + context 'when using allowed methods' do + it 'does not register an offence' do + inspect_source('Issue.minimum') + + expect(cop.offenses).to be_empty + end + end + end + + context 'with non related class' do + it 'does not register an offence' do + inspect_source('Rails.count') + + expect(cop.offenses).to be_empty + end + end + end +end |