debian-mirror-gitlab/spec/rubocop/cop/migration/versioned_migration_class_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
3 KiB
Ruby
Raw Normal View History

2021-11-11 11:23:49 +05:30
# frozen_string_literal: true
2022-10-11 01:57:18 +05:30
require 'rubocop_spec_helper'
2021-11-11 11:23:49 +05:30
require_relative '../../../../rubocop/cop/migration/versioned_migration_class'
2023-04-23 21:23:45 +05:30
RSpec.describe RuboCop::Cop::Migration::VersionedMigrationClass, feature_category: :database do
2021-11-11 11:23:49 +05:30
let(:migration) do
<<~SOURCE
2023-03-04 22:38:38 +05:30
class TestMigration < Gitlab::Database::Migration[2.1]
2021-11-11 11:23:49 +05:30
def up
execute 'select 1'
end
def down
execute 'select 1'
end
end
SOURCE
end
shared_examples 'a disabled cop' do
it 'does not register any offenses' do
expect_no_offenses(migration)
end
end
context 'outside of a migration' do
it_behaves_like 'a disabled cop'
end
context 'in migration' do
before do
allow(cop).to receive(:in_migration?).and_return(true)
end
context 'in an old migration' do
before do
allow(cop).to receive(:version).and_return(described_class::ENFORCED_SINCE - 5)
end
it_behaves_like 'a disabled cop'
end
context 'that is recent' do
before do
allow(cop).to receive(:version).and_return(described_class::ENFORCED_SINCE + 5)
end
it 'adds an offence if inheriting from ActiveRecord::Migration' do
expect_offense(<<~RUBY)
class MyMigration < ActiveRecord::Migration[6.1]
2023-04-23 21:23:45 +05:30
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration or old versions of Gitlab::Database::Migration. 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 inheriting from old version of Gitlab::Database::Migration' do
expect_offense(<<~RUBY)
class MyMigration < Gitlab::Database::Migration[2.0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration or old versions of Gitlab::Database::Migration. Use Gitlab::Database::Migration[2.1] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning.
2021-11-11 11:23:49 +05:30
end
RUBY
end
it 'adds an offence if including Gitlab::Database::MigrationHelpers directly' do
expect_offense(<<~RUBY)
2023-03-04 22:38:38 +05:30
class MyMigration < Gitlab::Database::Migration[2.1]
2021-11-11 11:23:49 +05:30
include Gitlab::Database::MigrationHelpers
2023-03-04 22:38:38 +05:30
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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.
2021-11-11 11:23:49 +05:30
end
RUBY
end
it 'excludes ActiveRecord classes defined inside the migration' do
expect_no_offenses(<<~RUBY)
2023-03-04 22:38:38 +05:30
class TestMigration < Gitlab::Database::Migration[2.1]
2021-11-11 11:23:49 +05:30
class TestModel < ApplicationRecord
end
class AnotherTestModel < ActiveRecord::Base
end
end
RUBY
end
end
end
end