blob: 0505b17ea914985e278b80e18733909d7296947b (
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
|
# frozen_string_literal: true
require 'fast_spec_helper'
RSpec.describe Gitlab::Ci::Config::Entry::Include::Rules::Rule do
let(:factory) do
Gitlab::Config::Entry::Factory.new(described_class)
.value(config)
end
subject(:entry) { factory.create! }
describe '.new' do
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 { is_expected.to be_valid }
end
context 'when specifying an exists: clause' do
let(:config) { { exists: './this.md' } }
it { is_expected.to be_valid }
end
context 'using a list of multiple expressions' do
let(:config) { { if: ['$MY_VAR == "this"', '$YOUR_VAR == "that"'] } }
it_behaves_like 'an invalid config', /invalid expression syntax/
end
context 'when specifying an invalid if: clause expression' do
let(:config) { { if: ['$MY_VAR =='] } }
it_behaves_like 'an invalid config', /invalid expression syntax/
end
context 'when specifying an if: clause expression with an invalid token' do
let(:config) { { if: ['$MY_VAR == 123'] } }
it_behaves_like 'an invalid config', /invalid expression syntax/
end
context 'when using invalid regex in an if: clause' do
let(:config) { { if: ['$MY_VAR =~ /some ( thing/'] } }
it_behaves_like 'an invalid config', /invalid expression syntax/
end
context 'when using an if: clause with lookahead regex character "?"' do
let(:config) { { if: '$CI_COMMIT_REF =~ /^(?!master).+/' } }
context 'when allow_unsafe_ruby_regexp is disabled' do
it_behaves_like 'an invalid config', /invalid expression syntax/
end
end
context 'when specifying unknown policy' do
let(:config) { { invalid: :something } }
it_behaves_like 'an invalid config', /unknown keys: invalid/
end
context 'when clause is empty' do
let(:config) { {} }
it_behaves_like 'an invalid config', /can't be blank/
end
context 'when policy strategy does not match' do
let(:config) { 'string strategy' }
it_behaves_like 'an invalid config', /should be a hash/
end
end
describe '#value' do
subject(:value) { entry.value }
context 'when specifying an if: clause' do
let(:config) { { if: '$THIS || $THAT' } }
it 'returns the config' do
expect(subject).to eq(if: '$THIS || $THAT')
end
end
context 'when specifying an exists: clause' do
let(:config) { { exists: './test.md' } }
it 'returns the config' do
expect(subject).to eq(exists: './test.md')
end
end
end
end
|