diff options
Diffstat (limited to 'spec/rubocop/cop')
16 files changed, 523 insertions, 121 deletions
diff --git a/spec/rubocop/cop/feature_flag_usage_spec.rb b/spec/rubocop/cop/feature_flag_usage_spec.rb new file mode 100644 index 00000000000..13f58ca7084 --- /dev/null +++ b/spec/rubocop/cop/feature_flag_usage_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' + +require_relative '../../../rubocop/cop/feature_flag_usage' + +RSpec.describe RuboCop::Cop::FeatureFlagUsage, feature_category: :scalability do + let(:msg) { described_class::MSG } + + context 'when calling Feature.enabled?' do + it 'registers offence' do + expect_offense(<<~PATTERN) + Feature.enabled?(:fflag) + ^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + PATTERN + end + + it 'registers offence when called with type parameter' do + expect_offense(<<~PATTERN) + Feature.enabled?(:fflag, type: :ops) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + PATTERN + end + + it 'registers offence when called under global namespace' do + expect_offense(<<~PATTERN) + ::Feature.enabled?(:fflag, type: :ops) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + PATTERN + end + end + + context 'when calling Feature.disabled?' do + it 'registers offence' do + expect_offense(<<~PATTERN) + Feature.disabled?(:fflag) + ^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + PATTERN + end + + it 'registers offence when called with type parameter' do + expect_offense(<<~PATTERN) + Feature.disabled?(:fflag, type: :ops) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + PATTERN + end + + it 'registers offence when called under global namespace' do + expect_offense(<<~PATTERN) + ::Feature.disabled?(:fflag, type: :ops) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + PATTERN + end + end +end diff --git a/spec/rubocop/cop/filename_length_spec.rb b/spec/rubocop/cop/filename_length_spec.rb index 1ea368d282f..a5bdce9a339 100644 --- a/spec/rubocop/cop/filename_length_spec.rb +++ b/spec/rubocop/cop/filename_length_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'rubocop_spec_helper' -require 'rubocop/rspec/support' require_relative '../../../rubocop/cop/filename_length' RSpec.describe RuboCop::Cop::FilenameLength do diff --git a/spec/rubocop/cop/gitlab/feature_available_usage_spec.rb b/spec/rubocop/cop/gitlab/feature_available_usage_spec.rb index 30edd33a318..b15c298099d 100644 --- a/spec/rubocop/cop/gitlab/feature_available_usage_spec.rb +++ b/spec/rubocop/cop/gitlab/feature_available_usage_spec.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true require 'rubocop_spec_helper' -require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/feature_available_usage' RSpec.describe RuboCop::Cop::Gitlab::FeatureAvailableUsage do diff --git a/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb b/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb index 6e60889f737..bfc0cebe203 100644 --- a/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb +++ b/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true require 'rubocop_spec_helper' -require 'rubocop' -require 'rubocop/rspec/support' require_relative '../../../../rubocop/cop/gitlab/mark_used_feature_flags' RSpec.describe RuboCop::Cop::Gitlab::MarkUsedFeatureFlags do diff --git a/spec/rubocop/cop/gitlab/strong_memoize_attr_spec.rb b/spec/rubocop/cop/gitlab/strong_memoize_attr_spec.rb new file mode 100644 index 00000000000..0ed699f4e8c --- /dev/null +++ b/spec/rubocop/cop/gitlab/strong_memoize_attr_spec.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require_relative '../../../../rubocop/cop/gitlab/strong_memoize_attr' + +RSpec.describe RuboCop::Cop::Gitlab::StrongMemoizeAttr do + context 'when strong_memoize() is the entire body of a method' do + context 'when the memoization name is the same as the method name' do + it 'registers an offense and autocorrects' do + expect_offense(<<~RUBY) + class Foo + def memoized_method + strong_memoize(:memoized_method) do + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `strong_memoize_attr`, instead of using `strong_memoize` directly + 'This is a memoized method' + end + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo + def memoized_method + 'This is a memoized method' + end + strong_memoize_attr :memoized_method + end + RUBY + end + end + + context 'when the memoization name is different from the method name' do + it 'registers an offense and autocorrects' do + expect_offense(<<~RUBY) + class Foo + def enabled? + strong_memoize(:enabled) do + ^^^^^^^^^^^^^^^^^^^^^^^^ Use `strong_memoize_attr`, instead of using `strong_memoize` directly + true + end + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo + def enabled? + true + end + strong_memoize_attr :enabled?, :enabled + end + RUBY + end + end + end + + context 'when strong_memoize() is not the entire body of the method' do + it 'registers an offense and does not autocorrect' do + expect_offense(<<~RUBY) + class Foo + def memoized_method + msg = 'This is a memoized method' + + strong_memoize(:memoized_method) do + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `strong_memoize_attr`, instead of using `strong_memoize` directly + msg + end + end + end + RUBY + + expect_no_corrections + end + end +end diff --git a/spec/rubocop/cop/graphql/descriptions_spec.rb b/spec/rubocop/cop/graphql/descriptions_spec.rb index 8826e700fdf..0ff2812d6f6 100644 --- a/spec/rubocop/cop/graphql/descriptions_spec.rb +++ b/spec/rubocop/cop/graphql/descriptions_spec.rb @@ -16,6 +16,8 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end TYPE + + expect_no_corrections end it 'adds an offense when description does not end in a period' do @@ -44,6 +46,8 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end TYPE + + expect_no_corrections end it 'adds an offense when description begins with "The"' do @@ -58,6 +62,46 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end TYPE + + expect_no_corrections + end + + it 'adds an offense when description contains the demonstrative "this"' do + expect_offense(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + ^^^^^^^^^^^^^^^ #{described_class::MSG_CONTAINS_THIS} + GraphQL::Types::String, + null: false, + description: 'Description of this thing.' + end + end + TYPE + + expect_correction(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::Types::String, + null: false, + description: 'Description of the thing.' + end + end + TYPE + end + + it 'does not add an offense when a word does not contain the substring "this"' do + expect_no_offenses(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::Types::String, + null: false, + description: 'Description of thistle.' + end + end + TYPE end it 'does not add an offense when description is correct' do @@ -96,6 +140,8 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end TYPE + + expect_no_corrections end it 'adds an offense when description does not end in a period' do @@ -124,6 +170,8 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end TYPE + + expect_no_corrections end it 'adds an offense when description begins with "The"' do @@ -138,6 +186,46 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end TYPE + + expect_no_corrections + end + + it 'adds an offense when description contains the demonstrative "this"' do + expect_offense(<<~TYPE) + module Types + class FakeType < BaseObject + argument :a_thing, + ^^^^^^^^^^^^^^^^^^ #{described_class::MSG_CONTAINS_THIS} + GraphQL::Types::String, + null: false, + description: 'Description of this thing.' + end + end + TYPE + + expect_correction(<<~TYPE) + module Types + class FakeType < BaseObject + argument :a_thing, + GraphQL::Types::String, + null: false, + description: 'Description of the thing.' + end + end + TYPE + end + + it 'does not add an offense when a word does not contain the substring "this"' do + expect_no_offenses(<<~TYPE) + module Types + class FakeType < BaseObject + argument :a_thing, + GraphQL::Types::String, + null: false, + description: 'Description of thistle.' + end + end + TYPE end it 'does not add an offense when description is correct' do @@ -164,6 +252,8 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end TYPE + + expect_no_corrections end it 'adds an offense when description does not end in a period' do @@ -186,6 +276,8 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end TYPE + + expect_no_corrections end it 'adds an offense when description begins with "A"' do @@ -197,6 +289,37 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end TYPE + + expect_no_corrections + end + + it 'adds an offense when description contains the demonstrative "this"' do + expect_offense(<<~TYPE.strip) + module Types + class FakeEnum < BaseEnum + value 'FOO', value: 'foo', description: 'Description of this issue.' + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG_CONTAINS_THIS} + end + end + TYPE + + expect_correction(<<~TYPE.strip) + module Types + class FakeEnum < BaseEnum + value 'FOO', value: 'foo', description: 'Description of the issue.' + end + end + TYPE + end + + it 'does not add an offense when a word does not contain the substring "this"' do + expect_no_offenses(<<~TYPE.strip) + module Types + class FakeEnum < BaseEnum + value 'FOO', value: 'foo', description: 'Description of thistle.' + end + end + TYPE end it 'does not add an offense when description is correct (defined using `description:`)' do @@ -220,8 +343,8 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end end - describe 'autocorrecting descriptions without periods' do - it 'can autocorrect' do + describe 'autocorrecting periods in descriptions' do + it 'autocorrects missing periods' do expect_offense(<<~TYPE) module Types class FakeType < BaseObject @@ -246,7 +369,20 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do TYPE end - it 'can autocorrect a heredoc' do + it 'does not autocorrect if periods exist' do + expect_no_offenses(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::Types::String, + null: false, + description: 'Behold! A description.' + end + end + TYPE + end + + it 'autocorrects a heredoc' do expect_offense(<<~TYPE) module Types class FakeType < BaseObject @@ -274,5 +410,104 @@ RSpec.describe RuboCop::Cop::Graphql::Descriptions do end TYPE end + + it 'does not autocorrect a heredoc if periods exist' do + expect_no_offenses(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::Types::String, + null: false, + description: <<~DESC + Behold! A description. + DESC + end + end + TYPE + end + end + + describe 'autocorrecting "this" to "the"' do + it 'autocorrects if "this" is found' do + expect_offense(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + ^^^^^^^^^^^^^^^ #{described_class::MSG_CONTAINS_THIS} + GraphQL::Types::String, + null: false, + description: 'Description of this thing.' + end + end + TYPE + + expect_correction(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::Types::String, + null: false, + description: 'Description of the thing.' + end + end + TYPE + end + + it 'does not autocorrect if "this" is not found' do + expect_no_offenses(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::Types::String, + null: false, + description: 'Description of the thing.' + end + end + TYPE + end + + it 'autocorrects a heredoc if "this" is found' do + expect_offense(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + ^^^^^^^^^^^^^^^ #{described_class::MSG_CONTAINS_THIS} + GraphQL::Types::String, + null: false, + description: <<~DESC + Description of this thing. + DESC + end + end + TYPE + + expect_correction(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::Types::String, + null: false, + description: <<~DESC + Description of the thing. + DESC + end + end + TYPE + end + + it 'does not autocorrect a heredoc if "this" is not found' do + expect_no_offenses(<<~TYPE) + module Types + class FakeType < BaseObject + field :a_thing, + GraphQL::Types::String, + null: false, + description: <<~DESC + Description of the thing. + DESC + end + end + TYPE + 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 deleted file mode 100644 index 865f567db44..00000000000 --- a/spec/rubocop/cop/migration/add_column_with_default_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true - -require 'rubocop_spec_helper' -require_relative '../../../../rubocop/cop/migration/add_column_with_default' - -RSpec.describe RuboCop::Cop::Migration::AddColumnWithDefault do - let(:cop) { described_class.new } - - context 'when outside of a migration' do - it 'does not register any offenses' do - expect_no_offenses(<<~RUBY) - def up - add_column_with_default(:merge_request_diff_files, :artifacts, :boolean, default: true, allow_null: false) - end - RUBY - end - end - - context 'when in a migration' do - before do - allow(cop).to receive(:in_migration?).and_return(true) - end - - 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) - ^^^^^^^^^^^^^^^^^^^^^^^ `add_column_with_default` is deprecated, use `add_column` instead - end - RUBY - end - end -end 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 85a86a27c48..a6a072e2caf 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 @@ -37,8 +37,8 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do add_column :test_text_limits, :email, :text ^^^^^^^^^^ #{msg} - add_column_with_default :test_text_limits, :role, :text, default: 'default' - ^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + add_column :test_text_limits, :role, :text, default: 'default' + ^^^^^^^^^^ #{msg} change_column_type_concurrently :test_text_limits, :test_id, :text ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} @@ -67,7 +67,7 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do end add_column :test_text_limits, :email, :text - add_column_with_default :test_text_limits, :role, :text, default: 'default' + add_column :test_text_limits, :role, :text, default: 'default' change_column_type_concurrently :test_text_limits, :test_id, :text add_text_limit :test_text_limits, :name, 255 @@ -115,7 +115,7 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do end add_column :test_text_limits, :email, :text, array: true - add_column_with_default :test_text_limits, :role, :text, default: [], array: true + add_column :test_text_limits, :role, :text, default: [], array: true change_column_type_concurrently :test_text_limits, :test_id, :text, array: true end end @@ -141,8 +141,8 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do add_column :test_text_limits, :email, :text ^^^^^^^^^^ #{msg} - add_column_with_default :test_text_limits, :role, :text, default: 'default' - ^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + add_column :test_text_limits, :role, :text, default: 'default' + ^^^^^^^^^^ #{msg} change_column_type_concurrently :test_text_limits, :test_id, :text ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} @@ -170,7 +170,7 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do end add_column :encrypted_test_text_limits, :encrypted_email, :text - add_column_with_default :encrypted_test_text_limits, :encrypted_role, :text, default: 'default' + add_column :encrypted_test_text_limits, :encrypted_role, :text, default: 'default' change_column_type_concurrently :encrypted_test_text_limits, :encrypted_test_id, :text end end @@ -194,7 +194,7 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do add_column :no_offense_on_down, :email, :text - add_column_with_default :no_offense_on_down, :role, :text, default: 'default' + add_column :no_offense_on_down, :role, :text, default: 'default' end end RUBY @@ -215,7 +215,7 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do end add_column :test_text_limits, :email, :text - add_column_with_default :test_text_limits, :role, :text, default: 'default' + add_column :test_text_limits, :role, :text, default: 'default' change_column_type_concurrently :test_text_limits, :test_id, :text end end diff --git a/spec/rubocop/cop/migration/batch_migrations_post_only_spec.rb b/spec/rubocop/cop/migration/batch_migrations_post_only_spec.rb new file mode 100644 index 00000000000..b5e2e83e788 --- /dev/null +++ b/spec/rubocop/cop/migration/batch_migrations_post_only_spec.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require_relative '../../../../rubocop/cop/migration/batch_migrations_post_only' + +RSpec.describe RuboCop::Cop::Migration::BatchMigrationsPostOnly do + let(:cop) { described_class.new } + + before do + allow(cop).to receive(:in_post_deployment_migration?).and_return post_migration? + end + + context 'when methods appear in a regular migration' do + let(:post_migration?) { false } + + it "does not allow 'ensure_batched_background_migration_is_finished' to be called" do + expect_offense(<<~CODE) + ensure_batched_background_migration_is_finished + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This method must only be used in post-deployment migrations. + CODE + end + + it "does not allow 'queue_batched_background_migration' to be called" do + expect_offense(<<~CODE) + queue_batched_background_migration + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This method must only be used in post-deployment migrations. + CODE + end + + it "does not allow 'delete_batched_background_migration' to be called" do + expect_offense(<<~CODE) + delete_batched_background_migration + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This method must only be used in post-deployment migrations. + CODE + end + + it "does not allow 'ensure_batched_background_migration_is_finished' to be called" do + expect_offense(<<~CODE) + finalize_batched_background_migration + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This method must only be used in post-deployment migrations. + CODE + end + + it 'allows arbitrary other method to be called' do + expect_no_offenses(<<~CODE) + foo + CODE + end + end + + context 'when methods appear in a post-deployment migration' do + let(:post_migration?) { true } + + it "allows 'ensure_batched_background_migration_is_finished' to be called" do + expect_no_offenses(<<~CODE) + ensure_batched_background_migration_is_finished + CODE + end + + it "allows 'queue_batched_background_migration' to be called" do + expect_no_offenses(<<~CODE) + queue_batched_background_migration + CODE + end + + it "allows 'delete_batched_background_migration' to be called" do + expect_no_offenses(<<~CODE) + delete_batched_background_migration + CODE + end + + it "allows 'ensure_batched_background_migration_is_finished' to be called" do + expect_no_offenses(<<~CODE) + finalize_batched_background_migration + CODE + end + + it 'allows arbitrary other method to be called' do + expect_no_offenses(<<~CODE) + foo + CODE + end + end +end diff --git a/spec/rubocop/cop/migration/prevent_strings_spec.rb b/spec/rubocop/cop/migration/prevent_strings_spec.rb index f1adeae6786..455b7765aa2 100644 --- a/spec/rubocop/cop/migration/prevent_strings_spec.rb +++ b/spec/rubocop/cop/migration/prevent_strings_spec.rb @@ -27,8 +27,8 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do add_column(:users, :bio, :string) ^^^^^^^^^^ %{msg} - add_column_with_default(:users, :url, :string, default: '/-/user', allow_null: false, limit: 255) - ^^^^^^^^^^^^^^^^^^^^^^^ %{msg} + add_column(:users, :url, :string, default: '/-/user', allow_null: false, limit: 255) + ^^^^^^^^^^ %{msg} change_column_type_concurrently :users, :commit_id, :string ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %{msg} @@ -67,7 +67,7 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do end add_column(:users, :bio, :text) - add_column_with_default(:users, :url, :text, default: '/-/user', allow_null: false, limit: 255) + add_column(:users, :url, :text, default: '/-/user', allow_null: false, limit: 255) change_column_type_concurrently :users, :commit_id, :text end end @@ -86,7 +86,7 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do end add_column :test_string_arrays, :email, :string, array: true - add_column_with_default :test_string_arrays, :role, :string, default: [], array: true + add_column :test_string_arrays, :role, :string, default: [], array: true change_column_type_concurrently :test_string_arrays, :test_id, :string, array: true end end @@ -112,7 +112,7 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do end add_column(:users, :bio, :string) - add_column_with_default(:users, :url, :string, default: '/-/user', allow_null: false, limit: 255) + add_column(:users, :url, :string, default: '/-/user', allow_null: false, limit: 255) change_column_type_concurrently :users, :commit_id, :string end end @@ -133,7 +133,7 @@ RSpec.describe RuboCop::Cop::Migration::PreventStrings do end add_column(:users, :bio, :string) - add_column_with_default(:users, :url, :string, default: '/-/user', allow_null: false, limit: 255) + add_column(:users, :url, :string, default: '/-/user', allow_null: false, limit: 255) change_column_type_concurrently :users, :commit_id, :string end end diff --git a/spec/rubocop/cop/migration/versioned_migration_class_spec.rb b/spec/rubocop/cop/migration/versioned_migration_class_spec.rb index b44f5d64a62..506e3146afa 100644 --- a/spec/rubocop/cop/migration/versioned_migration_class_spec.rb +++ b/spec/rubocop/cop/migration/versioned_migration_class_spec.rb @@ -6,7 +6,7 @@ require_relative '../../../../rubocop/cop/migration/versioned_migration_class' RSpec.describe RuboCop::Cop::Migration::VersionedMigrationClass do let(:migration) do <<~SOURCE - class TestMigration < Gitlab::Database::Migration[1.0] + class TestMigration < Gitlab::Database::Migration[2.1] def up execute 'select 1' end @@ -49,23 +49,23 @@ RSpec.describe RuboCop::Cop::Migration::VersionedMigrationClass do it 'adds an offence if inheriting from ActiveRecord::Migration' do expect_offense(<<~RUBY) class MyMigration < ActiveRecord::Migration[6.1] - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration but use Gitlab::Database::Migration[1.0] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration but use Gitlab::Database::Migration[2.1] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning. end RUBY end it 'adds an offence if including Gitlab::Database::MigrationHelpers directly' do expect_offense(<<~RUBY) - class MyMigration < Gitlab::Database::Migration[1.0] + class MyMigration < Gitlab::Database::Migration[2.1] include Gitlab::Database::MigrationHelpers - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't include migration helper modules directly. Inherit from Gitlab::Database::Migration[1.0] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't include migration helper modules directly. Inherit from Gitlab::Database::Migration[2.1] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning. end RUBY end it 'excludes ActiveRecord classes defined inside the migration' do expect_no_offenses(<<~RUBY) - class TestMigration < Gitlab::Database::Migration[1.0] + class TestMigration < Gitlab::Database::Migration[2.1] class TestModel < ApplicationRecord end diff --git a/spec/rubocop/cop/performance/readlines_each_spec.rb b/spec/rubocop/cop/performance/readlines_each_spec.rb index d876cbf79a5..11e2cee9262 100644 --- a/spec/rubocop/cop/performance/readlines_each_spec.rb +++ b/spec/rubocop/cop/performance/readlines_each_spec.rb @@ -6,7 +6,7 @@ require_relative '../../../../rubocop/cop/performance/readlines_each' RSpec.describe RuboCop::Cop::Performance::ReadlinesEach do let(:message) { 'Avoid `IO.readlines.each`, since it reads contents into memory in full. Use `IO.each_line` or `IO.each` instead.' } - shared_examples_for(:class_read) do |klass| + shared_examples_for('class read') do |klass| context "and it is called as a class method on #{klass}" do it 'flags it as an offense' do leading_readline = "#{klass}.readlines(file_path)." @@ -29,7 +29,7 @@ RSpec.describe RuboCop::Cop::Performance::ReadlinesEach do context 'when reading all lines using IO.readlines.each' do %w(IO File).each do |klass| - it_behaves_like(:class_read, klass) + it_behaves_like('class read', klass) end context 'and it is called as an instance method on a return value' do diff --git a/spec/rubocop/cop/rspec/avoid_test_prof_spec.rb b/spec/rubocop/cop/rspec/avoid_test_prof_spec.rb new file mode 100644 index 00000000000..b180134b226 --- /dev/null +++ b/spec/rubocop/cop/rspec/avoid_test_prof_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require 'rspec-parameterized' + +require_relative '../../../../rubocop/cop/rspec/avoid_test_prof' + +RSpec.describe RuboCop::Cop::RSpec::AvoidTestProf, feature_category: :not_owned do + using RSpec::Parameterized::TableSyntax + + context 'when there are offenses' do + where(:method_call, :method_name, :alternatives) do + 'let_it_be(:user)' | 'let_it_be' | '`let` or `let!`' + 'let_it_be_with_reload(:user)' | 'let_it_be_with_reload' | '`let` or `let!`' + 'let_it_be_with_refind(:user)' | 'let_it_be_with_refind' | '`let` or `let!`' + 'before_all' | 'before_all' | '`before` or `before(:all)`' + end + + with_them do + it 'registers the offense' do + error_message = "Prefer #{alternatives} over `#{method_name}` in migration specs. " \ + 'See ' \ + 'https://docs.gitlab.com/ee/development/testing_guide/best_practices.html' \ + '#testprof-in-migration-specs' + + expect_offense(<<~RUBY) + describe 'foo' do + #{method_call} { table(:users) } + #{'^' * method_call.size} #{error_message} + end + RUBY + end + end + end + + context 'when there are no offenses' do + where(method_call: %w[let(:user) let!(:user) before before(:all)]) + + with_them do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + describe 'foo' do + #{method_call} { table(:users) } + end + RUBY + end + end + end +end diff --git a/spec/rubocop/cop/rspec/timecop_freeze_spec.rb b/spec/rubocop/cop/rspec/timecop_freeze_spec.rb deleted file mode 100644 index 4361f587da3..00000000000 --- a/spec/rubocop/cop/rspec/timecop_freeze_spec.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -require 'rubocop_spec_helper' - -require_relative '../../../../rubocop/cop/rspec/timecop_freeze' - -RSpec.describe RuboCop::Cop::RSpec::TimecopFreeze do - context 'when calling Timecop.freeze' do - it 'registers an offense and corrects', :aggregate_failures do - expect_offense(<<~CODE) - Timecop.freeze(Time.current) { example.run } - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `Timecop.freeze`, use `freeze_time` instead. [...] - CODE - - expect_correction(<<~CODE) - freeze_time(Time.current) { example.run } - CODE - end - end - - context 'when calling a different method on Timecop' do - it 'does not register an offense' do - expect_no_offenses(<<~CODE) - Timecop.travel(Time.current) - CODE - end - end -end diff --git a/spec/rubocop/cop/rspec/timecop_travel_spec.rb b/spec/rubocop/cop/rspec/timecop_travel_spec.rb deleted file mode 100644 index 89c46ff6c59..00000000000 --- a/spec/rubocop/cop/rspec/timecop_travel_spec.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -require 'rubocop_spec_helper' - -require_relative '../../../../rubocop/cop/rspec/timecop_travel' - -RSpec.describe RuboCop::Cop::RSpec::TimecopTravel do - context 'when calling Timecop.travel' do - it 'registers an offense and corrects', :aggregate_failures do - expect_offense(<<~CODE) - Timecop.travel(1.day.ago) { create(:issue) } - ^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `Timecop.travel`, use `travel_to` instead. [...] - CODE - - expect_correction(<<~CODE) - travel_to(1.day.ago) { create(:issue) } - CODE - end - end - - context 'when calling a different method on Timecop' do - it 'does not register an offense' do - expect_no_offenses(<<~CODE) - Timecop.freeze { create(:issue) } - CODE - end - end -end diff --git a/spec/rubocop/cop/user_admin_spec.rb b/spec/rubocop/cop/user_admin_spec.rb index 99e87d619c0..21bf027324b 100644 --- a/spec/rubocop/cop/user_admin_spec.rb +++ b/spec/rubocop/cop/user_admin_spec.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true require 'rubocop_spec_helper' - -require 'rubocop' require_relative '../../../rubocop/cop/user_admin' RSpec.describe RuboCop::Cop::UserAdmin do |