diff options
Diffstat (limited to 'spec/rubocop')
7 files changed, 328 insertions, 14 deletions
diff --git a/spec/rubocop/cop/gitlab/policy_rule_boolean_spec.rb b/spec/rubocop/cop/gitlab/policy_rule_boolean_spec.rb new file mode 100644 index 00000000000..6221d038512 --- /dev/null +++ b/spec/rubocop/cop/gitlab/policy_rule_boolean_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'rubocop' +require 'rubocop/rspec/support' +require_relative '../../../../rubocop/cop/gitlab/policy_rule_boolean' + +RSpec.describe RuboCop::Cop::Gitlab::PolicyRuleBoolean, type: :rubocop do + include CopHelper + + subject(:cop) { described_class.new } + + it 'registers offense for &&' do + expect_offense(<<~SOURCE) + rule { conducts_electricity && batteries }.enable :light_bulb + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ && is not allowed within a rule block. Did you mean to use `&`? + SOURCE + end + + it 'registers offense for ||' do + expect_offense(<<~SOURCE) + rule { conducts_electricity || batteries }.enable :light_bulb + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ || is not allowed within a rule block. Did you mean to use `|`? + SOURCE + end + + it 'registers offense for if' do + expect_offense(<<~SOURCE) + rule { if conducts_electricity then can?(:magnetize) else batteries end }.enable :motor + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ if and ternary operators are not allowed within a rule block. + SOURCE + end + + it 'registers offense for ternary operator' do + expect_offense(<<~SOURCE) + rule { conducts_electricity ? can?(:magnetize) : batteries }.enable :motor + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ if and ternary operators are not allowed within a rule block. + SOURCE + end + + it 'registers no offense for &' do + expect_no_offenses(<<~SOURCE) + rule { conducts_electricity & batteries }.enable :light_bulb + SOURCE + end + + it 'registers no offense for |' do + expect_no_offenses(<<~SOURCE) + rule { conducts_electricity | batteries }.enable :light_bulb + SOURCE + end +end diff --git a/spec/rubocop/cop/graphql/descriptions_spec.rb b/spec/rubocop/cop/graphql/descriptions_spec.rb index 3b29cd2fbee..f4693057bcb 100644 --- a/spec/rubocop/cop/graphql/descriptions_spec.rb +++ b/spec/rubocop/cop/graphql/descriptions_spec.rb @@ -10,7 +10,7 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions, type: :rubocop do subject(:cop) { described_class.new } context 'fields' do - it 'adds an offense when there is no field description' do + it 'adds an offense when there is no description' do inspect_source(<<~TYPE) module Types class FakeType < BaseObject @@ -24,24 +24,37 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions, type: :rubocop do expect(cop.offenses.size).to eq 1 end - it 'does not add an offense for fields with a description' do - expect_no_offenses(<<~TYPE.strip) + it 'adds an offense when description does not end in a period' do + inspect_source(<<~TYPE) module Types class FakeType < BaseObject - graphql_name 'FakeTypeName' - - argument :a_thing, + field :a_thing, 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 + expect_no_offenses(<<~TYPE.strip) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::STRING_TYPE, + null: false, + description: 'A descriptive description.' + end + end + TYPE end end context 'arguments' do - it 'adds an offense when there is no argument description' do + it 'adds an offense when there is no description' do inspect_source(<<~TYPE) module Types class FakeType < BaseObject @@ -55,19 +68,88 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions, type: :rubocop do expect(cop.offenses.size).to eq 1 end - it 'does not add an offense for arguments with a description' do - expect_no_offenses(<<~TYPE.strip) + it 'adds an offense when description does not end in a period' do + inspect_source(<<~TYPE) module Types class FakeType < BaseObject - graphql_name 'FakeTypeName' + argument :a_thing, + 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 + expect_no_offenses(<<~TYPE.strip) + module Types + class FakeType < BaseObject argument :a_thing, GraphQL::STRING_TYPE, null: false, + description: 'Behold! A description.' + end + end + TYPE + end + end + + describe 'autocorrecting descriptions without periods' do + it 'can autocorrect' do + expect_offense(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + ^^^^^^^^^^^^^^^ `description` strings must end with a `.`. + GraphQL::STRING_TYPE, + null: false, description: 'Behold! A description' end end TYPE + + expect_correction(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::STRING_TYPE, + null: false, + description: 'Behold! A description.' + end + end + TYPE + end + + it 'can autocorrect a heredoc' do + expect_offense(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + ^^^^^^^^^^^^^^^ `description` strings must end with a `.`. + GraphQL::STRING_TYPE, + null: false, + description: <<~DESC + Behold! A description + DESC + end + end + TYPE + + expect_correction(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::STRING_TYPE, + null: false, + description: <<~DESC + Behold! A description. + DESC + end + end + TYPE end end end diff --git a/spec/rubocop/cop/include_sidekiq_worker_spec.rb b/spec/rubocop/cop/include_sidekiq_worker_spec.rb index 8d056c6a13e..33737babee5 100644 --- a/spec/rubocop/cop/include_sidekiq_worker_spec.rb +++ b/spec/rubocop/cop/include_sidekiq_worker_spec.rb @@ -16,7 +16,7 @@ RSpec.describe RuboCop::Cop::IncludeSidekiqWorker, type: :rubocop do let(:source) { 'include Sidekiq::Worker' } let(:correct_source) { 'include ApplicationWorker' } - it 'registers an offense ' do + it 'registers an offense' do inspect_source(source) aggregate_failures do diff --git a/spec/rubocop/cop/lint/last_keyword_argument_spec.rb b/spec/rubocop/cop/lint/last_keyword_argument_spec.rb new file mode 100644 index 00000000000..5822bf74e8d --- /dev/null +++ b/spec/rubocop/cop/lint/last_keyword_argument_spec.rb @@ -0,0 +1,138 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'rubocop' +require_relative '../../../../rubocop/cop/lint/last_keyword_argument' + +RSpec.describe RuboCop::Cop::Lint::LastKeywordArgument, type: :rubocop do + include CopHelper + + subject(:cop) { described_class.new } + + before do + described_class.instance_variable_set(:@keyword_warnings, nil) + end + + context 'deprecation files does not exist' do + before do + allow(Dir).to receive(:glob).and_return([]) + allow(File).to receive(:exist?).and_return(false) + end + + it 'does not register an offense' do + expect_no_offenses(<<~SOURCE) + users.call(params) + SOURCE + end + end + + context 'deprecation files does exist' do + let(:create_spec_yaml) do + <<~YAML + --- + test_mutations/boards/lists/create#resolve_with_proper_permissions_backlog_list_creates_one_and_only_one_backlog: + - | + DEPRECATION WARNING: /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/batch-loader-1.4.0/lib/batch_loader/graphql.rb:38: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call + /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/batch-loader-1.4.0/lib/batch_loader.rb:26: warning: The called method `batch' is defined here + test_mutations/boards/lists/create#ready?_raises_an_error_if_required_arguments_are_missing: + - | + DEPRECATION WARNING: /Users/tkuah/code/ee-gdk/gitlab/create_service.rb:1: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call + /Users/tkuah/code/ee-gdk/gitlab/user.rb:17: warning: The called method `call' is defined here + YAML + end + + let(:projects_spec_yaml) do + <<~YAML + --- + test_api/projects_get_/projects_when_unauthenticated_behaves_like_projects_response_returns_an_array_of_projects: + - | + DEPRECATION WARNING: /Users/tkuah/code/ee-gdk/gitlab/projects_spec.rb:1: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call + /Users/tkuah/code/ee-gdk/gitlab/lib/gitlab/project.rb:15: warning: The called method `initialize' is defined here + - | + DEPRECATION WARNING: /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/state_machines-activerecord-0.6.0/lib/state_machines/integrations/active_record.rb:511: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call + /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/suppressor.rb:43: warning: The called method `save' is defined here + - | + DEPRECATION WARNING: /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rack-2.2.3/lib/rack/builder.rb:158: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call + /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/grape-1.4.0/lib/grape/middleware/error.rb:30: warning: The called method `initialize' is defined here + YAML + end + + before do + allow(Dir).to receive(:glob).and_return(['deprecations/service/create_spec.yml', 'deprecations/api/projects_spec.yml']) + allow(File).to receive(:read).and_return(create_spec_yaml, projects_spec_yaml) + end + + it 'registers an offense' do + expect_offense(<<~SOURCE, 'create_service.rb') + users.call(params) + ^^^^^^ Using the last argument as keyword parameters is deprecated + SOURCE + + expect_correction(<<~SOURCE) + users.call(**params) + SOURCE + end + + it 'registers an offense for the new method call' do + expect_offense(<<~SOURCE, 'projects_spec.rb') + Project.new(params) + ^^^^^^ Using the last argument as keyword parameters is deprecated + SOURCE + + expect_correction(<<~SOURCE) + Project.new(**params) + SOURCE + end + + it 'registers an offense and corrects by converting hash to kwarg' do + expect_offense(<<~SOURCE, 'create_service.rb') + users.call(id, { a: :b, c: :d }) + ^^^^^^^^^^^^^^^^ Using the last argument as keyword parameters is deprecated + SOURCE + + expect_correction(<<~SOURCE) + users.call(id, a: :b, c: :d) + SOURCE + end + + it 'registers an offense and corrects by converting splat to double splat' do + expect_offense(<<~SOURCE, 'create_service.rb') + users.call(id, *params) + ^^^^^^^ Using the last argument as keyword parameters is deprecated + SOURCE + + expect_correction(<<~SOURCE) + users.call(id, **params) + SOURCE + end + + it 'does not register an offense if already a kwarg', :aggregate_failures do + expect_no_offenses(<<~SOURCE, 'create_service.rb') + users.call(**params) + SOURCE + + expect_no_offenses(<<~SOURCE, 'create_service.rb') + users.call(id, a: :b, c: :d) + SOURCE + end + + it 'does not register an offense if the method name does not match' do + expect_no_offenses(<<~SOURCE, 'create_service.rb') + users.process(params) + SOURCE + end + + it 'does not register an offense if the line number does not match' do + expect_no_offenses(<<~SOURCE, 'create_service.rb') + users.process + users.call(params) + SOURCE + end + + it 'does not register an offense if the filename does not match' do + expect_no_offenses(<<~SOURCE, 'update_service.rb') + users.call(params) + SOURCE + end + end +end diff --git a/spec/rubocop/cop/migration/add_column_with_default_spec.rb b/spec/rubocop/cop/migration/add_column_with_default_spec.rb index 50af344e0d4..6deb092f235 100644 --- a/spec/rubocop/cop/migration/add_column_with_default_spec.rb +++ b/spec/rubocop/cop/migration/add_column_with_default_spec.rb @@ -26,7 +26,7 @@ RSpec.describe RuboCop::Cop::Migration::AddColumnWithDefault, type: :rubocop do let(:offense) { '`add_column_with_default` is deprecated, use `add_column` instead' } - it 'registers an offense ' do + it 'registers an offense' do expect_offense(<<~RUBY) def up add_column_with_default(:merge_request_diff_files, :artifacts, :boolean, default: true, allow_null: false) diff --git a/spec/rubocop/cop/migration/create_table_with_foreign_keys_spec.rb b/spec/rubocop/cop/migration/create_table_with_foreign_keys_spec.rb index 93f43b0feb0..eaaa50b8190 100644 --- a/spec/rubocop/cop/migration/create_table_with_foreign_keys_spec.rb +++ b/spec/rubocop/cop/migration/create_table_with_foreign_keys_spec.rb @@ -88,7 +88,7 @@ RSpec.describe RuboCop::Cop::Migration::CreateTableWithForeignKeys, type: :ruboc shared_examples 'target to high traffic table' do |dsl_method, table_name| context 'when the target is defined as option' do - it 'registers an offense ' do + it 'registers an offense' do expect_offense(<<~RUBY) def up create_table(:foo) do |t| @@ -102,7 +102,7 @@ RSpec.describe RuboCop::Cop::Migration::CreateTableWithForeignKeys, type: :ruboc end context 'when the target has implicit definition' do - it 'registers an offense ' do + it 'registers an offense' do expect_offense(<<~RUBY) def up create_table(:foo) do |t| diff --git a/spec/rubocop/cop/rspec/htt_party_basic_auth_spec.rb b/spec/rubocop/cop/rspec/htt_party_basic_auth_spec.rb new file mode 100644 index 00000000000..8c3703a488a --- /dev/null +++ b/spec/rubocop/cop/rspec/htt_party_basic_auth_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +require_relative '../../../../rubocop/cop/rspec/httparty_basic_auth' + +RSpec.describe RuboCop::Cop::RSpec::HTTPartyBasicAuth, type: :rubocop do + include CopHelper + + subject(:cop) { described_class.new } + + context 'when passing `basic_auth: { user: ... }`' do + it 'registers an offence' do + expect_offense(<<~SOURCE, 'spec/foo.rb') + HTTParty.put( + url, + basic_auth: { user: user, password: token }, + ^^^^ #{described_class::MESSAGE} + body: body + ) + SOURCE + end + + it 'can autocorrect the source' do + bad = 'HTTParty.put(url, basic_auth: { user: user, password: token })' + good = 'HTTParty.put(url, basic_auth: { username: user, password: token })' + expect(autocorrect_source(bad)).to eq(good) + end + end + + context 'when passing `basic_auth: { username: ... }`' do + it 'does not register an offence' do + expect_no_offenses(<<~SOURCE, 'spec/frontend/fixtures/foo.rb') + HTTParty.put( + url, + basic_auth: { username: user, password: token }, + body: body + ) + SOURCE + end + end +end |