Welcome to mirror list, hosted at ThFree Co, Russian Federation.

inputs_spec.rb « interpolation « config « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b0618081207b38e6d014f58e60532081ce8eb445 (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
306
307
308
309
310
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Config::Interpolation::Inputs, feature_category: :pipeline_composition do
  let(:inputs) { described_class.new(specs, args) }
  let(:specs) { { foo: { default: 'bar' } } }
  let(:args) { {} }

  context 'when given unrecognized inputs' do
    let(:specs) { { foo: nil } }
    let(:args) { { foo: 'bar', test: 'bar' } }

    it 'is invalid' do
      expect(inputs).not_to be_valid
      expect(inputs.errors).to contain_exactly('unknown input arguments: test')
    end
  end

  context 'when given unrecognized configuration keywords' do
    let(:specs) { { foo: 123 } }
    let(:args) { {} }

    it 'is invalid' do
      expect(inputs).not_to be_valid
      expect(inputs.errors).to contain_exactly(
        'unknown input specification for `foo` (valid types: boolean, number, string)'
      )
    end
  end

  context 'when the inputs have multiple errors' do
    let(:specs) { { foo: nil } }
    let(:args) { { test: 'bar', gitlab: '1' } }

    it 'reports all of them' do
      expect(inputs).not_to be_valid
      expect(inputs.errors).to contain_exactly(
        'unknown input arguments: test, gitlab',
        '`foo` input: required value has not been provided'
      )
    end
  end

  describe 'required inputs' do
    let(:specs) { { foo: nil } }

    context 'when a value is given' do
      let(:args) { { foo: 'bar' } }

      it 'is valid' do
        expect(inputs).to be_valid
        expect(inputs.to_hash).to eq(foo: 'bar')
      end
    end

    context 'when no value is given' do
      let(:args) { {} }

      it 'is invalid' do
        expect(inputs).not_to be_valid
        expect(inputs.errors).to contain_exactly('`foo` input: required value has not been provided')
      end
    end
  end

  describe 'inputs with a default value' do
    let(:specs) { { foo: { default: 'bar' } } }

    context 'when a value is given' do
      let(:args) { { foo: 'test' } }

      it 'uses the given value' do
        expect(inputs).to be_valid
        expect(inputs.to_hash).to eq(foo: 'test')
      end
    end

    context 'when no value is given' do
      let(:args) { {} }

      it 'uses the default value' do
        expect(inputs).to be_valid
        expect(inputs.to_hash).to eq(foo: 'bar')
      end
    end
  end

  describe 'inputs with type validation' do
    describe 'string validation' do
      let(:specs) { { a_input: nil, b_input: { default: 'test' }, c_input: { default: 123 } } }
      let(:args) { { a_input: 123, b_input: 123, c_input: 'test' } }

      it 'is the default type' do
        expect(inputs).not_to be_valid
        expect(inputs.errors).to contain_exactly(
          '`a_input` input: provided value is not a string',
          '`b_input` input: provided value is not a string',
          '`c_input` input: default value is not a string'
        )
      end

      context 'when the value is a string' do
        let(:specs) { { foo: { type: 'string' } } }
        let(:args) { { foo: 'bar' } }

        it 'is valid' do
          expect(inputs).to be_valid
          expect(inputs.to_hash).to eq(foo: 'bar')
        end
      end

      context 'when the default is a string' do
        let(:specs) { { foo: { type: 'string', default: 'bar' } } }
        let(:args) { {} }

        it 'is valid' do
          expect(inputs).to be_valid
          expect(inputs.to_hash).to eq(foo: 'bar')
        end
      end

      context 'when the value is not a string' do
        let(:specs) { { foo: { type: 'string' } } }
        let(:args) { { foo: 123 } }

        it 'is invalid' do
          expect(inputs).not_to be_valid
          expect(inputs.errors).to contain_exactly('`foo` input: provided value is not a string')
        end
      end

      context 'when the default is not a string' do
        let(:specs) { { foo: { default: 123, type: 'string' } } }
        let(:args) { {} }

        it 'is invalid' do
          expect(inputs).not_to be_valid
          expect(inputs.errors).to contain_exactly('`foo` input: default value is not a string')
        end
      end
    end

    describe 'number validation' do
      let(:specs) { { integer: { type: 'number' }, float: { type: 'number' } } }

      context 'when the value is a float or integer' do
        let(:args) { { integer: 6, float: 6.6 } }

        it 'is valid' do
          expect(inputs).to be_valid
          expect(inputs.to_hash).to eq(integer: 6, float: 6.6)
        end
      end

      context 'when the default is a float or integer' do
        let(:specs) { { integer: { default: 6, type: 'number' }, float: { default: 6.6, type: 'number' } } }

        it 'is valid' do
          expect(inputs).to be_valid
          expect(inputs.to_hash).to eq(integer: 6, float: 6.6)
        end
      end

      context 'when the value is not a number' do
        let(:specs) { { number_input: { type: 'number' } } }
        let(:args) { { number_input: 'NaN' } }

        it 'is invalid' do
          expect(inputs).not_to be_valid
          expect(inputs.errors).to contain_exactly('`number_input` input: provided value is not a number')
        end
      end

      context 'when the default is not a number' do
        let(:specs) { { number_input: { default: 'NaN', type: 'number' } } }
        let(:args) { {} }

        it 'is invalid' do
          expect(inputs).not_to be_valid
          expect(inputs.errors).to contain_exactly('`number_input` input: default value is not a number')
        end
      end
    end

    describe 'boolean validation' do
      context 'when the value is true or false' do
        let(:specs) { { truthy: { type: 'boolean' }, falsey: { type: 'boolean' } } }
        let(:args) { { truthy: true, falsey: false } }

        it 'is valid' do
          expect(inputs).to be_valid
          expect(inputs.to_hash).to eq(truthy: true, falsey: false)
        end
      end

      context 'when the default is true or false' do
        let(:specs) { { truthy: { default: true, type: 'boolean' }, falsey: { default: false, type: 'boolean' } } }
        let(:args) { {} }

        it 'is valid' do
          expect(inputs).to be_valid
          expect(inputs.to_hash).to eq(truthy: true, falsey: false)
        end
      end

      context 'when the value is not a boolean' do
        let(:specs) { { boolean_input: { type: 'boolean' } } }
        let(:args) { { boolean_input: 'string' } }

        it 'is invalid' do
          expect(inputs).not_to be_valid
          expect(inputs.errors).to contain_exactly('`boolean_input` input: provided value is not a boolean')
        end
      end

      context 'when the default is not a boolean' do
        let(:specs) { { boolean_input: { default: 'string', type: 'boolean' } } }
        let(:args) { {} }

        it 'is invalid' do
          expect(inputs).not_to be_valid
          expect(inputs.errors).to contain_exactly('`boolean_input` input: default value is not a boolean')
        end
      end
    end

    context 'when given an unknown type' do
      let(:specs) { { unknown: { type: 'datetime' } } }
      let(:args) { { unknown: '2023-10-31' } }

      it 'is invalid' do
        expect(inputs).not_to be_valid
        expect(inputs.errors).to contain_exactly(
          'unknown input specification for `unknown` (valid types: boolean, number, string)'
        )
      end
    end
  end

  describe 'inputs with RegEx validation' do
    context 'when given a value that matches the pattern' do
      let(:specs) { { test_input: { regex: '^input_value$' } } }
      let(:args) { { test_input: 'input_value' } }

      it 'is valid' do
        expect(inputs).to be_valid
        expect(inputs.to_hash).to eq(test_input: 'input_value')
      end
    end

    context 'when given a default that matches the pattern' do
      let(:specs) { { test_input: { default: 'input_value', regex: '^input_value$' } } }
      let(:args) { {} }

      it 'is valid' do
        expect(inputs).to be_valid
        expect(inputs.to_hash).to eq(test_input: 'input_value')
      end
    end

    context 'when given a value that does not match the pattern' do
      let(:specs) { { test_input: { regex: '^input_value$' } } }
      let(:args) { { test_input: 'input' } }

      it 'is invalid' do
        expect(inputs).not_to be_valid
        expect(inputs.errors).to contain_exactly(
          '`test_input` input: provided value does not match required RegEx pattern'
        )
      end
    end

    context 'when given a default that does not match the pattern' do
      let(:specs) { { test_input: { default: 'input', regex: '^input_value$' } } }
      let(:args) { {} }

      it 'is invalid' do
        expect(inputs).not_to be_valid
        expect(inputs.errors).to contain_exactly(
          '`test_input` input: default value does not match required RegEx pattern'
        )
      end
    end

    context 'when used with any type other than `string`' do
      let(:specs) { { test_input: { regex: '^input_value$', type: 'number' } } }
      let(:args) { { test_input: 999 } }

      it 'is invalid' do
        expect(inputs).not_to be_valid
        expect(inputs.errors).to contain_exactly(
          '`test_input` input: RegEx validation can only be used with string inputs'
        )
      end
    end

    context 'when the pattern is unsafe' do
      let(:specs) { { test_input: { regex: 'a++' } } }
      let(:args) { { test_input: 'aaaaaaaaaaaaaaaaaaaaa' } }

      it 'is invalid' do
        expect(inputs).not_to be_valid
        expect(inputs.errors).to contain_exactly(
          '`test_input` input: invalid regular expression'
        )
      end
    end
  end
end