summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/utils/override_spec.rb
blob: a5e53c1dfc18afa5f62a9ec4a45a2767cb03e23b (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# frozen_string_literal: true

require 'fast_spec_helper'

# Patching ActiveSupport::Concern
require_relative '../../../../config/initializers/0_as_concern'

RSpec.describe Gitlab::Utils::Override do
  let(:base) do
    Struct.new(:good) do
      def self.good
        0
      end
    end
  end

  let(:derived) { Class.new(base).tap { |m| m.extend described_class } }
  let(:extension) { Module.new.tap { |m| m.extend described_class } }

  let(:prepending_class) { base.tap { |m| m.prepend extension } }
  let(:including_class) { base.tap { |m| m.include extension } }

  let(:prepending_class_methods) do
    base.tap { |m| m.singleton_class.prepend extension }
  end

  let(:extending_class_methods) do
    base.tap { |m| m.extend extension }
  end

  let(:klass) { subject }

  def good(mod, bad_arity: false, negative_arity: false)
    mod.module_eval do
      override :good

      if bad_arity
        def good(num)
        end
      elsif negative_arity
        def good(*args)
          super.succ
        end
      else
        def good
          super.succ
        end
      end
    end

    mod
  end

  def bad(mod)
    mod.module_eval do
      override :bad
      def bad
        true
      end
    end

    mod
  end

  shared_examples 'checking as intended' do
    it 'checks ok for overriding method' do
      good(subject)
      result = instance.good

      expect(result).to eq(1)
      described_class.verify!
    end

    it 'checks ok for overriding method using negative arity' do
      good(subject, negative_arity: true)
      result = instance.good

      expect(result).to eq(1)
      described_class.verify!
    end

    it 'raises NotImplementedError when it is not overriding anything' do
      expect do
        bad(subject)
        instance.bad
        described_class.verify!
      end.to raise_error(NotImplementedError)
    end

    it 'raises NotImplementedError when overriding a method with different arity' do
      expect do
        good(subject, bad_arity: true)
        instance.good(1)
        described_class.verify!
      end.to raise_error(NotImplementedError)
    end
  end

  shared_examples 'checking as intended, nothing was overridden' do
    it 'raises NotImplementedError because it is not overriding it' do
      expect do
        good(subject)
        instance.good
        described_class.verify!
      end.to raise_error(NotImplementedError)
    end

    it 'raises NotImplementedError when it is not overriding anything' do
      expect do
        bad(subject)
        instance.bad
        described_class.verify!
      end.to raise_error(NotImplementedError)
    end
  end

  shared_examples 'nothing happened' do
    it 'does not complain when it is overriding something' do
      good(subject)
      result = instance.good

      expect(result).to eq(1)
      described_class.verify!
    end

    it 'does not complain when it is not overriding anything' do
      bad(subject)
      result = instance.bad

      expect(result).to eq(true)
      described_class.verify!
    end
  end

  before do
    # Make sure we're not touching the internal cache
    allow(described_class).to receive(:extensions).and_return({})
  end

  describe '#override' do
    context 'when instance is klass.new(0)' do
      let(:instance) { klass.new(0) }

      context 'when STATIC_VERIFICATION is set' do
        before do
          stub_env('STATIC_VERIFICATION', 'true')
        end

        context 'when subject is a class' do
          subject { derived }

          it_behaves_like 'checking as intended'
        end

        context 'when subject is a module, and class is prepending it' do
          subject { extension }

          let(:klass) { prepending_class }

          it_behaves_like 'checking as intended'
        end

        context 'when subject is a module, and class is including it' do
          subject { extension }

          let(:klass) { including_class }

          it_behaves_like 'checking as intended, nothing was overridden'
        end

        context 'when ActiveSupport::Concern and class_methods are used' do
          # We need to give module names before using Override
          let(:base) { stub_const('Base', Module.new) }
          let(:extension) { stub_const('Extension', Module.new) }

          def define_base(method_name:)
            base.module_eval do
              extend ActiveSupport::Concern

              class_methods do
                define_method(method_name) do
                  :f
                end
              end
            end
          end

          def define_extension(method_name:)
            extension.module_eval do
              extend ActiveSupport::Concern

              class_methods do
                extend Gitlab::Utils::Override

                override method_name
                define_method(method_name) do
                  :g
                end
              end
            end
          end

          context 'when it is defining a overriding method' do
            before do
              define_base(method_name: :f)
              define_extension(method_name: :f)

              base.prepend(extension)
            end

            it 'verifies' do
              expect(base.f).to eq(:g)

              described_class.verify!
            end
          end

          context 'when it is not defining a overriding method' do
            before do
              define_base(method_name: :f)
              define_extension(method_name: :g)

              base.prepend(extension)
            end

            it 'raises NotImplementedError' do
              expect(base.f).to eq(:f)

              expect { described_class.verify! }
                .to raise_error(NotImplementedError)
            end
          end
        end
      end

      context 'when STATIC_VERIFICATION is not set' do
        before do
          stub_env('STATIC_VERIFICATION', nil)
        end

        context 'when subject is a class' do
          subject { derived }

          it_behaves_like 'nothing happened'
        end

        context 'when subject is a module, and class is prepending it' do
          subject { extension }

          let(:klass) { prepending_class }

          it_behaves_like 'nothing happened'
        end

        context 'when subject is a module, and class is including it' do
          subject { extension }

          let(:klass) { including_class }

          it 'does not complain when it is overriding something' do
            good(subject)
            result = instance.good

            expect(result).to eq(0)
            described_class.verify!
          end

          it 'does not complain when it is not overriding anything' do
            bad(subject)
            result = instance.bad

            expect(result).to eq(true)
            described_class.verify!
          end
        end
      end
    end

    context 'when instance is klass' do
      let(:instance) { klass }

      context 'when STATIC_VERIFICATION is set' do
        before do
          stub_env('STATIC_VERIFICATION', 'true')
        end

        context 'when subject is a module, and class is prepending it' do
          subject { extension }

          let(:klass) { prepending_class_methods }

          it_behaves_like 'checking as intended'
        end

        context 'when subject is a module, and class is extending it' do
          subject { extension }

          let(:klass) { extending_class_methods }

          it_behaves_like 'checking as intended, nothing was overridden'
        end
      end
    end
  end
end