summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/migration/background_migration_missing_active_concern_spec.rb
blob: c74a7d29056f19c47b89ae9c3d5cfbcdbc65b45f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true

require 'rubocop_spec_helper'
require_relative '../../../../rubocop/cop/migration/background_migration_missing_active_concern'

RSpec.describe RuboCop::Cop::Migration::BackgroundMigrationMissingActiveConcern do
  shared_examples 'offense is not registered' do
    it 'does not register any offenses' do
      expect_no_offenses(<<~RUBY)
        module Gitlab
          module BackgroundMigration
            prepended do
              scope_to -> (relation) { relation }
            end
          end
        end
      RUBY
    end
  end

  context 'when outside of a migration' do
    it_behaves_like 'offense is not registered'
  end

  context 'in non-ee background migration' do
    before do
      allow(cop).to receive(:in_ee_background_migration?).and_return(false)
    end

    it_behaves_like 'offense is not registered'
  end

  context 'in ee background migration' do
    before do
      allow(cop).to receive(:in_ee_background_migration?).and_return(true)
    end

    context 'when scope_to is not used inside prepended block' do
      it 'does not register any offenses' do
        expect_no_offenses(<<~RUBY)
          module Gitlab
            module BackgroundMigration
              prepended do
                some_method_to -> (relation) { relation }
              end

              def foo
                scope_to -> (relation) { relation }
              end
            end
          end
        RUBY
      end
    end

    context 'when scope_to is used inside prepended block' do
      it 'does not register any offenses if the module does extend ActiveSupport::Concern' do
        expect_no_offenses(<<~RUBY)
        module Gitlab
          module BackgroundMigration
            extend ::Gitlab::Utils::Override
            extend ActiveSupport::Concern

            prepended do
              scope_to -> (relation) { relation }
            end
          end
        end
        RUBY
      end

      it 'registers an offense if the module does not extend ActiveSupport::Concern' do
        expect_offense(<<~RUBY)
          module Gitlab
            module BackgroundMigration
              prepended do
                scope_to -> (relation) { relation }
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Extend `ActiveSupport::Concern` [...]
              end
            end
          end
        RUBY
      end
    end
  end
end