diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-20 12:10:52 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-20 12:10:52 +0300 |
commit | 8994750e4e1beb9f6d1b37c1625fba5868b9babf (patch) | |
tree | 57c6b4e91ceff31047a15f0be5bbdc5ef9129ef3 /spec/rubocop | |
parent | 4ca378cac72f532536a84158b3ee0da80e602151 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop')
-rw-r--r-- | spec/rubocop/code_reuse_helpers_spec.rb | 56 | ||||
-rw-r--r-- | spec/rubocop/cop/api/base_spec.rb | 5 | ||||
-rw-r--r-- | spec/rubocop/cop/api/grape_array_missing_coerce_spec.rb | 28 | ||||
-rw-r--r-- | spec/rubocop/cop/code_reuse/finder_spec.rb | 5 | ||||
-rw-r--r-- | spec/rubocop/cop/code_reuse/presenter_spec.rb | 3 | ||||
-rw-r--r-- | spec/rubocop/cop/code_reuse/serializer_spec.rb | 3 | ||||
-rw-r--r-- | spec/rubocop/cop/code_reuse/service_class_spec.rb | 3 | ||||
-rw-r--r-- | spec/rubocop/cop/code_reuse/worker_spec.rb | 3 | ||||
-rw-r--r-- | spec/rubocop/cop/graphql/authorize_types_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubocop/cop/graphql/descriptions_spec.rb | 22 | ||||
-rw-r--r-- | spec/rubocop/cop/graphql/gid_expected_type_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubocop/cop/graphql/id_type_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubocop/cop/graphql/json_type_spec.rb | 24 | ||||
-rw-r--r-- | spec/rubocop/cop/graphql/resolver_type_spec.rb | 18 | ||||
-rw-r--r-- | spec/rubocop/qa_helpers_spec.rb | 6 |
15 files changed, 75 insertions, 122 deletions
diff --git a/spec/rubocop/code_reuse_helpers_spec.rb b/spec/rubocop/code_reuse_helpers_spec.rb index 574a4a85a34..4c3dd8f8167 100644 --- a/spec/rubocop/code_reuse_helpers_spec.rb +++ b/spec/rubocop/code_reuse_helpers_spec.rb @@ -6,7 +6,7 @@ require 'parser/current' require_relative '../../rubocop/code_reuse_helpers' 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.source = source @@ -24,13 +24,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#send_to_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) end 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) end @@ -38,13 +38,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#send_receiver_name_ends_with?' 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) end 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) end @@ -52,7 +52,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#file_path_for_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) expect(path).to eq('foo.rb') @@ -61,7 +61,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#name_of_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) end @@ -69,13 +69,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#in_finder?' 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) end 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) end @@ -83,13 +83,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#in_model?' 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) end 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) end @@ -97,13 +97,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#in_service_class?' 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) end 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) end @@ -111,13 +111,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#in_presenter?' 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) end 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) end @@ -125,13 +125,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#in_serializer?' 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) end 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) end @@ -139,13 +139,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#in_worker?' 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) end 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) end @@ -153,13 +153,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#in_api?' 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) end 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) end @@ -167,21 +167,21 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#in_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) end it 'returns true for a directory in the EE app/ directory' do 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) end it 'returns false for a directory in the lib/ directory' do 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) end @@ -189,7 +189,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#name_of_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') end @@ -197,7 +197,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#each_class_method' do it 'yields every class method to the supplied block' do - node = parse_source(<<~RUBY) + node = build_and_parse_source(<<~RUBY) class Foo class << self def first @@ -220,7 +220,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#each_send_node' 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 expect(nodes.length).to eq(2) @@ -231,7 +231,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do describe '#disallow_send_to' do it 'disallows sending a message to a constant' do - def_node = parse_source(<<~RUBY) + def_node = build_and_parse_source(<<~RUBY) def foo FooFinder.new end diff --git a/spec/rubocop/cop/api/base_spec.rb b/spec/rubocop/cop/api/base_spec.rb index de05ab93874..ec646b9991b 100644 --- a/spec/rubocop/cop/api/base_spec.rb +++ b/spec/rubocop/cop/api/base_spec.rb @@ -2,12 +2,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/api/base' RSpec.describe RuboCop::Cop::API::Base do - include CopHelper - subject(:cop) { described_class.new } let(:corrected) do @@ -17,7 +14,7 @@ RSpec.describe RuboCop::Cop::API::Base do CORRECTED 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 expect_offense(<<~CODE) class SomeAPI < #{offense} diff --git a/spec/rubocop/cop/api/grape_array_missing_coerce_spec.rb b/spec/rubocop/cop/api/grape_array_missing_coerce_spec.rb index c7bb8255398..b50866b54b3 100644 --- a/spec/rubocop/cop/api/grape_array_missing_coerce_spec.rb +++ b/spec/rubocop/cop/api/grape_array_missing_coerce_spec.rb @@ -5,36 +5,38 @@ require 'rubocop' require_relative '../../../../rubocop/cop/api/grape_array_missing_coerce' 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 } it 'adds an offense with a required parameter' do - inspect_source(<<~CODE) + expect_offense(<<~TYPE) class SomeAPI < Grape::API::Instance params do requires :values, type: Array[String] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end end - CODE - - expect(cop.offenses.size).to eq(1) + TYPE end it 'adds an offense with an optional parameter' do - inspect_source(<<~CODE) + expect_offense(<<~TYPE) class SomeAPI < Grape::API::Instance params do optional :values, type: Array[String] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end end - CODE - - expect(cop.offenses.size).to eq(1) + TYPE end it 'does not add an offense' do - inspect_source(<<~CODE) + expect_no_offenses(<<~CODE) class SomeAPI < Grape::API::Instance params do requires :values, type: Array[String], coerce_with: ->(val) { val.split(',').map(&:strip) } @@ -44,19 +46,15 @@ RSpec.describe RuboCop::Cop::API::GrapeArrayMissingCoerce do end end CODE - - expect(cop.offenses.size).to be_zero end it 'does not add an offense for unrelated classes' do - inspect_source(<<~CODE) + expect_no_offenses(<<~CODE) class SomeClass params do requires :values, type: Array[String] end end CODE - - expect(cop.offenses.size).to be_zero end end diff --git a/spec/rubocop/cop/code_reuse/finder_spec.rb b/spec/rubocop/cop/code_reuse/finder_spec.rb index 6f04d5e0d60..484a1549a89 100644 --- a/spec/rubocop/cop/code_reuse/finder_spec.rb +++ b/spec/rubocop/cop/code_reuse/finder_spec.rb @@ -2,12 +2,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/code_reuse/finder' RSpec.describe RuboCop::Cop::CodeReuse::Finder do - include CopHelper - subject(:cop) { described_class.new } it 'flags the use of a Finder inside another Finder' do @@ -23,8 +20,6 @@ RSpec.describe RuboCop::Cop::CodeReuse::Finder do end end SOURCE - - expect(cop.offenses.size).to eq(1) end it 'flags the use of a Finder inside a model class method' do diff --git a/spec/rubocop/cop/code_reuse/presenter_spec.rb b/spec/rubocop/cop/code_reuse/presenter_spec.rb index 8efd4da8aa1..4639854588e 100644 --- a/spec/rubocop/cop/code_reuse/presenter_spec.rb +++ b/spec/rubocop/cop/code_reuse/presenter_spec.rb @@ -2,12 +2,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/code_reuse/presenter' RSpec.describe RuboCop::Cop::CodeReuse::Presenter do - include CopHelper - subject(:cop) { described_class.new } it 'flags the use of a Presenter in a Service class' do diff --git a/spec/rubocop/cop/code_reuse/serializer_spec.rb b/spec/rubocop/cop/code_reuse/serializer_spec.rb index 74999df5859..84db2e62b41 100644 --- a/spec/rubocop/cop/code_reuse/serializer_spec.rb +++ b/spec/rubocop/cop/code_reuse/serializer_spec.rb @@ -2,12 +2,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/code_reuse/serializer' RSpec.describe RuboCop::Cop::CodeReuse::Serializer do - include CopHelper - subject(:cop) { described_class.new } it 'flags the use of a Serializer in a Service class' do diff --git a/spec/rubocop/cop/code_reuse/service_class_spec.rb b/spec/rubocop/cop/code_reuse/service_class_spec.rb index 4870daf72dc..b6d94dd749f 100644 --- a/spec/rubocop/cop/code_reuse/service_class_spec.rb +++ b/spec/rubocop/cop/code_reuse/service_class_spec.rb @@ -2,12 +2,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/code_reuse/service_class' RSpec.describe RuboCop::Cop::CodeReuse::ServiceClass do - include CopHelper - subject(:cop) { described_class.new } it 'flags the use of a Service class in a Finder' do diff --git a/spec/rubocop/cop/code_reuse/worker_spec.rb b/spec/rubocop/cop/code_reuse/worker_spec.rb index 9e015f286d8..42c9303a93b 100644 --- a/spec/rubocop/cop/code_reuse/worker_spec.rb +++ b/spec/rubocop/cop/code_reuse/worker_spec.rb @@ -2,12 +2,9 @@ require 'fast_spec_helper' require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/code_reuse/worker' RSpec.describe RuboCop::Cop::CodeReuse::Worker do - include CopHelper - subject(:cop) { described_class.new } it 'flags the use of a worker in a controller' do diff --git a/spec/rubocop/cop/graphql/authorize_types_spec.rb b/spec/rubocop/cop/graphql/authorize_types_spec.rb index a1b7a3f3a9b..9242b865b20 100644 --- a/spec/rubocop/cop/graphql/authorize_types_spec.rb +++ b/spec/rubocop/cop/graphql/authorize_types_spec.rb @@ -6,21 +6,18 @@ require 'rubocop' require_relative '../../../../rubocop/cop/graphql/authorize_types' RSpec.describe RuboCop::Cop::Graphql::AuthorizeTypes do - include CopHelper - subject(:cop) { described_class.new } it 'adds an offense when there is no authorize call' do - inspect_source(<<~TYPE) + expect_offense(<<~TYPE) module Types 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 :another_thing end end TYPE - - expect(cop.offenses.size).to eq 1 end it 'does not add an offense for classes that have an authorize call' do diff --git a/spec/rubocop/cop/graphql/descriptions_spec.rb b/spec/rubocop/cop/graphql/descriptions_spec.rb index b44205b0920..9ad40fad83d 100644 --- a/spec/rubocop/cop/graphql/descriptions_spec.rb +++ b/spec/rubocop/cop/graphql/descriptions_spec.rb @@ -5,38 +5,34 @@ require 'rubocop' require_relative '../../../../rubocop/cop/graphql/descriptions' RSpec.describe RuboCop::Cop::Graphql::Descriptions do - include CopHelper - subject(:cop) { described_class.new } context 'fields' do it 'adds an offense when there is no description' do - inspect_source(<<~TYPE) + expect_offense(<<~TYPE) module Types class FakeType < BaseObject field :a_thing, + ^^^^^^^^^^^^^^^ Please add a `description` property. GraphQL::STRING_TYPE, null: false end end TYPE - - expect(cop.offenses.size).to eq 1 end it 'adds an offense when description does not end in a period' do - inspect_source(<<~TYPE) + expect_offense(<<~TYPE) module Types class FakeType < BaseObject field :a_thing, + ^^^^^^^^^^^^^^^ `description` strings must end with a `.`. GraphQL::STRING_TYPE, null: false, description: 'A descriptive description' end end TYPE - - expect(cop.offenses.size).to eq 1 end it 'does not add an offense when description is correct' do @@ -55,32 +51,30 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do context 'arguments' do it 'adds an offense when there is no description' do - inspect_source(<<~TYPE) + expect_offense(<<~TYPE) module Types class FakeType < BaseObject argument :a_thing, + ^^^^^^^^^^^^^^^^^^ Please add a `description` property. GraphQL::STRING_TYPE, null: false end end TYPE - - expect(cop.offenses.size).to eq 1 end it 'adds an offense when description does not end in a period' do - inspect_source(<<~TYPE) + expect_offense(<<~TYPE) module Types class FakeType < BaseObject argument :a_thing, + ^^^^^^^^^^^^^^^^^^ `description` strings must end with a `.`. GraphQL::STRING_TYPE, null: false, description: 'Behold! A description' end end TYPE - - expect(cop.offenses.size).to eq 1 end it 'does not add an offense when description is correct' do diff --git a/spec/rubocop/cop/graphql/gid_expected_type_spec.rb b/spec/rubocop/cop/graphql/gid_expected_type_spec.rb index 8fd7ae03748..d9a129244d6 100644 --- a/spec/rubocop/cop/graphql/gid_expected_type_spec.rb +++ b/spec/rubocop/cop/graphql/gid_expected_type_spec.rb @@ -6,16 +6,13 @@ require 'rubocop' require_relative '../../../../rubocop/cop/graphql/gid_expected_type' RSpec.describe RuboCop::Cop::Graphql::GIDExpectedType do - include CopHelper - subject(:cop) { described_class.new } it 'adds an offense when there is no expected_type parameter' do - inspect_source(<<~TYPE) + expect_offense(<<~TYPE) GitlabSchema.object_from_id(received_id) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an expected_type parameter to #object_from_id calls if possible. TYPE - - expect(cop.offenses.size).to eq 1 end it 'does not add an offense for calls that have an expected_type parameter' do diff --git a/spec/rubocop/cop/graphql/id_type_spec.rb b/spec/rubocop/cop/graphql/id_type_spec.rb index 6135c9fef43..93c01cd7f06 100644 --- a/spec/rubocop/cop/graphql/id_type_spec.rb +++ b/spec/rubocop/cop/graphql/id_type_spec.rb @@ -6,16 +6,13 @@ require 'rubocop' require_relative '../../../../rubocop/cop/graphql/id_type' RSpec.describe RuboCop::Cop::Graphql::IDType do - include CopHelper - subject(:cop) { described_class.new } 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 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use GraphQL::ID_TYPE, use a specific GlobalIDType instead TYPE - - expect(cop.offenses.size).to eq 1 end context 'whitelisted arguments' do diff --git a/spec/rubocop/cop/graphql/json_type_spec.rb b/spec/rubocop/cop/graphql/json_type_spec.rb index 6d9f86e44d2..91838c1708e 100644 --- a/spec/rubocop/cop/graphql/json_type_spec.rb +++ b/spec/rubocop/cop/graphql/json_type_spec.rb @@ -5,29 +5,29 @@ require 'rubocop' require_relative '../../../../rubocop/cop/graphql/json_type' 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 } context 'fields' do it 'adds an offense when GraphQL::Types::JSON is used' do - inspect_source(<<~RUBY.strip) + expect_offense(<<~RUBY) class MyType field :some_field, GraphQL::Types::JSON + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} 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) + expect_offense(<<~RUBY) class MyType field :some_field, GraphQL::Types::JSON, null: true, description: 'My description' + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end RUBY - - expect(cop.offenses.size).to eq(1) end it 'does not add an offense for other types' do @@ -41,23 +41,21 @@ RSpec.describe RuboCop::Cop::Graphql::JSONType do context 'arguments' do it 'adds an offense when GraphQL::Types::JSON is used' do - inspect_source(<<~RUBY.strip) + expect_offense(<<~RUBY) class MyType argument :some_arg, GraphQL::Types::JSON + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} 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) + expect_offense(<<~RUBY) class MyType argument :some_arg, GraphQL::Types::JSON, null: true, description: 'My description' + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end RUBY - - expect(cop.offenses.size).to eq(1) end it 'does not add an offense for other types' do diff --git a/spec/rubocop/cop/graphql/resolver_type_spec.rb b/spec/rubocop/cop/graphql/resolver_type_spec.rb index 25213e30528..11c0ad284a9 100644 --- a/spec/rubocop/cop/graphql/resolver_type_spec.rb +++ b/spec/rubocop/cop/graphql/resolver_type_spec.rb @@ -6,24 +6,19 @@ require 'rubocop' require_relative '../../../../rubocop/cop/graphql/resolver_type' RSpec.describe RuboCop::Cop::Graphql::ResolverType do - include CopHelper - subject(:cop) { described_class.new } - it 'adds an offense when there is no type annotaion' do - lacks_type = <<-SRC + it 'adds an offense when there is no type annotation' do + expect_offense(<<~SRC) module Resolvers class FooResolver < BaseResolver + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Missing type annotation: Please add `type` DSL method call. e.g: type UserType.connection_type, null: true 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 @@ -41,9 +36,10 @@ RSpec.describe RuboCop::Cop::Graphql::ResolverType do end it 'ignores type calls on other objects' do - lacks_type = <<-SRC + expect_offense(<<~SRC) module Resolvers class FooResolver < BaseResolver + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Missing type annotation: Please add `type` DSL method call. e.g: type UserType.connection_type, null: true class FalsePositive < BaseObject type RedHerringType, null: true end @@ -54,10 +50,6 @@ RSpec.describe RuboCop::Cop::Graphql::ResolverType do 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 diff --git a/spec/rubocop/qa_helpers_spec.rb b/spec/rubocop/qa_helpers_spec.rb index 051817903a8..cf6d2f1a845 100644 --- a/spec/rubocop/qa_helpers_spec.rb +++ b/spec/rubocop/qa_helpers_spec.rb @@ -6,7 +6,7 @@ require 'parser/current' require_relative '../../rubocop/qa_helpers' 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.source = source @@ -24,13 +24,13 @@ RSpec.describe RuboCop::QAHelpers do describe '#in_qa_file?' 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) end 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) end |