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

require 'fast_spec_helper'
require 'support/helpers/stub_feature_flags'
require_dependency 'active_model'

RSpec.describe Gitlab::Ci::Config::Entry::Rules do
  let(:factory) do
    Gitlab::Config::Entry::Factory.new(described_class)
      .metadata(metadata)
      .value(config)
  end

  let(:metadata) { { allowed_when: %w[always never] } }
  let(:entry)    { factory.create! }

  describe '.new' do
    subject { entry }

    before do
      subject.compose!
    end

    context 'with a list of rule rule' do
      let(:config) do
        [{ if: '$THIS == "that"', when: 'never' }]
      end

      it { is_expected.to be_a(described_class) }
      it { is_expected.to be_valid }
    end

    context 'with a list of two rules' do
      let(:config) do
        [
          { if: '$THIS == "that"', when: 'always' },
          { if: '$SKIP',           when: 'never' }
        ]
      end

      it { is_expected.to be_valid }
    end

    context 'with a single rule object' do
      let(:config) do
        { if: '$SKIP', when: 'never' }
      end

      it { is_expected.not_to be_valid }
    end

    context 'with nested rules' do
      let(:config) do
        [
          { if: '$THIS == "that"', when: 'always' },
          [{ if: '$SKIP', when: 'never' }, { if: '$THIS == "other"', when: 'always' }]
        ]
      end

      it { is_expected.to be_valid }
    end

    context 'with rules nested more than one level' do
      let(:config) do
        [
          { if: '$THIS == "that"', when: 'always' },
          [{ if: '$SKIP', when: 'never' }, [{ if: '$THIS == "other"', when: 'always' }]]
        ]
      end

      it { is_expected.to be_valid }
    end
  end

  describe '#value' do
    subject { entry.value }

    context 'with a list of rule rule' do
      let(:config) do
        [{ if: '$THIS == "that"', when: 'never' }]
      end

      it { is_expected.to eq(config) }
    end

    context 'with a list of two rules' do
      let(:config) do
        [
          { if: '$THIS == "that"', when: 'always' },
          { if: '$SKIP',           when: 'never' }
        ]
      end

      it { is_expected.to eq(config) }
    end

    context 'with a single rule object' do
      let(:config) do
        { if: '$SKIP', when: 'never' }
      end

      it { is_expected.to eq([config]) }
    end

    context 'with nested rules' do
      let(:first_rule) { { if: '$THIS == "that"', when: 'always' } }
      let(:second_rule) { { if: '$SKIP', when: 'never' } }

      let(:config) do
        [
          first_rule,
          [second_rule]
        ]
      end

      it { is_expected.to contain_exactly(first_rule, second_rule) }
    end

    context 'with rules nested more than one level' do
      let(:first_rule) { { if: '$THIS == "that"', when: 'always' } }
      let(:second_rule) { { if: '$SKIP', when: 'never' } }
      let(:third_rule) { { if: '$THIS == "other"', when: 'always' } }

      let(:config) do
        [
          first_rule,
          [second_rule, [third_rule]]
        ]
      end

      it { is_expected.to contain_exactly(first_rule, second_rule, third_rule) }
    end
  end

  describe '.default' do
    it 'does not have default policy' do
      expect(described_class.default).to be_nil
    end
  end
end