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
|
# frozen_string_literal: true
require 'fast_spec_helper'
require_dependency 'active_model'
RSpec.describe Gitlab::Ci::Config::Entry::Include::Rules::Rule, feature_category: :pipeline_composition do
let(:factory) do
Gitlab::Config::Entry::Factory.new(described_class).value(config)
end
subject(:entry) { factory.create! }
before do
entry.compose!
end
shared_examples 'a valid config' do |expected_value = nil|
it { is_expected.to be_valid }
it 'returns the expected value' do
expect(entry.value).to eq(expected_value || config.compact)
end
end
shared_examples 'an invalid config' do |error_message|
it { is_expected.not_to be_valid }
it 'has errors' do
expect(entry.errors).to include(error_message)
end
end
context 'when specifying an if: clause' do
let(:config) { { if: '$THIS || $THAT' } }
it_behaves_like 'a valid config'
context 'with when:' do
let(:config) { { if: '$THIS || $THAT', when: 'never' } }
it_behaves_like 'a valid config'
end
context 'with when: <invalid string>' do
let(:config) { { if: '$THIS || $THAT', when: 'on_success' } }
it_behaves_like 'an invalid config', /when unknown value: on_success/
end
context 'with when: null' do
let(:config) { { if: '$THIS || $THAT', when: nil } }
it_behaves_like 'a valid config'
end
context 'when if: clause is invalid' do
let(:config) { { if: '$MY_VAR ==' } }
it_behaves_like 'an invalid config', /invalid expression syntax/
end
context 'when if: clause has an integer operand' do
let(:config) { { if: '$MY_VAR == 123' } }
it_behaves_like 'an invalid config', /invalid expression syntax/
end
context 'when if: clause has invalid regex' do
let(:config) { { if: '$MY_VAR =~ /some ( thing/' } }
it_behaves_like 'an invalid config', /invalid expression syntax/
end
context 'when if: clause has lookahead regex character "?"' do
let(:config) { { if: '$CI_COMMIT_REF =~ /^(?!master).+/' } }
it_behaves_like 'an invalid config', /invalid expression syntax/
end
context 'when if: clause has array of expressions' do
let(:config) { { if: ['$MY_VAR == "this"', '$YOUR_VAR == "that"'] } }
it_behaves_like 'an invalid config', /invalid expression syntax/
end
end
context 'when specifying an exists: clause' do
let(:config) { { exists: './this.md' } }
it_behaves_like 'a valid config'
context 'when exists: clause is an array' do
let(:config) { { exists: ['./this.md', './that.md'] } }
it_behaves_like 'a valid config'
end
context 'when exists: clause is null' do
let(:config) { { exists: nil } }
it_behaves_like 'a valid config'
end
end
context 'when specifying a changes: clause' do
let(:config) { { changes: %w[Dockerfile lib/* paths/**/*.rb] } }
it_behaves_like 'a valid config', { changes: { paths: %w[Dockerfile lib/* paths/**/*.rb] } }
context 'with paths:' do
let(:config) { { changes: { paths: %w[Dockerfile lib/* paths/**/*.rb] } } }
it_behaves_like 'a valid config'
end
context 'with paths: and compare_to:' do
let(:config) { { changes: { paths: ['Dockerfile'], compare_to: 'branch1' } } }
it_behaves_like 'a valid config'
end
end
context 'when specifying an unknown keyword' do
let(:config) { { invalid: :something } }
it_behaves_like 'an invalid config', /unknown keys: invalid/
end
context 'when config is blank' do
let(:config) { {} }
it_behaves_like 'an invalid config', /can't be blank/
end
context 'when config type is invalid' do
let(:config) { 'invalid' }
it_behaves_like 'an invalid config', /should be a hash/
end
end
|