summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/config/extendable/entry_spec.rb
blob: e00104e3c688f9c8c46824cb5dbaf4f338caf59e (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
# frozen_string_literal: true

require 'fast_spec_helper'

describe Gitlab::Ci::Config::Extendable::Entry do
  describe '.new' do
    context 'when entry key is not included in the context hash' do
      it 'raises error' do
        expect { described_class.new(:test, something: 'something') }
          .to raise_error StandardError, 'Invalid entry key!'
      end
    end
  end

  describe '#value' do
    it 'reads a hash value from the context' do
      entry = described_class.new(:test, test: 'something')

      expect(entry.value).to eq 'something'
    end
  end

  describe '#extensible?' do
    context 'when entry has inheritance defined' do
      it 'is extensible' do
        entry = described_class.new(:test, test: { extends: 'something' })

        expect(entry).to be_extensible
      end
    end

    context 'when entry does not have inheritance specified' do
      it 'is not extensible' do
        entry = described_class.new(:test, test: { script: 'something' })

        expect(entry).not_to be_extensible
      end
    end

    context 'when entry value is not a hash' do
      it 'is not extensible' do
        entry = described_class.new(:test, test: 'something')

        expect(entry).not_to be_extensible
      end
    end
  end

  describe '#extends_keys' do
    context 'when entry is extensible' do
      it 'returns symbolized extends key value' do
        entry = described_class.new(:test, test: { extends: 'something' })

        expect(entry.extends_keys).to eq [:something]
      end
    end

    context 'when entry is not extensible' do
      it 'returns nil' do
        entry = described_class.new(:test, test: 'something')

        expect(entry.extends_keys).to be_nil
      end
    end
  end

  describe '#ancestors' do
    let(:parent) do
      described_class.new(:test, test: { extends: 'something' })
    end

    let(:child) do
      described_class.new(:job, { job: { script: 'something' } }, parent)
    end

    it 'returns ancestors keys' do
      expect(child.ancestors).to eq [:test]
    end
  end

  describe '#base_hashes!' do
    subject { described_class.new(:test, hash) }

    context 'when base hash is not extensible' do
      let(:hash) do
        {
          template: { script: 'rspec' },
          test: { extends: 'template' }
        }
      end

      it 'returns unchanged base hashes' do
        expect(subject.base_hashes!).to eq([{ script: 'rspec' }])
      end
    end

    context 'when base hash is extensible too' do
      let(:hash) do
        {
          first: { script: 'rspec' },
          second: { extends: 'first' },
          test: { extends: 'second' }
        }
      end

      it 'extends the base hashes first' do
        expect(subject.base_hashes!).to eq([{ extends: 'first', script: 'rspec' }])
      end

      it 'mutates original context' do
        subject.base_hashes!

        expect(hash.fetch(:second)).to eq(extends: 'first', script: 'rspec')
      end
    end
  end

  describe '#extend!' do
    subject { described_class.new(:test, hash) }

    context 'when extending a non-hash value' do
      let(:hash) do
        {
          first: 'my value',
          test: { extends: 'first' }
        }
      end

      it 'raises an error' do
        expect { subject.extend! }
          .to raise_error(described_class::InvalidExtensionError,
                          /invalid base hash/)
      end
    end

    context 'when extending unknown key' do
      let(:hash) do
        { test: { extends: 'something' } }
      end

      it 'raises an error' do
        expect { subject.extend! }
          .to raise_error(described_class::InvalidExtensionError,
                          /unknown key/)
      end
    end

    context 'when extending a hash correctly' do
      let(:hash) do
        {
          first: { script: 'my value' },
          second: { extends: 'first' },
          test: { extends: 'second' }
        }
      end

      let(:result) do
        {
          first: { script: 'my value' },
          second: { extends: 'first', script: 'my value' },
          test: { extends: 'second', script: 'my value' }
        }
      end

      it 'returns extended part of the hash' do
        expect(subject.extend!).to eq result[:test]
      end

      it 'mutates original context' do
        subject.extend!

        expect(hash).to eq result
      end
    end

    context 'when extending multiple hashes correctly' do
      let(:hash) do
        {
          first: { script: 'my value', image: 'ubuntu' },
          second: { image: 'alpine' },
          test: { extends: %w(first second) }
        }
      end

      let(:result) do
        {
          first: { script: 'my value', image: 'ubuntu' },
          second: { image: 'alpine' },
          test: { extends: %w(first second), script: 'my value', image: 'alpine' }
        }
      end

      it 'returns extended part of the hash' do
        expect(subject.extend!).to eq result[:test]
      end

      it 'mutates original context' do
        subject.extend!

        expect(hash).to eq result
      end
    end

    context 'when hash is not extensible' do
      let(:hash) do
        {
          first: { script: 'my value' },
          second: { extends: 'first' },
          test: { value: 'something' }
        }
      end

      it 'returns original key value' do
        expect(subject.extend!).to eq(value: 'something')
      end

      it 'does not mutate orignal context' do
        original = hash.deep_dup

        subject.extend!

        expect(hash).to eq original
      end
    end

    context 'when circular depenency gets detected' do
      let(:hash) do
        { test: { extends: 'test' } }
      end

      it 'raises an error' do
        expect { subject.extend! }
          .to raise_error(described_class::CircularDependencyError,
                          /circular dependency detected/)
      end
    end

    context 'when nesting level is too deep' do
      before do
        stub_const("#{described_class}::MAX_NESTING_LEVELS", 0)
      end

      let(:hash) do
        {
          first: { script: 'my value' },
          second: { extends: 'first' },
          test: { extends: 'second' }
        }
      end

      it 'raises an error' do
        expect { subject.extend! }
          .to raise_error(described_class::NestingTooDeepError)
      end
    end
  end
end