summaryrefslogtreecommitdiff
path: root/spec/config/inject_enterprise_edition_module_spec.rb
blob: 6689fed02f549ef77539d3d427984dd7be331d1a (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe InjectEnterpriseEditionModule, feature_category: :shared do
  let(:extension_name) { 'FF' }
  let(:extension_namespace) { Module.new }
  let(:fish_name) { 'Fish' }
  let(:fish_class) { Class.new }
  let(:fish_extension) { Module.new }

  before do
    # Make sure we're not relying on which mode we're running under
    allow(GitlabEdition).to receive(:extensions).and_return([extension_name.downcase])

    # Test on an imagined extension and imagined class
    stub_const(fish_name, fish_class) # Fish
    allow(fish_class).to receive(:name).and_return(fish_name)
  end

  shared_examples 'expand the extension with' do |method|
    context 'when extension namespace is set at top-level' do
      before do
        stub_const(extension_name, extension_namespace) # FF
        extension_namespace.const_set(fish_name, fish_extension) # FF::Fish
      end

      it "calls #{method} with the extension module" do
        expect(fish_class).to receive(method).with(fish_extension)

        fish_class.__send__("#{method}_mod_with", fish_name)
      end
    end

    context 'when extension namespace is set at another namespace' do
      let(:another_namespace) { Module.new } # QA

      before do
        another_namespace.const_set(extension_name, extension_namespace) # QA::FF
        extension_namespace.const_set(fish_name, fish_extension) # QA::FF::Fish
      end

      it "calls #{method} with the extension module from the additional namespace" do
        expect(fish_class).to receive(method).with(fish_extension)

        fish_class.__send__("#{method}_mod_with", fish_name, namespace: another_namespace)
      end
    end

    context 'when extension namespace exists but not the extension' do
      before do
        stub_const(extension_name, extension_namespace) # FF
      end

      it "does not call #{method}" do
        expect(fish_class).not_to receive(method).with(fish_extension)

        fish_class.__send__("#{method}_mod_with", fish_name)
      end
    end

    context 'when extension namespace does not exist' do
      it "does not call #{method}" do
        expect(fish_class).not_to receive(method).with(fish_extension)

        fish_class.__send__("#{method}_mod_with", fish_name)
      end
    end
  end

  shared_examples 'expand the assumed extension with' do |method|
    context 'when extension namespace is set at top-level' do
      before do
        stub_const(extension_name, extension_namespace) # FF
        extension_namespace.const_set(fish_name, fish_extension) # FF::Fish
      end

      it "calls #{method} with the extension module" do
        expect(fish_class).to receive(method).with(fish_extension)

        fish_class.__send__("#{method}_mod")
      end
    end

    context 'when extension namespace exists but not the extension' do
      before do
        stub_const(extension_name, extension_namespace) # FF
      end

      it "does not call #{method}" do
        expect(fish_class).not_to receive(method).with(fish_extension)

        fish_class.__send__("#{method}_mod")
      end
    end

    context 'when extension namespace does not exist' do
      it "does not call #{method}" do
        expect(fish_class).not_to receive(method).with(fish_extension)

        fish_class.__send__("#{method}_mod")
      end
    end
  end

  describe '#prepend_mod_with' do
    it_behaves_like 'expand the extension with', :prepend
  end

  describe '#extend_mod_with' do
    it_behaves_like 'expand the extension with', :extend
  end

  describe '#include_mod_with' do
    it_behaves_like 'expand the extension with', :include
  end

  describe '#prepend_mod' do
    it_behaves_like 'expand the assumed extension with', :prepend
  end

  describe '#extend_mod' do
    it_behaves_like 'expand the assumed extension with', :extend
  end

  describe '#include_mod' do
    it_behaves_like 'expand the assumed extension with', :include
  end

  describe '#gitlab_extensions' do
    context 'when there are no extension modules' do
      it 'returns the class itself' do
        expect(fish_class.gitlab_extensions).to contain_exactly(fish_class)
      end
    end

    context 'when there are extension modules' do
      it 'returns the class itself and any extensions' do
        stub_const(extension_name, extension_namespace)
        extension_namespace.const_set(fish_name, fish_extension)
        fish_class.prepend_mod

        expect(fish_class.gitlab_extensions).to contain_exactly(fish_class, fish_extension)
      end
    end
  end
end