diff options
Diffstat (limited to 'spec/rubocop/cop')
-rw-r--r-- | spec/rubocop/cop/code_reuse/active_record_spec.rb | 134 | ||||
-rw-r--r-- | spec/rubocop/cop/graphql/resolver_type_spec.rb | 74 | ||||
-rw-r--r-- | spec/rubocop/cop/line_break_around_conditional_block_spec.rb | 454 | ||||
-rw-r--r-- | spec/rubocop/cop/rspec/be_success_matcher_spec.rb | 2 |
4 files changed, 75 insertions, 589 deletions
diff --git a/spec/rubocop/cop/code_reuse/active_record_spec.rb b/spec/rubocop/cop/code_reuse/active_record_spec.rb deleted file mode 100644 index e15b9e11aed..00000000000 --- a/spec/rubocop/cop/code_reuse/active_record_spec.rb +++ /dev/null @@ -1,134 +0,0 @@ -# frozen_string_literal: true - -require 'fast_spec_helper' -require 'rubocop' -require_relative '../../../../rubocop/cop/code_reuse/active_record' - -RSpec.describe RuboCop::Cop::CodeReuse::ActiveRecord, type: :rubocop do - include CopHelper - - subject(:cop) { described_class.new } - - it 'flags the use of "where" without any arguments' do - expect_offense(<<~SOURCE) - def foo - User.where - ^^^^^ This method can only be used inside an ActiveRecord model: https://gitlab.com/gitlab-org/gitlab-foss/issues/49653 - end - SOURCE - end - - it 'flags the use of "where" with arguments' do - expect_offense(<<~SOURCE) - def foo - User.where(id: 10) - ^^^^^ This method can only be used inside an ActiveRecord model: https://gitlab.com/gitlab-org/gitlab-foss/issues/49653 - end - SOURCE - end - - it 'does not flag the use of "group" without any arguments' do - expect_no_offenses(<<~SOURCE) - def foo - project.group - end - SOURCE - end - - it 'flags the use of "group" with arguments' do - expect_offense(<<~SOURCE) - def foo - project.group(:name) - ^^^^^ This method can only be used inside an ActiveRecord model: https://gitlab.com/gitlab-org/gitlab-foss/issues/49653 - end - SOURCE - end - - it 'does not flag the use of ActiveRecord models in a model' do - path = rails_root_join('app', 'models', 'foo.rb').to_s - - expect_no_offenses(<<~SOURCE, path) - def foo - project.group(:name) - end - SOURCE - end - - it 'does not flag the use of ActiveRecord models in a spec' do - path = rails_root_join('spec', 'foo_spec.rb').to_s - - expect_no_offenses(<<~SOURCE, path) - def foo - project.group(:name) - end - SOURCE - end - - it 'does not flag the use of ActiveRecord models in a background migration' do - path = rails_root_join('lib', 'gitlab', 'background_migration', 'foo.rb').to_s - - expect_no_offenses(<<~SOURCE, path) - def foo - project.group(:name) - end - SOURCE - end - - it 'does not flag the use of ActiveRecord models in lib/gitlab/database' do - path = rails_root_join('lib', 'gitlab', 'database', 'foo.rb').to_s - - expect_no_offenses(<<~SOURCE, path) - def foo - project.group(:name) - end - SOURCE - end - - it 'autocorrects offenses in instance methods by allowing them' do - corrected = autocorrect_source(<<~SOURCE) - def foo - User.where - end - SOURCE - - expect(corrected).to eq(<<~SOURCE) - # rubocop: disable CodeReuse/ActiveRecord - def foo - User.where - end - # rubocop: enable CodeReuse/ActiveRecord - SOURCE - end - - it 'autocorrects offenses in class methods by allowing them' do - corrected = autocorrect_source(<<~SOURCE) - def self.foo - User.where - end - SOURCE - - expect(corrected).to eq(<<~SOURCE) - # rubocop: disable CodeReuse/ActiveRecord - def self.foo - User.where - end - # rubocop: enable CodeReuse/ActiveRecord - SOURCE - end - - it 'autocorrects offenses in blocks by allowing them' do - corrected = autocorrect_source(<<~SOURCE) - get '/' do - User.where - end - SOURCE - - expect(corrected).to eq(<<~SOURCE) - # rubocop: disable CodeReuse/ActiveRecord - get '/' do - User.where - end - # rubocop: enable CodeReuse/ActiveRecord - SOURCE - end -end diff --git a/spec/rubocop/cop/graphql/resolver_type_spec.rb b/spec/rubocop/cop/graphql/resolver_type_spec.rb new file mode 100644 index 00000000000..4807d66396a --- /dev/null +++ b/spec/rubocop/cop/graphql/resolver_type_spec.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'rubocop' + +require_relative '../../../../rubocop/cop/graphql/resolver_type' + +RSpec.describe RuboCop::Cop::Graphql::ResolverType, type: :rubocop do + include CopHelper + + subject(:cop) { described_class.new } + + it 'adds an offense when there is no type annotaion' do + lacks_type = <<-SRC + module Resolvers + class FooResolver < BaseResolver + def resolve(**args) + [:thing] + end + end + end + SRC + + inspect_source(lacks_type) + + expect(cop.offenses.size).to eq 1 + end + + it 'does not add an offense for resolvers that have a type call' do + expect_no_offenses(<<-SRC) + module Resolvers + class FooResolver < BaseResolver + type [SomeEnum], null: true + + def resolve(**args) + [:thing] + end + end + end + SRC + end + + it 'ignores type calls on other objects' do + lacks_type = <<-SRC + module Resolvers + class FooResolver < BaseResolver + class FalsePositive < BaseObject + type RedHerringType, null: true + end + + def resolve(**args) + [:thing] + end + end + end + SRC + + inspect_source(lacks_type) + + expect(cop.offenses.size).to eq 1 + end + + it 'does not add an offense unless the class is named using the Resolver convention' do + expect_no_offenses(<<-TYPE) + module Resolvers + class FooThingy + def something_other_than_resolve(**args) + [:thing] + end + end + end + TYPE + end +end diff --git a/spec/rubocop/cop/line_break_around_conditional_block_spec.rb b/spec/rubocop/cop/line_break_around_conditional_block_spec.rb deleted file mode 100644 index 0a26ef49e35..00000000000 --- a/spec/rubocop/cop/line_break_around_conditional_block_spec.rb +++ /dev/null @@ -1,454 +0,0 @@ -# frozen_string_literal: true - -require 'fast_spec_helper' -require 'rubocop' -require 'rubocop/rspec/support' -require_relative '../../../rubocop/cop/line_break_around_conditional_block' - -RSpec.describe RuboCop::Cop::LineBreakAroundConditionalBlock, type: :rubocop do - include CopHelper - - subject(:cop) { described_class.new } - - shared_examples 'examples with conditional' do |conditional| - it "flags violation for #{conditional} without line break before" do - source = <<~RUBY - do_something - #{conditional} condition - do_something_more - end - RUBY - inspect_source(source) - - expect(cop.offenses.size).to eq(1) - offense = cop.offenses.first - - expect(offense.line).to eq(2) - expect(cop.highlights).to eq(["#{conditional} condition\n do_something_more\nend"]) - expect(offense.message).to eq('Add a line break around conditional blocks') - end - - it "flags violation for #{conditional} without line break after" do - source = <<~RUBY - #{conditional} condition - do_something - end - do_something_more - RUBY - inspect_source(source) - - expect(cop.offenses.size).to eq(1) - offense = cop.offenses.first - - expect(offense.line).to eq(1) - expect(cop.highlights).to eq(["#{conditional} condition\n do_something\nend"]) - expect(offense.message).to eq('Add a line break around conditional blocks') - end - - it "doesn't flag violation for #{conditional} with line break before and after" do - source = <<~RUBY - #{conditional} condition - do_something - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a method definition" do - source = <<~RUBY - def a_method - #{conditional} condition - do_something - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a class definition" do - source = <<~RUBY - class Foo - #{conditional} condition - do_something - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a module definition" do - source = <<~RUBY - module Foo - #{conditional} condition - do_something - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a begin definition" do - source = <<~RUBY - begin - #{conditional} condition - do_something - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by an assign/begin definition" do - source = <<~RUBY - @project ||= begin - #{conditional} condition - do_something - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a block definition" do - source = <<~RUBY - on_block(param_a) do |item| - #{conditional} condition - do_something - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a block definition with a comment" do - source = <<~RUBY - on_block(param_a) do |item| # a short comment - #{conditional} condition - do_something - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a block definition using brackets" do - source = <<~RUBY - on_block(param_a) { |item| - #{conditional} condition - do_something - end - } - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a comment" do - source = <<~RUBY - # a short comment - #{conditional} condition - do_something - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by an assignment" do - source = <<~RUBY - foo = - #{conditional} condition - do_something - else - do_something_more - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a multiline comment" do - source = <<~RUBY - =begin - a multiline comment - =end - #{conditional} condition - do_something - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by another conditional" do - source = <<~RUBY - #{conditional} condition_a - #{conditional} condition_b - do_something - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by an else" do - source = <<~RUBY - if condition_a - do_something - else - #{conditional} condition_b - do_something_extra - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by an elsif" do - source = <<~RUBY - if condition_a - do_something - elsif condition_b - #{conditional} condition_c - do_something_extra - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by an ensure" do - source = <<~RUBY - def a_method - ensure - #{conditional} condition_c - do_something_extra - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a when" do - source = <<~RUBY - case field - when value - #{conditional} condition_c - do_something_extra - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} followed by a comment" do - source = <<~RUBY - #{conditional} condition - do_something - end - # a short comment - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} followed by an end" do - source = <<~RUBY - class Foo - - #{conditional} condition - do_something - end - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} followed by an else" do - source = <<~RUBY - #{conditional} condition_a - #{conditional} condition_b - do_something - end - else - do_something_extra - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} followed by a when" do - source = <<~RUBY - case - when condition_a - #{conditional} condition_b - do_something - end - when condition_c - do_something_extra - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} followed by an elsif" do - source = <<~RUBY - if condition_a - #{conditional} condition_b - do_something - end - elsif condition_c - do_something_extra - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} preceded by a rescue" do - source = <<~RUBY - def a_method - do_something - rescue - #{conditional} condition - do_something - end - end - RUBY - - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "doesn't flag violation for #{conditional} followed by a rescue" do - source = <<~RUBY - def a_method - #{conditional} condition - do_something - end - rescue - do_something_extra - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - end - - it "autocorrects #{conditional} without line break before" do - source = <<~RUBY - do_something - #{conditional} condition - do_something_more - end - RUBY - autocorrected = autocorrect_source(source) - - expected_source = <<~RUBY - do_something - - #{conditional} condition - do_something_more - end - RUBY - expect(autocorrected).to eql(expected_source) - end - - it "autocorrects #{conditional} without line break after" do - source = <<~RUBY - #{conditional} condition - do_something - end - do_something_more - RUBY - autocorrected = autocorrect_source(source) - - expected_source = <<~RUBY - #{conditional} condition - do_something - end - - do_something_more - RUBY - expect(autocorrected).to eql(expected_source) - end - - it "autocorrects #{conditional} without line break before and after" do - source = <<~RUBY - do_something - #{conditional} condition - do_something_more - end - do_something_extra - RUBY - autocorrected = autocorrect_source(source) - - expected_source = <<~RUBY - do_something - - #{conditional} condition - do_something_more - end - - do_something_extra - RUBY - expect(autocorrected).to eql(expected_source) - end - end - - %w[if unless].each do |example| - it_behaves_like 'examples with conditional', example - end - - it "doesn't flag violation for if with elsif" do - source = <<~RUBY - if condition - do_something - elsif another_condition - do_something_more - end - RUBY - inspect_source(source) - - expect(cop.offenses).to be_empty - 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 a16cd8b634f..b14cf39cbde 100644 --- a/spec/rubocop/cop/rspec/be_success_matcher_spec.rb +++ b/spec/rubocop/cop/rspec/be_success_matcher_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'fast_spec_helper' - +require 'rubocop' require_relative '../../../../rubocop/cop/rspec/be_success_matcher' RSpec.describe RuboCop::Cop::RSpec::BeSuccessMatcher, type: :rubocop do |