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

rule_spec.rb « rules « include « entry « config « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dd15b049b9b896989fbb4c1a2574d50a5d9df576 (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
# frozen_string_literal: true

require 'spec_helper' # Change this to fast spec helper when FF `ci_refactor_external_rules` is removed
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
    it { is_expected.to be_valid }

    it 'returns the expected value' do
      expect(entry.value).to eq(config.compact)
    end

    context 'when FF `ci_refactor_external_rules` is disabled' do
      before do
        stub_feature_flags(ci_refactor_external_rules: false)
      end

      it 'returns the expected value' do
        expect(entry.value).to eq(config)
      end
    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 array' do
      let(:config) { { exists: ['./this.md', './that.md'] } }

      it_behaves_like 'a valid config'
    end

    context 'when null' do
      let(:config) { { exists: nil } }

      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