diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
commit | 9dc93a4519d9d5d7be48ff274127136236a3adb3 (patch) | |
tree | 70467ae3692a0e35e5ea56bcb803eb512a10bedb /spec/rubocop | |
parent | 4b0f34b6d759d6299322b3a54453e930c6121ff0 (diff) | |
download | gitlab-ce-9dc93a4519d9d5d7be48ff274127136236a3adb3.tar.gz |
Add latest changes from gitlab-org/gitlab@13-11-stable-eev13.11.0-rc43
Diffstat (limited to 'spec/rubocop')
13 files changed, 306 insertions, 76 deletions
diff --git a/spec/rubocop/cop/gitlab/delegate_predicate_methods_spec.rb b/spec/rubocop/cop/gitlab/delegate_predicate_methods_spec.rb new file mode 100644 index 00000000000..1ceff0dd681 --- /dev/null +++ b/spec/rubocop/cop/gitlab/delegate_predicate_methods_spec.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require_relative '../../../../rubocop/cop/gitlab/delegate_predicate_methods' + +RSpec.describe RuboCop::Cop::Gitlab::DelegatePredicateMethods do + subject(:cop) { described_class.new } + + it 'registers offense for single predicate method with allow_nil:true' do + expect_offense(<<~SOURCE) + delegate :is_foo?, :do_foo, to: :bar, allow_nil: true + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Using `delegate` with `allow_nil` on the following predicate methods is discouraged: is_foo?. + SOURCE + end + + it 'registers offense for multiple predicate methods with allow_nil:true' do + expect_offense(<<~SOURCE) + delegate :is_foo?, :is_bar?, to: :bar, allow_nil: true + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Using `delegate` with `allow_nil` on the following predicate methods is discouraged: is_foo?, is_bar?. + SOURCE + end + + it 'registers no offense for non-predicate method with allow_nil:true' do + expect_no_offenses(<<~SOURCE) + delegate :do_foo, to: :bar, allow_nil: true + SOURCE + end + + it 'registers no offense with predicate method with allow_nil:false' do + expect_no_offenses(<<~SOURCE) + delegate :is_foo?, to: :bar, allow_nil: false + SOURCE + end + + it 'registers no offense with predicate method without allow_nil' do + expect_no_offenses(<<~SOURCE) + delegate :is_foo?, to: :bar + SOURCE + end +end diff --git a/spec/rubocop/cop/gitlab/feature_available_usage_spec.rb b/spec/rubocop/cop/gitlab/feature_available_usage_spec.rb new file mode 100644 index 00000000000..514ef357785 --- /dev/null +++ b/spec/rubocop/cop/gitlab/feature_available_usage_spec.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'rubocop' +require 'rubocop/rspec/support' +require_relative '../../../../rubocop/cop/gitlab/feature_available_usage' + +RSpec.describe RuboCop::Cop::Gitlab::FeatureAvailableUsage do + subject(:cop) { described_class.new } + + context 'no arguments given' do + it 'does not flag the use of Gitlab::Sourcegraph.feature_available? with no arguments' do + expect_no_offenses('Gitlab::Sourcegraph.feature_available?') + expect_no_offenses('subject { described_class.feature_available? }') + end + end + + context 'one argument given' do + it 'does not flag the use of License.feature_available?' do + expect_no_offenses('License.feature_available?(:push_rules)') + end + + it 'flags the use with a dynamic feature as nil' do + expect_offense(<<~SOURCE) + feature_available?(nil) + ^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should not be called for features that can be licensed (`nil` given), use `licensed_feature_available?(feature)` instead. + SOURCE + expect_offense(<<~SOURCE) + project.feature_available?(nil) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should not be called for features that can be licensed (`nil` given), use `licensed_feature_available?(feature)` instead. + SOURCE + end + + it 'flags the use with an OSS project feature' do + expect_offense(<<~SOURCE) + project.feature_available?(:issues) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should be called with two arguments: `feature` and `user`. + SOURCE + expect_offense(<<~SOURCE) + feature_available?(:issues) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should be called with two arguments: `feature` and `user`. + SOURCE + end + + it 'flags the use with a feature that is not a project feature' do + expect_offense(<<~SOURCE) + feature_available?(:foo) + ^^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should not be called for features that can be licensed (`:foo` given), use `licensed_feature_available?(feature)` instead. + SOURCE + expect_offense(<<~SOURCE) + project.feature_available?(:foo) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should not be called for features that can be licensed (`:foo` given), use `licensed_feature_available?(feature)` instead. + SOURCE + expect_offense(<<~SOURCE) + feature_available?(foo) + ^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should not be called for features that can be licensed (`foo` isn't a literal so we cannot say if it's legit or not), using `licensed_feature_available?(feature)` may be more appropriate. + SOURCE + expect_offense(<<~SOURCE) + foo = :feature + feature_available?(foo) + ^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should not be called for features that can be licensed (`foo` isn't a literal so we cannot say if it's legit or not), using `licensed_feature_available?(feature)` may be more appropriate. + SOURCE + end + end + + context 'two arguments given' do + it 'does not flag the use with an OSS project feature' do + expect_no_offenses('feature_available?(:issues, user)') + expect_no_offenses('project.feature_available?(:issues, user)') + end + + it 'does not flag the use with an EE project feature' do + expect_no_offenses('feature_available?(:requirements, user)') + expect_no_offenses('project.feature_available?(:requirements, user)') + end + + it 'flags the use with a dynamic feature as a method call with two args' do + expect_offense(<<~SOURCE) + feature_available?(:foo, current_user) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should not be called for features that can be licensed (`:foo` given), use `licensed_feature_available?(feature)` instead. + SOURCE + expect_offense(<<~SOURCE) + project.feature_available?(:foo, current_user) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should not be called for features that can be licensed (`:foo` given), use `licensed_feature_available?(feature)` instead. + SOURCE + expect_offense(<<~SOURCE) + feature_available?(foo, current_user) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should not be called for features that can be licensed (`foo` isn't a literal so we cannot say if it's legit or not), using `licensed_feature_available?(feature)` may be more appropriate. + SOURCE + expect_offense(<<~SOURCE) + project.feature_available?(foo, current_user) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `feature_available?` should not be called for features that can be licensed (`foo` isn't a literal so we cannot say if it's legit or not), using `licensed_feature_available?(feature)` may be more appropriate. + SOURCE + end + end +end diff --git a/spec/rubocop/cop/graphql/descriptions_spec.rb b/spec/rubocop/cop/graphql/descriptions_spec.rb index af660aee165..9709a253bdc 100644 --- a/spec/rubocop/cop/graphql/descriptions_spec.rb +++ b/spec/rubocop/cop/graphql/descriptions_spec.rb @@ -6,7 +6,7 @@ require_relative '../../../../rubocop/cop/graphql/descriptions' RSpec.describe RuboCop::Cop::Graphql::Descriptions do subject(:cop) { described_class.new } - context 'fields' do + context 'with fields' do it 'adds an offense when there is no description' do expect_offense(<<~TYPE) module Types @@ -46,9 +46,19 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end TYPE end + + it 'does not add an offense when there is a resolver' do + expect_no_offenses(<<~TYPE.strip) + module Types + class FakeType < BaseObject + field :a_thing, resolver: ThingResolver + end + end + TYPE + end end - context 'arguments' do + context 'with arguments' do it 'adds an offense when there is no description' do expect_offense(<<~TYPE) module Types @@ -90,7 +100,7 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end - context 'enum values' do + context 'with enum values' do it 'adds an offense when there is no description' do expect_offense(<<~TYPE) module Types diff --git a/spec/rubocop/cop/migration/add_limit_to_text_columns_spec.rb b/spec/rubocop/cop/migration/add_limit_to_text_columns_spec.rb index f4695ff8d2d..899872859a9 100644 --- a/spec/rubocop/cop/migration/add_limit_to_text_columns_spec.rb +++ b/spec/rubocop/cop/migration/add_limit_to_text_columns_spec.rb @@ -17,7 +17,6 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do it 'registers an offense' do expect_offense(<<~RUBY) class TestTextLimits < ActiveRecord::Migration[6.0] - DOWNTIME = false disable_ddl_transaction! def up @@ -54,7 +53,6 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do it 'registers no offense' do expect_no_offenses(<<~RUBY) class TestTextLimits < ActiveRecord::Migration[6.0] - DOWNTIME = false disable_ddl_transaction! def up @@ -90,8 +88,6 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do it 'registers no offense' do expect_no_offenses(<<~RUBY) class TestTextLimits < ActiveRecord::Migration[6.0] - DOWNTIME = false - def up create_table :test_text_limits, id: false do |t| t.integer :test_id, null: false @@ -113,7 +109,6 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do it 'registers an offense' do expect_offense(<<~RUBY) class TestTextLimits < ActiveRecord::Migration[6.0] - DOWNTIME = false disable_ddl_transaction! def up @@ -146,7 +141,6 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do it 'registers no offenses' do expect_no_offenses(<<~RUBY) class TestTextLimits < ActiveRecord::Migration[6.0] - DOWNTIME = false disable_ddl_transaction! def up @@ -168,8 +162,6 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do it 'registers no offense' do expect_no_offenses(<<~RUBY) class TestTextLimits < ActiveRecord::Migration[6.0] - DOWNTIME = false - def up drop_table :no_offense_on_down end @@ -194,7 +186,6 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do it 'registers no offense' do expect_no_offenses(<<~RUBY) class TestTextLimits < ActiveRecord::Migration[6.0] - DOWNTIME = false disable_ddl_transaction! def up diff --git a/spec/rubocop/cop/migration/add_timestamps_spec.rb b/spec/rubocop/cop/migration/add_timestamps_spec.rb index ef5a856722f..2a11d46be6e 100644 --- a/spec/rubocop/cop/migration/add_timestamps_spec.rb +++ b/spec/rubocop/cop/migration/add_timestamps_spec.rb @@ -9,8 +9,6 @@ RSpec.describe RuboCop::Cop::Migration::AddTimestamps do let(:migration_with_add_timestamps) do %q( class Users < ActiveRecord::Migration[4.2] - DOWNTIME = false - def change add_column(:users, :username, :text) add_timestamps(:users) @@ -22,8 +20,6 @@ RSpec.describe RuboCop::Cop::Migration::AddTimestamps do let(:migration_without_add_timestamps) do %q( class Users < ActiveRecord::Migration[4.2] - DOWNTIME = false - def change add_column(:users, :username, :text) end @@ -34,8 +30,6 @@ RSpec.describe RuboCop::Cop::Migration::AddTimestamps do let(:migration_with_add_timestamps_with_timezone) do %q( class Users < ActiveRecord::Migration[4.2] - DOWNTIME = false - def change add_column(:users, :username, :text) add_timestamps_with_timezone(:users) @@ -52,8 +46,6 @@ RSpec.describe RuboCop::Cop::Migration::AddTimestamps do it 'registers an offense when the "add_timestamps" method is used' do expect_offense(<<~RUBY) class Users < ActiveRecord::Migration[4.2] - DOWNTIME = false - def change add_column(:users, :username, :text) add_timestamps(:users) diff --git a/spec/rubocop/cop/migration/complex_indexes_require_name_spec.rb b/spec/rubocop/cop/migration/complex_indexes_require_name_spec.rb index 15e947a1e53..ac814c10550 100644 --- a/spec/rubocop/cop/migration/complex_indexes_require_name_spec.rb +++ b/spec/rubocop/cop/migration/complex_indexes_require_name_spec.rb @@ -18,8 +18,6 @@ RSpec.describe RuboCop::Cop::Migration::ComplexIndexesRequireName do it 'registers an offense' do expect_offense(<<~RUBY) class TestComplexIndexes < ActiveRecord::Migration[6.0] - DOWNTIME = false - def up create_table :test_table do |t| t.integer :column1, null: false @@ -46,8 +44,6 @@ RSpec.describe RuboCop::Cop::Migration::ComplexIndexesRequireName do it 'registers no offense' do expect_no_offenses(<<~RUBY) class TestComplexIndexes < ActiveRecord::Migration[6.0] - DOWNTIME = false - def up create_table :test_table do |t| t.integer :column1, null: false @@ -74,8 +70,6 @@ RSpec.describe RuboCop::Cop::Migration::ComplexIndexesRequireName do it 'registers an offense' do expect_offense(<<~RUBY) class TestComplexIndexes < ActiveRecord::Migration[6.0] - DOWNTIME = false - disable_ddl_transaction! def up @@ -101,8 +95,6 @@ RSpec.describe RuboCop::Cop::Migration::ComplexIndexesRequireName do it 'registers no offenses' do expect_no_offenses(<<~RUBY) class TestComplexIndexes < ActiveRecord::Migration[6.0] - DOWNTIME = false - INDEX_NAME = 'my_test_name' disable_ddl_transaction! @@ -135,8 +127,6 @@ RSpec.describe RuboCop::Cop::Migration::ComplexIndexesRequireName do it 'registers no offenses' do expect_no_offenses(<<~RUBY) class TestComplexIndexes < ActiveRecord::Migration[6.0] - DOWNTIME = false - disable_ddl_transaction! def up diff --git a/spec/rubocop/cop/migration/datetime_spec.rb b/spec/rubocop/cop/migration/datetime_spec.rb index 3854ddfe99c..95a875b3baa 100644 --- a/spec/rubocop/cop/migration/datetime_spec.rb +++ b/spec/rubocop/cop/migration/datetime_spec.rb @@ -9,8 +9,6 @@ RSpec.describe RuboCop::Cop::Migration::Datetime do let(:create_table_migration_without_datetime) do %q( class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def change create_table :users do |t| t.string :username, null: false @@ -24,8 +22,6 @@ RSpec.describe RuboCop::Cop::Migration::Datetime do let(:create_table_migration_with_datetime_with_timezone) do %q( class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def change create_table :users do |t| t.string :username, null: false @@ -39,8 +35,6 @@ RSpec.describe RuboCop::Cop::Migration::Datetime do let(:add_column_migration_with_datetime) do %q( class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def change add_column(:users, :username, :text) add_column(:users, :last_sign_in, :datetime) @@ -52,8 +46,6 @@ RSpec.describe RuboCop::Cop::Migration::Datetime do let(:add_column_migration_with_timestamp) do %q( class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def change add_column(:users, :username, :text) add_column(:users, :last_sign_in, :timestamp) @@ -65,8 +57,6 @@ RSpec.describe RuboCop::Cop::Migration::Datetime do let(:add_column_migration_without_datetime) do %q( class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def change add_column(:users, :username, :text) end @@ -77,8 +67,6 @@ RSpec.describe RuboCop::Cop::Migration::Datetime do let(:add_column_migration_with_datetime_with_timezone) do %q( class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def change add_column(:users, :username, :text) add_column(:users, :last_sign_in, :datetime_with_timezone) @@ -95,8 +83,6 @@ RSpec.describe RuboCop::Cop::Migration::Datetime do it 'registers an offense when the ":datetime" data type is used on create_table' do expect_offense(<<~RUBY) class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def change create_table :users do |t| t.string :username, null: false @@ -111,8 +97,6 @@ RSpec.describe RuboCop::Cop::Migration::Datetime do it 'registers an offense when the ":timestamp" data type is used on create_table' do expect_offense(<<~RUBY) class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def change create_table :users do |t| t.string :username, null: false @@ -135,8 +119,6 @@ RSpec.describe RuboCop::Cop::Migration::Datetime do it 'registers an offense when the ":datetime" data type is used on add_column' do expect_offense(<<~RUBY) class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def change add_column(:users, :username, :text) add_column(:users, :last_sign_in, :datetime) @@ -149,8 +131,6 @@ RSpec.describe RuboCop::Cop::Migration::Datetime do it 'registers an offense when the ":timestamp" data type is used on add_column' do expect_offense(<<~RUBY) class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def change add_column(:users, :username, :text) add_column(:users, :last_sign_in, :timestamp) diff --git a/spec/rubocop/cop/migration/prevent_strings_spec.rb b/spec/rubocop/cop/migration/prevent_strings_spec.rb index a9b62f23a77..d1760c2db88 100644 --- a/spec/rubocop/cop/migration/prevent_strings_spec.rb +++ b/spec/rubocop/cop/migration/prevent_strings_spec.rb @@ -15,8 +15,6 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do it 'registers an offense' do expect_offense(<<~RUBY, msg: "Do not use the `string` data type, use `text` instead.[...]") class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def up create_table :users do |t| t.string :username, null: false @@ -46,8 +44,6 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do it 'registers no offense' do expect_no_offenses(<<~RUBY) class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def up create_table :users do |t| t.integer :not_a_string, null: false @@ -65,8 +61,6 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do it 'registers no offense' do expect_no_offenses(<<~RUBY) class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def up create_table :users do |t| t.text :username, null: false @@ -87,8 +81,6 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do it 'registers no offense' do expect_no_offenses(<<~RUBY) class TestStringArrays < ActiveRecord::Migration[6.0] - DOWNTIME = false - def up create_table :test_string_arrays, id: false do |t| t.integer :test_id, null: false @@ -108,8 +100,6 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do it 'registers no offense' do expect_no_offenses(<<~RUBY) class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def up remove_column :users, :bio remove_column :users, :url @@ -137,8 +127,6 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do it 'registers no offense' do expect_no_offenses(<<~RUBY) class Users < ActiveRecord::Migration[6.0] - DOWNTIME = false - def up create_table :users do |t| t.string :username, null: false diff --git a/spec/rubocop/cop/migration/refer_to_index_by_name_spec.rb b/spec/rubocop/cop/migration/refer_to_index_by_name_spec.rb index b3e66492d83..c65f86d1e13 100644 --- a/spec/rubocop/cop/migration/refer_to_index_by_name_spec.rb +++ b/spec/rubocop/cop/migration/refer_to_index_by_name_spec.rb @@ -15,8 +15,6 @@ RSpec.describe RuboCop::Cop::Migration::ReferToIndexByName do it 'registers an offense' do expect_offense(<<~RUBY, msg: 'migration methods that refer to existing indexes must do so by name') class TestReferToIndexByName < ActiveRecord::Migration[6.0] - DOWNTIME = false - INDEX_NAME = 'my_test_name' disable_ddl_transaction! @@ -63,8 +61,6 @@ RSpec.describe RuboCop::Cop::Migration::ReferToIndexByName do it 'registers no offenses' do expect_no_offenses(<<~RUBY) class TestReferToIndexByName < ActiveRecord::Migration[6.0] - DOWNTIME = false - disable_ddl_transaction! def up diff --git a/spec/rubocop/cop/migration/timestamps_spec.rb b/spec/rubocop/cop/migration/timestamps_spec.rb index 91bb5c1b05b..2f99a3ff35b 100644 --- a/spec/rubocop/cop/migration/timestamps_spec.rb +++ b/spec/rubocop/cop/migration/timestamps_spec.rb @@ -9,8 +9,6 @@ RSpec.describe RuboCop::Cop::Migration::Timestamps do let(:migration_with_timestamps) do %q( class Users < ActiveRecord::Migration[4.2] - DOWNTIME = false - def change create_table :users do |t| t.string :username, null: false @@ -25,8 +23,6 @@ RSpec.describe RuboCop::Cop::Migration::Timestamps do let(:migration_without_timestamps) do %q( class Users < ActiveRecord::Migration[4.2] - DOWNTIME = false - def change create_table :users do |t| t.string :username, null: false @@ -40,8 +36,6 @@ RSpec.describe RuboCop::Cop::Migration::Timestamps do let(:migration_with_timestamps_with_timezone) do %q( class Users < ActiveRecord::Migration[4.2] - DOWNTIME = false - def change create_table :users do |t| t.string :username, null: false @@ -61,8 +55,6 @@ RSpec.describe RuboCop::Cop::Migration::Timestamps do it 'registers an offense when the "timestamps" method is used' do expect_offense(<<~RUBY) class Users < ActiveRecord::Migration[4.2] - DOWNTIME = false - def change create_table :users do |t| t.string :username, null: false diff --git a/spec/rubocop/cop/rspec/env_assignment_spec.rb b/spec/rubocop/cop/rspec/env_assignment_spec.rb index da6bb2fa2fb..0fd09eeae11 100644 --- a/spec/rubocop/cop/rspec/env_assignment_spec.rb +++ b/spec/rubocop/cop/rspec/env_assignment_spec.rb @@ -5,8 +5,8 @@ require 'fast_spec_helper' require_relative '../../../../rubocop/cop/rspec/env_assignment' RSpec.describe RuboCop::Cop::RSpec::EnvAssignment do - offense_call_single_quotes_key = %(ENV['FOO'] = 'bar').freeze - offense_call_double_quotes_key = %(ENV["FOO"] = 'bar').freeze + offense_call_single_quotes_key = %(ENV['FOO'] = 'bar') + offense_call_double_quotes_key = %(ENV["FOO"] = 'bar') let(:source_file) { 'spec/foo_spec.rb' } diff --git a/spec/rubocop/cop/style/regexp_literal_mixed_preserve_spec.rb b/spec/rubocop/cop/style/regexp_literal_mixed_preserve_spec.rb new file mode 100644 index 00000000000..384a834a512 --- /dev/null +++ b/spec/rubocop/cop/style/regexp_literal_mixed_preserve_spec.rb @@ -0,0 +1,131 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +require_relative '../../../../rubocop/cop/style/regexp_literal_mixed_preserve' + +# This spec contains only relevant examples. +# +# See also https://github.com/rubocop/rubocop/pull/9688 +RSpec.describe RuboCop::Cop::Style::RegexpLiteralMixedPreserve, :config do + let(:config) do + supported_styles = { 'SupportedStyles' => %w[slashes percent_r mixed mixed_preserve] } + RuboCop::Config.new('Style/PercentLiteralDelimiters' => + percent_literal_delimiters_config, + 'Style/RegexpLiteralMixedPreserve' => + cop_config.merge(supported_styles)) + end + + let(:percent_literal_delimiters_config) { { 'PreferredDelimiters' => { '%r' => '{}' } } } + + context 'when EnforcedStyle is set to mixed_preserve' do + let(:cop_config) { { 'EnforcedStyle' => 'mixed_preserve' } } + + describe 'a single-line `//` regex without slashes' do + it 'is accepted' do + expect_no_offenses('foo = /a/') + end + end + + describe 'a single-line `//` regex with slashes' do + it 'registers an offense and corrects' do + expect_offense(<<~'RUBY') + foo = /home\// + ^^^^^^^^ Use `%r` around regular expression. + RUBY + + expect_correction(<<~'RUBY') + foo = %r{home/} + RUBY + end + + describe 'when configured to allow inner slashes' do + before do + cop_config['AllowInnerSlashes'] = true + end + + it 'is accepted' do + expect_no_offenses('foo = /home\\//') + end + end + end + + describe 'a multi-line `//` regex without slashes' do + it 'is accepted' do + expect_no_offenses(<<~'RUBY') + foo = / + foo + bar + /x + RUBY + end + end + + describe 'a multi-line `//` regex with slashes' do + it 'registers an offense and corrects' do + expect_offense(<<~'RUBY') + foo = / + ^ Use `%r` around regular expression. + https?:\/\/ + example\.com + /x + RUBY + + expect_correction(<<~'RUBY') + foo = %r{ + https?:// + example\.com + }x + RUBY + end + end + + describe 'a single-line %r regex without slashes' do + it 'is accepted' do + expect_no_offenses(<<~RUBY) + foo = %r{a} + RUBY + end + end + + describe 'a single-line %r regex with slashes' do + it 'is accepted' do + expect_no_offenses('foo = %r{home/}') + end + + describe 'when configured to allow inner slashes' do + before do + cop_config['AllowInnerSlashes'] = true + end + + it 'is accepted' do + expect_no_offenses(<<~RUBY) + foo = %r{home/} + RUBY + end + end + end + + describe 'a multi-line %r regex without slashes' do + it 'is accepted' do + expect_no_offenses(<<~RUBY) + foo = %r{ + foo + bar + }x + RUBY + end + end + + describe 'a multi-line %r regex with slashes' do + it 'is accepted' do + expect_no_offenses(<<~RUBY) + foo = %r{ + https?:// + example\.com + }x + RUBY + end + end + end +end diff --git a/spec/rubocop/cop/user_admin_spec.rb b/spec/rubocop/cop/user_admin_spec.rb new file mode 100644 index 00000000000..3bf458348f3 --- /dev/null +++ b/spec/rubocop/cop/user_admin_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +require 'rubocop' +require_relative '../../../rubocop/cop/user_admin' + +RSpec.describe RuboCop::Cop::UserAdmin do + subject(:cop) { described_class.new } + + it 'flags a method call' do + expect_offense(<<~SOURCE) + user.admin? + ^^^^^^ #{described_class::MSG} + SOURCE + end + + it 'flags a method call with safe operator' do + expect_offense(<<~SOURCE) + user&.admin? + ^^^^^^ #{described_class::MSG} + SOURCE + end +end |