Commit 3f69c327 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '276734-deprecate-use-of-rubocop-cop_helper' into 'master'

Resolve depcrecate cop_helper exceptions part 1

See merge request gitlab-org/gitlab!51989
parents ee5225cb e3595400
...@@ -31,9 +31,6 @@ FactoryBot/InlineAssociation: ...@@ -31,9 +31,6 @@ FactoryBot/InlineAssociation:
InternalAffairs/DeprecateCopHelper: InternalAffairs/DeprecateCopHelper:
Exclude: Exclude:
- 'spec/rubocop/code_reuse_helpers_spec.rb'
- 'spec/rubocop/qa_helpers_spec.rb'
- 'spec/rubocop/migration_helpers_spec.rb'
- 'spec/rubocop/cop/group_public_or_visible_to_user_spec.rb' - 'spec/rubocop/cop/group_public_or_visible_to_user_spec.rb'
- 'spec/rubocop/cop/static_translation_definition_spec.rb' - 'spec/rubocop/cop/static_translation_definition_spec.rb'
- 'spec/rubocop/cop/lint/last_keyword_argument_spec.rb' - 'spec/rubocop/cop/lint/last_keyword_argument_spec.rb'
...@@ -64,25 +61,12 @@ InternalAffairs/DeprecateCopHelper: ...@@ -64,25 +61,12 @@ InternalAffairs/DeprecateCopHelper:
- 'spec/rubocop/cop/qa/ambiguous_page_object_name_spec.rb' - 'spec/rubocop/cop/qa/ambiguous_page_object_name_spec.rb'
- 'spec/rubocop/cop/qa/element_with_pattern_spec.rb' - 'spec/rubocop/cop/qa/element_with_pattern_spec.rb'
- 'spec/rubocop/cop/inject_enterprise_edition_module_spec.rb' - 'spec/rubocop/cop/inject_enterprise_edition_module_spec.rb'
- 'spec/rubocop/cop/code_reuse/finder_spec.rb'
- 'spec/rubocop/cop/code_reuse/worker_spec.rb'
- 'spec/rubocop/cop/code_reuse/service_class_spec.rb'
- 'spec/rubocop/cop/code_reuse/presenter_spec.rb'
- 'spec/rubocop/cop/code_reuse/serializer_spec.rb'
- 'spec/rubocop/cop/avoid_keyword_arguments_in_sidekiq_workers_spec.rb' - 'spec/rubocop/cop/avoid_keyword_arguments_in_sidekiq_workers_spec.rb'
- 'spec/rubocop/cop/default_scope_spec.rb' - 'spec/rubocop/cop/default_scope_spec.rb'
- 'spec/rubocop/cop/graphql/resolver_type_spec.rb'
- 'spec/rubocop/cop/graphql/descriptions_spec.rb'
- 'spec/rubocop/cop/graphql/json_type_spec.rb'
- 'spec/rubocop/cop/graphql/gid_expected_type_spec.rb'
- 'spec/rubocop/cop/graphql/authorize_types_spec.rb'
- 'spec/rubocop/cop/graphql/id_type_spec.rb'
- 'spec/rubocop/cop/scalability/bulk_perform_with_context_spec.rb' - 'spec/rubocop/cop/scalability/bulk_perform_with_context_spec.rb'
- 'spec/rubocop/cop/scalability/idempotent_worker_spec.rb' - 'spec/rubocop/cop/scalability/idempotent_worker_spec.rb'
- 'spec/rubocop/cop/scalability/cron_worker_context_spec.rb' - 'spec/rubocop/cop/scalability/cron_worker_context_spec.rb'
- 'spec/rubocop/cop/scalability/file_uploads_spec.rb' - 'spec/rubocop/cop/scalability/file_uploads_spec.rb'
- 'spec/rubocop/cop/api/grape_array_missing_coerce_spec.rb'
- 'spec/rubocop/cop/api/base_spec.rb'
- 'spec/rubocop/cop/destroy_all_spec.rb' - 'spec/rubocop/cop/destroy_all_spec.rb'
- 'spec/rubocop/cop/safe_params_spec.rb' - 'spec/rubocop/cop/safe_params_spec.rb'
- 'spec/rubocop/cop/include_sidekiq_worker_spec.rb' - 'spec/rubocop/cop/include_sidekiq_worker_spec.rb'
......
...@@ -6,7 +6,7 @@ require 'parser/current' ...@@ -6,7 +6,7 @@ require 'parser/current'
require_relative '../../rubocop/code_reuse_helpers' require_relative '../../rubocop/code_reuse_helpers'
RSpec.describe RuboCop::CodeReuseHelpers do RSpec.describe RuboCop::CodeReuseHelpers do
def parse_source(source, path = 'foo.rb') def build_and_parse_source(source, path = 'foo.rb')
buffer = Parser::Source::Buffer.new(path) buffer = Parser::Source::Buffer.new(path)
buffer.source = source buffer.source = source
...@@ -24,13 +24,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -24,13 +24,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#send_to_constant?' do describe '#send_to_constant?' do
it 'returns true when sending to a constant' do it 'returns true when sending to a constant' do
node = parse_source('Foo.bar') node = build_and_parse_source('Foo.bar')
expect(cop.send_to_constant?(node)).to eq(true) expect(cop.send_to_constant?(node)).to eq(true)
end end
it 'returns false when sending to something other than a constant' do it 'returns false when sending to something other than a constant' do
node = parse_source('10') node = build_and_parse_source('10')
expect(cop.send_to_constant?(node)).to eq(false) expect(cop.send_to_constant?(node)).to eq(false)
end end
...@@ -38,13 +38,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -38,13 +38,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#send_receiver_name_ends_with?' do describe '#send_receiver_name_ends_with?' do
it 'returns true when the receiver ends with a suffix' do it 'returns true when the receiver ends with a suffix' do
node = parse_source('FooFinder.new') node = build_and_parse_source('FooFinder.new')
expect(cop.send_receiver_name_ends_with?(node, 'Finder')).to eq(true) expect(cop.send_receiver_name_ends_with?(node, 'Finder')).to eq(true)
end end
it 'returns false when the receiver is the same as a suffix' do it 'returns false when the receiver is the same as a suffix' do
node = parse_source('Finder.new') node = build_and_parse_source('Finder.new')
expect(cop.send_receiver_name_ends_with?(node, 'Finder')).to eq(false) expect(cop.send_receiver_name_ends_with?(node, 'Finder')).to eq(false)
end end
...@@ -52,7 +52,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -52,7 +52,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#file_path_for_node' do describe '#file_path_for_node' do
it 'returns the file path of a node' do it 'returns the file path of a node' do
node = parse_source('10') node = build_and_parse_source('10')
path = cop.file_path_for_node(node) path = cop.file_path_for_node(node)
expect(path).to eq('foo.rb') expect(path).to eq('foo.rb')
...@@ -61,7 +61,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -61,7 +61,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#name_of_constant' do describe '#name_of_constant' do
it 'returns the name of a constant' do it 'returns the name of a constant' do
node = parse_source('Foo') node = build_and_parse_source('Foo')
expect(cop.name_of_constant(node)).to eq(:Foo) expect(cop.name_of_constant(node)).to eq(:Foo)
end end
...@@ -69,13 +69,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -69,13 +69,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_finder?' do describe '#in_finder?' do
it 'returns true for a node in the finders directory' do it 'returns true for a node in the finders directory' do
node = parse_source('10', rails_root_join('app', 'finders', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'finders', 'foo.rb'))
expect(cop.in_finder?(node)).to eq(true) expect(cop.in_finder?(node)).to eq(true)
end end
it 'returns false for a node outside the finders directory' do it 'returns false for a node outside the finders directory' do
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_finder?(node)).to eq(false) expect(cop.in_finder?(node)).to eq(false)
end end
...@@ -83,13 +83,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -83,13 +83,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_model?' do describe '#in_model?' do
it 'returns true for a node in the models directory' do it 'returns true for a node in the models directory' do
node = parse_source('10', rails_root_join('app', 'models', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'models', 'foo.rb'))
expect(cop.in_model?(node)).to eq(true) expect(cop.in_model?(node)).to eq(true)
end end
it 'returns false for a node outside the models directory' do it 'returns false for a node outside the models directory' do
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_model?(node)).to eq(false) expect(cop.in_model?(node)).to eq(false)
end end
...@@ -97,13 +97,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -97,13 +97,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_service_class?' do describe '#in_service_class?' do
it 'returns true for a node in the services directory' do it 'returns true for a node in the services directory' do
node = parse_source('10', rails_root_join('app', 'services', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'services', 'foo.rb'))
expect(cop.in_service_class?(node)).to eq(true) expect(cop.in_service_class?(node)).to eq(true)
end end
it 'returns false for a node outside the services directory' do it 'returns false for a node outside the services directory' do
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_service_class?(node)).to eq(false) expect(cop.in_service_class?(node)).to eq(false)
end end
...@@ -111,13 +111,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -111,13 +111,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_presenter?' do describe '#in_presenter?' do
it 'returns true for a node in the presenters directory' do it 'returns true for a node in the presenters directory' do
node = parse_source('10', rails_root_join('app', 'presenters', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'presenters', 'foo.rb'))
expect(cop.in_presenter?(node)).to eq(true) expect(cop.in_presenter?(node)).to eq(true)
end end
it 'returns false for a node outside the presenters directory' do it 'returns false for a node outside the presenters directory' do
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_presenter?(node)).to eq(false) expect(cop.in_presenter?(node)).to eq(false)
end end
...@@ -125,13 +125,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -125,13 +125,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_serializer?' do describe '#in_serializer?' do
it 'returns true for a node in the serializers directory' do it 'returns true for a node in the serializers directory' do
node = parse_source('10', rails_root_join('app', 'serializers', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'serializers', 'foo.rb'))
expect(cop.in_serializer?(node)).to eq(true) expect(cop.in_serializer?(node)).to eq(true)
end end
it 'returns false for a node outside the serializers directory' do it 'returns false for a node outside the serializers directory' do
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_serializer?(node)).to eq(false) expect(cop.in_serializer?(node)).to eq(false)
end end
...@@ -139,13 +139,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -139,13 +139,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_worker?' do describe '#in_worker?' do
it 'returns true for a node in the workers directory' do it 'returns true for a node in the workers directory' do
node = parse_source('10', rails_root_join('app', 'workers', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'workers', 'foo.rb'))
expect(cop.in_worker?(node)).to eq(true) expect(cop.in_worker?(node)).to eq(true)
end end
it 'returns false for a node outside the workers directory' do it 'returns false for a node outside the workers directory' do
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_worker?(node)).to eq(false) expect(cop.in_worker?(node)).to eq(false)
end end
...@@ -153,13 +153,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -153,13 +153,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_api?' do describe '#in_api?' do
it 'returns true for a node in the API directory' do it 'returns true for a node in the API directory' do
node = parse_source('10', rails_root_join('lib', 'api', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('lib', 'api', 'foo.rb'))
expect(cop.in_api?(node)).to eq(true) expect(cop.in_api?(node)).to eq(true)
end end
it 'returns false for a node outside the API directory' do it 'returns false for a node outside the API directory' do
node = parse_source('10', rails_root_join('lib', 'foo', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('lib', 'foo', 'foo.rb'))
expect(cop.in_api?(node)).to eq(false) expect(cop.in_api?(node)).to eq(false)
end end
...@@ -167,21 +167,21 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -167,21 +167,21 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_directory?' do describe '#in_directory?' do
it 'returns true for a directory in the CE app/ directory' do it 'returns true for a directory in the CE app/ directory' do
node = parse_source('10', rails_root_join('app', 'models', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'models', 'foo.rb'))
expect(cop.in_directory?(node, 'models')).to eq(true) expect(cop.in_directory?(node, 'models')).to eq(true)
end end
it 'returns true for a directory in the EE app/ directory' do it 'returns true for a directory in the EE app/ directory' do
node = node =
parse_source('10', rails_root_join('ee', 'app', 'models', 'foo.rb')) build_and_parse_source('10', rails_root_join('ee', 'app', 'models', 'foo.rb'))
expect(cop.in_directory?(node, 'models')).to eq(true) expect(cop.in_directory?(node, 'models')).to eq(true)
end end
it 'returns false for a directory in the lib/ directory' do it 'returns false for a directory in the lib/ directory' do
node = node =
parse_source('10', rails_root_join('lib', 'models', 'foo.rb')) build_and_parse_source('10', rails_root_join('lib', 'models', 'foo.rb'))
expect(cop.in_directory?(node, 'models')).to eq(false) expect(cop.in_directory?(node, 'models')).to eq(false)
end end
...@@ -189,7 +189,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -189,7 +189,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#name_of_receiver' do describe '#name_of_receiver' do
it 'returns the name of a send receiver' do it 'returns the name of a send receiver' do
node = parse_source('Foo.bar') node = build_and_parse_source('Foo.bar')
expect(cop.name_of_receiver(node)).to eq('Foo') expect(cop.name_of_receiver(node)).to eq('Foo')
end end
...@@ -197,7 +197,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -197,7 +197,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#each_class_method' do describe '#each_class_method' do
it 'yields every class method to the supplied block' do it 'yields every class method to the supplied block' do
node = parse_source(<<~RUBY) node = build_and_parse_source(<<~RUBY)
class Foo class Foo
class << self class << self
def first def first
...@@ -220,7 +220,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -220,7 +220,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#each_send_node' do describe '#each_send_node' do
it 'yields every send node to the supplied block' do it 'yields every send node to the supplied block' do
node = parse_source("foo\nbar") node = build_and_parse_source("foo\nbar")
nodes = cop.each_send_node(node).to_a nodes = cop.each_send_node(node).to_a
expect(nodes.length).to eq(2) expect(nodes.length).to eq(2)
...@@ -231,7 +231,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do ...@@ -231,7 +231,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#disallow_send_to' do describe '#disallow_send_to' do
it 'disallows sending a message to a constant' do it 'disallows sending a message to a constant' do
def_node = parse_source(<<~RUBY) def_node = build_and_parse_source(<<~RUBY)
def foo def foo
FooFinder.new FooFinder.new
end end
......
...@@ -2,12 +2,9 @@ ...@@ -2,12 +2,9 @@
require 'fast_spec_helper' require 'fast_spec_helper'
require 'rubocop' require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/api/base' require_relative '../../../../rubocop/cop/api/base'
RSpec.describe RuboCop::Cop::API::Base do RSpec.describe RuboCop::Cop::API::Base do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
let(:corrected) do let(:corrected) do
...@@ -17,7 +14,7 @@ RSpec.describe RuboCop::Cop::API::Base do ...@@ -17,7 +14,7 @@ RSpec.describe RuboCop::Cop::API::Base do
CORRECTED CORRECTED
end end
['Grape::API', '::Grape::API', 'Grape::API::Instance', '::Grape::API::Instance'].each do |offense| %w[Grape::API ::Grape::API Grape::API::Instance ::Grape::API::Instance].each do |offense|
it "adds an offense when inheriting from #{offense}" do it "adds an offense when inheriting from #{offense}" do
expect_offense(<<~CODE) expect_offense(<<~CODE)
class SomeAPI < #{offense} class SomeAPI < #{offense}
......
...@@ -5,36 +5,38 @@ require 'rubocop' ...@@ -5,36 +5,38 @@ require 'rubocop'
require_relative '../../../../rubocop/cop/api/grape_array_missing_coerce' require_relative '../../../../rubocop/cop/api/grape_array_missing_coerce'
RSpec.describe RuboCop::Cop::API::GrapeArrayMissingCoerce do RSpec.describe RuboCop::Cop::API::GrapeArrayMissingCoerce do
include CopHelper let(:msg) do
"This Grape parameter defines an Array but is missing a coerce_with definition. " \
"For more details, see " \
"https://github.com/ruby-grape/grape/blob/master/UPGRADING.md#ensure-that-array-types-have-explicit-coercions"
end
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'adds an offense with a required parameter' do it 'adds an offense with a required parameter' do
inspect_source(<<~CODE) expect_offense(<<~TYPE)
class SomeAPI < Grape::API::Instance class SomeAPI < Grape::API::Instance
params do params do
requires :values, type: Array[String] requires :values, type: Array[String]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end end
end end
CODE TYPE
expect(cop.offenses.size).to eq(1)
end end
it 'adds an offense with an optional parameter' do it 'adds an offense with an optional parameter' do
inspect_source(<<~CODE) expect_offense(<<~TYPE)
class SomeAPI < Grape::API::Instance class SomeAPI < Grape::API::Instance
params do params do
optional :values, type: Array[String] optional :values, type: Array[String]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end end
end end
CODE TYPE
expect(cop.offenses.size).to eq(1)
end end
it 'does not add an offense' do it 'does not add an offense' do
inspect_source(<<~CODE) expect_no_offenses(<<~CODE)
class SomeAPI < Grape::API::Instance class SomeAPI < Grape::API::Instance
params do params do
requires :values, type: Array[String], coerce_with: ->(val) { val.split(',').map(&:strip) } requires :values, type: Array[String], coerce_with: ->(val) { val.split(',').map(&:strip) }
...@@ -44,19 +46,15 @@ RSpec.describe RuboCop::Cop::API::GrapeArrayMissingCoerce do ...@@ -44,19 +46,15 @@ RSpec.describe RuboCop::Cop::API::GrapeArrayMissingCoerce do
end end
end end
CODE CODE
expect(cop.offenses.size).to be_zero
end end
it 'does not add an offense for unrelated classes' do it 'does not add an offense for unrelated classes' do
inspect_source(<<~CODE) expect_no_offenses(<<~CODE)
class SomeClass class SomeClass
params do params do
requires :values, type: Array[String] requires :values, type: Array[String]
end end
end end
CODE CODE
expect(cop.offenses.size).to be_zero
end end
end end
...@@ -2,12 +2,9 @@ ...@@ -2,12 +2,9 @@
require 'fast_spec_helper' require 'fast_spec_helper'
require 'rubocop' require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/code_reuse/finder' require_relative '../../../../rubocop/cop/code_reuse/finder'
RSpec.describe RuboCop::Cop::CodeReuse::Finder do RSpec.describe RuboCop::Cop::CodeReuse::Finder do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'flags the use of a Finder inside another Finder' do it 'flags the use of a Finder inside another Finder' do
...@@ -23,8 +20,6 @@ RSpec.describe RuboCop::Cop::CodeReuse::Finder do ...@@ -23,8 +20,6 @@ RSpec.describe RuboCop::Cop::CodeReuse::Finder do
end end
end end
SOURCE SOURCE
expect(cop.offenses.size).to eq(1)
end end
it 'flags the use of a Finder inside a model class method' do it 'flags the use of a Finder inside a model class method' do
......
...@@ -2,12 +2,9 @@ ...@@ -2,12 +2,9 @@
require 'fast_spec_helper' require 'fast_spec_helper'
require 'rubocop' require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/code_reuse/presenter' require_relative '../../../../rubocop/cop/code_reuse/presenter'
RSpec.describe RuboCop::Cop::CodeReuse::Presenter do RSpec.describe RuboCop::Cop::CodeReuse::Presenter do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'flags the use of a Presenter in a Service class' do it 'flags the use of a Presenter in a Service class' do
......
...@@ -2,12 +2,9 @@ ...@@ -2,12 +2,9 @@
require 'fast_spec_helper' require 'fast_spec_helper'
require 'rubocop' require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/code_reuse/serializer' require_relative '../../../../rubocop/cop/code_reuse/serializer'
RSpec.describe RuboCop::Cop::CodeReuse::Serializer do RSpec.describe RuboCop::Cop::CodeReuse::Serializer do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'flags the use of a Serializer in a Service class' do it 'flags the use of a Serializer in a Service class' do
......
...@@ -2,12 +2,9 @@ ...@@ -2,12 +2,9 @@
require 'fast_spec_helper' require 'fast_spec_helper'
require 'rubocop' require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/code_reuse/service_class' require_relative '../../../../rubocop/cop/code_reuse/service_class'
RSpec.describe RuboCop::Cop::CodeReuse::ServiceClass do RSpec.describe RuboCop::Cop::CodeReuse::ServiceClass do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'flags the use of a Service class in a Finder' do it 'flags the use of a Service class in a Finder' do
......
...@@ -2,12 +2,9 @@ ...@@ -2,12 +2,9 @@
require 'fast_spec_helper' require 'fast_spec_helper'
require 'rubocop' require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/code_reuse/worker' require_relative '../../../../rubocop/cop/code_reuse/worker'
RSpec.describe RuboCop::Cop::CodeReuse::Worker do RSpec.describe RuboCop::Cop::CodeReuse::Worker do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'flags the use of a worker in a controller' do it 'flags the use of a worker in a controller' do
......
...@@ -6,21 +6,18 @@ require 'rubocop' ...@@ -6,21 +6,18 @@ require 'rubocop'
require_relative '../../../../rubocop/cop/graphql/authorize_types' require_relative '../../../../rubocop/cop/graphql/authorize_types'
RSpec.describe RuboCop::Cop::Graphql::AuthorizeTypes do RSpec.describe RuboCop::Cop::Graphql::AuthorizeTypes do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'adds an offense when there is no authorize call' do it 'adds an offense when there is no authorize call' do
inspect_source(<<~TYPE) expect_offense(<<~TYPE)
module Types module Types
class AType < BaseObject class AType < BaseObject
^^^^^^^^^^^^^^^^^^^^^^^^ Add an `authorize :ability` call to the type: https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#type-authorization
field :a_thing field :a_thing
field :another_thing field :another_thing
end end
end end
TYPE TYPE
expect(cop.offenses.size).to eq 1
end end
it 'does not add an offense for classes that have an authorize call' do it 'does not add an offense for classes that have an authorize call' do
......
...@@ -5,38 +5,34 @@ require 'rubocop' ...@@ -5,38 +5,34 @@ require 'rubocop'
require_relative '../../../../rubocop/cop/graphql/descriptions' require_relative '../../../../rubocop/cop/graphql/descriptions'
RSpec.describe RuboCop::Cop::Graphql::Descriptions do RSpec.describe RuboCop::Cop::Graphql::Descriptions do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
context 'fields' do context 'fields' do
it 'adds an offense when there is no description' do it 'adds an offense when there is no description' do
inspect_source(<<~TYPE) expect_offense(<<~TYPE)
module Types module Types
class FakeType < BaseObject class FakeType < BaseObject
field :a_thing, field :a_thing,
^^^^^^^^^^^^^^^ Please add a `description` property.
GraphQL::STRING_TYPE, GraphQL::STRING_TYPE,
null: false null: false
end end
end end
TYPE TYPE
expect(cop.offenses.size).to eq 1
end end
it 'adds an offense when description does not end in a period' do it 'adds an offense when description does not end in a period' do
inspect_source(<<~TYPE) expect_offense(<<~TYPE)
module Types module Types
class FakeType < BaseObject class FakeType < BaseObject
field :a_thing, field :a_thing,
^^^^^^^^^^^^^^^ `description` strings must end with a `.`.
GraphQL::STRING_TYPE, GraphQL::STRING_TYPE,
null: false, null: false,
description: 'A descriptive description' description: 'A descriptive description'
end end
end end
TYPE TYPE
expect(cop.offenses.size).to eq 1
end end
it 'does not add an offense when description is correct' do it 'does not add an offense when description is correct' do
...@@ -55,32 +51,30 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do ...@@ -55,32 +51,30 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do
context 'arguments' do context 'arguments' do
it 'adds an offense when there is no description' do it 'adds an offense when there is no description' do
inspect_source(<<~TYPE) expect_offense(<<~TYPE)
module Types module Types
class FakeType < BaseObject class FakeType < BaseObject
argument :a_thing, argument :a_thing,
^^^^^^^^^^^^^^^^^^ Please add a `description` property.
GraphQL::STRING_TYPE, GraphQL::STRING_TYPE,
null: false null: false
end end
end end
TYPE TYPE
expect(cop.offenses.size).to eq 1
end end
it 'adds an offense when description does not end in a period' do it 'adds an offense when description does not end in a period' do
inspect_source(<<~TYPE) expect_offense(<<~TYPE)
module Types module Types
class FakeType < BaseObject class FakeType < BaseObject
argument :a_thing, argument :a_thing,
^^^^^^^^^^^^^^^^^^ `description` strings must end with a `.`.
GraphQL::STRING_TYPE, GraphQL::STRING_TYPE,
null: false, null: false,
description: 'Behold! A description' description: 'Behold! A description'
end end
end end
TYPE TYPE
expect(cop.offenses.size).to eq 1
end end
it 'does not add an offense when description is correct' do it 'does not add an offense when description is correct' do
......
...@@ -6,16 +6,13 @@ require 'rubocop' ...@@ -6,16 +6,13 @@ require 'rubocop'
require_relative '../../../../rubocop/cop/graphql/gid_expected_type' require_relative '../../../../rubocop/cop/graphql/gid_expected_type'
RSpec.describe RuboCop::Cop::Graphql::GIDExpectedType do RSpec.describe RuboCop::Cop::Graphql::GIDExpectedType do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'adds an offense when there is no expected_type parameter' do it 'adds an offense when there is no expected_type parameter' do
inspect_source(<<~TYPE) expect_offense(<<~TYPE)
GitlabSchema.object_from_id(received_id) GitlabSchema.object_from_id(received_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an expected_type parameter to #object_from_id calls if possible.
TYPE TYPE
expect(cop.offenses.size).to eq 1
end end
it 'does not add an offense for calls that have an expected_type parameter' do it 'does not add an offense for calls that have an expected_type parameter' do
......
...@@ -6,16 +6,13 @@ require 'rubocop' ...@@ -6,16 +6,13 @@ require 'rubocop'
require_relative '../../../../rubocop/cop/graphql/id_type' require_relative '../../../../rubocop/cop/graphql/id_type'
RSpec.describe RuboCop::Cop::Graphql::IDType do RSpec.describe RuboCop::Cop::Graphql::IDType do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'adds an offense when GraphQL::ID_TYPE is used as a param to #argument' do it 'adds an offense when GraphQL::ID_TYPE is used as a param to #argument' do
inspect_source(<<~TYPE) expect_offense(<<~TYPE)
argument :some_arg, GraphQL::ID_TYPE, some: other, params: do_not_matter argument :some_arg, GraphQL::ID_TYPE, some: other, params: do_not_matter
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use GraphQL::ID_TYPE, use a specific GlobalIDType instead
TYPE TYPE
expect(cop.offenses.size).to eq 1
end end
context 'whitelisted arguments' do context 'whitelisted arguments' do
......
...@@ -5,29 +5,29 @@ require 'rubocop' ...@@ -5,29 +5,29 @@ require 'rubocop'
require_relative '../../../../rubocop/cop/graphql/json_type' require_relative '../../../../rubocop/cop/graphql/json_type'
RSpec.describe RuboCop::Cop::Graphql::JSONType do RSpec.describe RuboCop::Cop::Graphql::JSONType do
include CopHelper let(:msg) do
'Avoid using GraphQL::Types::JSON. See: https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#json'
end
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
context 'fields' do context 'fields' do
it 'adds an offense when GraphQL::Types::JSON is used' do it 'adds an offense when GraphQL::Types::JSON is used' do
inspect_source(<<~RUBY.strip) expect_offense(<<~RUBY)
class MyType class MyType
field :some_field, GraphQL::Types::JSON field :some_field, GraphQL::Types::JSON
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end end
RUBY RUBY
expect(cop.offenses.size).to eq(1)
end end
it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do
inspect_source(<<~RUBY.strip) expect_offense(<<~RUBY)
class MyType class MyType
field :some_field, GraphQL::Types::JSON, null: true, description: 'My description' field :some_field, GraphQL::Types::JSON, null: true, description: 'My description'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end end
RUBY RUBY
expect(cop.offenses.size).to eq(1)
end end
it 'does not add an offense for other types' do it 'does not add an offense for other types' do
...@@ -41,23 +41,21 @@ RSpec.describe RuboCop::Cop::Graphql::JSONType do ...@@ -41,23 +41,21 @@ RSpec.describe RuboCop::Cop::Graphql::JSONType do
context 'arguments' do context 'arguments' do
it 'adds an offense when GraphQL::Types::JSON is used' do it 'adds an offense when GraphQL::Types::JSON is used' do
inspect_source(<<~RUBY.strip) expect_offense(<<~RUBY)
class MyType class MyType
argument :some_arg, GraphQL::Types::JSON argument :some_arg, GraphQL::Types::JSON
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end end
RUBY RUBY
expect(cop.offenses.size).to eq(1)
end end
it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do
inspect_source(<<~RUBY.strip) expect_offense(<<~RUBY)
class MyType class MyType
argument :some_arg, GraphQL::Types::JSON, null: true, description: 'My description' argument :some_arg, GraphQL::Types::JSON, null: true, description: 'My description'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end end
RUBY RUBY
expect(cop.offenses.size).to eq(1)
end end
it 'does not add an offense for other types' do it 'does not add an offense for other types' do
......
...@@ -6,24 +6,19 @@ require 'rubocop' ...@@ -6,24 +6,19 @@ require 'rubocop'
require_relative '../../../../rubocop/cop/graphql/resolver_type' require_relative '../../../../rubocop/cop/graphql/resolver_type'
RSpec.describe RuboCop::Cop::Graphql::ResolverType do RSpec.describe RuboCop::Cop::Graphql::ResolverType do
include CopHelper
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'adds an offense when there is no type annotaion' do it 'adds an offense when there is no type annotation' do
lacks_type = <<-SRC expect_offense(<<~SRC)
module Resolvers module Resolvers
class FooResolver < BaseResolver class FooResolver < BaseResolver
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Missing type annotation: Please add `type` DSL method call. e.g: type UserType.connection_type, null: true
def resolve(**args) def resolve(**args)
[:thing] [:thing]
end end
end end
end end
SRC SRC
inspect_source(lacks_type)
expect(cop.offenses.size).to eq 1
end end
it 'does not add an offense for resolvers that have a type call' do it 'does not add an offense for resolvers that have a type call' do
...@@ -41,9 +36,10 @@ RSpec.describe RuboCop::Cop::Graphql::ResolverType do ...@@ -41,9 +36,10 @@ RSpec.describe RuboCop::Cop::Graphql::ResolverType do
end end
it 'ignores type calls on other objects' do it 'ignores type calls on other objects' do
lacks_type = <<-SRC expect_offense(<<~SRC)
module Resolvers module Resolvers
class FooResolver < BaseResolver class FooResolver < BaseResolver
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Missing type annotation: Please add `type` DSL method call. e.g: type UserType.connection_type, null: true
class FalsePositive < BaseObject class FalsePositive < BaseObject
type RedHerringType, null: true type RedHerringType, null: true
end end
...@@ -54,10 +50,6 @@ RSpec.describe RuboCop::Cop::Graphql::ResolverType do ...@@ -54,10 +50,6 @@ RSpec.describe RuboCop::Cop::Graphql::ResolverType do
end end
end end
SRC SRC
inspect_source(lacks_type)
expect(cop.offenses.size).to eq 1
end end
it 'does not add an offense unless the class is named using the Resolver convention' do it 'does not add an offense unless the class is named using the Resolver convention' do
......
...@@ -6,7 +6,7 @@ require 'parser/current' ...@@ -6,7 +6,7 @@ require 'parser/current'
require_relative '../../rubocop/qa_helpers' require_relative '../../rubocop/qa_helpers'
RSpec.describe RuboCop::QAHelpers do RSpec.describe RuboCop::QAHelpers do
def parse_source(source, path = 'foo.rb') def build_and_parse_source(source, path = 'foo.rb')
buffer = Parser::Source::Buffer.new(path) buffer = Parser::Source::Buffer.new(path)
buffer.source = source buffer.source = source
...@@ -24,13 +24,13 @@ RSpec.describe RuboCop::QAHelpers do ...@@ -24,13 +24,13 @@ RSpec.describe RuboCop::QAHelpers do
describe '#in_qa_file?' do describe '#in_qa_file?' do
it 'returns true for a node in the qa/ directory' do it 'returns true for a node in the qa/ directory' do
node = parse_source('10', rails_root_join('qa', 'qa', 'page', 'dashboard', 'groups.rb')) node = build_and_parse_source('10', rails_root_join('qa', 'qa', 'page', 'dashboard', 'groups.rb'))
expect(cop.in_qa_file?(node)).to eq(true) expect(cop.in_qa_file?(node)).to eq(true)
end end
it 'returns false for a node outside the qa/ directory' do it 'returns false for a node outside the qa/ directory' do
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb')) node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_qa_file?(node)).to eq(false) expect(cop.in_qa_file?(node)).to eq(false)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment