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

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

require 'spec_helper'

RSpec.describe Gitlab::Ci::Config::External::Rules, feature_category: :pipeline_composition do
  let(:context) { double(variables_hash: {}) }
  let(:rule_hashes) {}
  let(:pipeline) { instance_double(Ci::Pipeline) }
  let_it_be(:project) { create(:project, :custom_repo, files: { 'file.txt' => 'file' }) }

  subject(:rules) { described_class.new(rule_hashes) }

  before do
    allow(context).to receive(:project).and_return(project)
    allow(context).to receive(:pipeline).and_return(pipeline)
  end

  describe '#evaluate' do
    subject(:result) { rules.evaluate(context).pass? }

    context 'when there is no rule' do
      it { is_expected.to eq(true) }
    end

    shared_examples 'with when: specified' do
      context 'with when: never' do
        before do
          rule_hashes.first[:when] = 'never'
        end

        it { is_expected.to eq(false) }
      end

      context 'with when: always' do
        before do
          rule_hashes.first[:when] = 'always'
        end

        it { is_expected.to eq(true) }
      end

      context 'with when: <invalid string>' do
        before do
          rule_hashes.first[:when] = 'on_success'
        end

        it 'raises an error' do
          expect { result }.to raise_error(described_class::InvalidIncludeRulesError, /when unknown value: on_success/)
        end
      end

      context 'with when: null' do
        before do
          rule_hashes.first[:when] = nil
        end

        it { is_expected.to eq(true) }
      end
    end

    context 'when there is a rule with if:' do
      let(:rule_hashes) { [{ if: '$MY_VAR == "hello"' }] }

      context 'when the rule matches' do
        let(:context) { double(variables_hash: { 'MY_VAR' => 'hello' }) }

        it { is_expected.to eq(true) }

        it_behaves_like 'with when: specified'
      end

      context 'when the rule does not match' do
        let(:context) { double(variables_hash: { 'MY_VAR' => 'invalid' }) }

        it { is_expected.to eq(false) }
      end
    end

    context 'when there is a rule with exists:' do
      let(:rule_hashes) { [{ exists: 'file.txt' }] }

      context 'when the file exists' do
        let(:context) { double(top_level_worktree_paths: ['file.txt']) }

        it { is_expected.to eq(true) }

        it_behaves_like 'with when: specified'
      end

      context 'when the file does not exist' do
        let(:context) { double(top_level_worktree_paths: ['README.md']) }

        it { is_expected.to eq(false) }
      end
    end

    context 'when there is a rule with changes:' do
      let(:rule_hashes) { [{ changes: ['file.txt'] }] }

      shared_examples 'when the pipeline has modified paths' do
        let(:modified_paths) { ['file.txt'] }

        before do
          allow(pipeline).to receive(:modified_paths).and_return(modified_paths)
        end

        context 'when the file has changed' do
          it { is_expected.to eq(true) }

          it_behaves_like 'with when: specified'
        end

        context 'when the file has not changed' do
          let(:modified_paths) { ['README.md'] }

          it { is_expected.to eq(false) }
        end
      end

      it_behaves_like 'when the pipeline has modified paths'

      context 'with paths: specified' do
        let(:rule_hashes) { [{ changes: { paths: ['file.txt'] } }] }

        it_behaves_like 'when the pipeline has modified paths'
      end

      context 'with paths: and compare_to: specified' do
        before_all do
          project.repository.add_branch(project.owner, 'branch1', 'master')

          project.repository.update_file(
            project.owner, 'file.txt', 'file updated', message: 'Update file.txt', branch_name: 'branch1'
          )

          project.repository.add_branch(project.owner, 'branch2', 'branch1')
        end

        let_it_be(:pipeline) do
          build(:ci_pipeline, project: project, ref: 'branch2', sha: project.commit('branch2').sha)
        end

        context 'when the file has changed compared to the given ref' do
          let(:rule_hashes) { [{ changes: { paths: ['file.txt'], compare_to: 'master' } }] }

          it { is_expected.to eq(true) }

          it_behaves_like 'with when: specified'
        end

        context 'when the file has not changed compared to the given ref' do
          let(:rule_hashes) { [{ changes: { paths: ['file.txt'], compare_to: 'branch1' } }] }

          it { is_expected.to eq(false) }
        end

        context 'when compare_to: is invalid' do
          let(:rule_hashes) { [{ changes: { paths: ['file.txt'], compare_to: 'invalid' } }] }

          it 'raises an error' do
            expect { result }.to raise_error(described_class::InvalidIncludeRulesError, /compare_to is not a valid ref/)
          end
        end
      end
    end

    context 'when there is a rule with an invalid key' do
      let(:rule_hashes) { [{ invalid: ['$MY_VAR'] }] }

      it 'raises an error' do
        expect { result }.to raise_error(described_class::InvalidIncludeRulesError, /contains unknown keys: invalid/)
      end
    end
  end
end