blob: af4c7fa5d8ec9170a284d716222c9373bd378d02 (
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
|
# frozen_string_literal: true
require 'fast_spec_helper'
RSpec.describe Gitlab::Utils::DelegatorOverride do
let(:delegator_class) do
Class.new(::SimpleDelegator) do
extend(::Gitlab::Utils::DelegatorOverride)
def foo
end
end
end
let(:target_class) do
Class.new do
def foo
end
def bar
end
end
end
let(:dummy_module) do
Module.new do
def foobar
end
end
end
before do
stub_env('STATIC_VERIFICATION', 'true')
end
describe '.delegator_target' do
subject { delegator_class.delegator_target(target_class) }
it 'sets the delegator target to the validator' do
expect(described_class.validator(delegator_class))
.to receive(:add_target).with(target_class)
subject
end
context 'when the class does not inherit SimpleDelegator' do
let(:delegator_class) do
Class.new do
extend(::Gitlab::Utils::DelegatorOverride)
end
end
it 'raises an error' do
expect { subject }.to raise_error(ArgumentError, /not a subclass of 'SimpleDelegator' class/)
end
end
end
describe '.delegator_override' do
subject { delegator_class.delegator_override(:foo) }
it 'adds the method name to the allowlist' do
expect(described_class.validator(delegator_class))
.to receive(:add_allowlist).with([:foo])
subject
end
end
describe '.delegator_override_with' do
subject { delegator_class.delegator_override_with(dummy_module) }
it 'adds the method names of the module to the allowlist' do
expect(described_class.validator(delegator_class))
.to receive(:add_allowlist).with([:foobar])
subject
end
end
describe '.verify!' do
subject { described_class.verify! }
it 'does not raise an error when an override is in allowlist' do
delegator_class.delegator_target(target_class)
delegator_class.delegator_override(:foo)
expect { subject }.not_to raise_error
end
it 'raises an error when there is an override' do
delegator_class.delegator_target(target_class)
expect { subject }.to raise_error(Gitlab::Utils::DelegatorOverride::Validator::UnexpectedDelegatorOverrideError)
end
end
end
|