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: 1ba5caa1d4bcdc2610059b300fd8e36fd743e17d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Config::External::Rules, feature_category: :pipeline_composition do
  # Remove `project` property when FF `ci_support_include_rules_when_never` is removed
  let(:context) { double(variables_hash: {}, project: nil) }
  let(:rule_hashes) { [{ if: '$MY_VAR == "hello"' }] }

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

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

    context 'when there is no rule' do
      let(:rule_hashes) {}

      it { is_expected.to eq(true) }
    end

    shared_examples 'when there is a rule with if' do |rule_matched_result = true, rule_not_matched_result = false|
      # Remove this `before` block when FF `ci_support_include_rules_when_never` is removed
      before do
        allow(context).to receive(:project).and_return(nil)
      end

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

        it { is_expected.to eq(rule_matched_result) }
      end

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

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

    shared_examples 'when there is a rule with exists' do |file_exists_result = true, file_not_exists_result = false|
      let(:project) { create(:project, :repository) }

      context 'when the file exists' do
        let(:context) { double(project: project, sha: project.repository.tree.sha, top_level_worktree_paths: ['Dockerfile']) }

        before do
          project.repository.create_file(project.first_owner, 'Dockerfile', "commit", message: 'test', branch_name: "master")
        end

        it { is_expected.to eq(file_exists_result) }
      end

      context 'when the file does not exist' do
        let(:context) { double(project: project, sha: project.repository.tree.sha, top_level_worktree_paths: ['test.md']) }

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

    it_behaves_like 'when there is a rule with if'

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

      it_behaves_like 'when there is a rule with exists'
    end

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

        it_behaves_like 'when there is a rule with if', false, false

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

          it_behaves_like 'when there is a rule with if'
        end
      end

      context 'with when: always' do
        let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: 'always' }] }

        it_behaves_like 'when there is a rule with if'

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

          it_behaves_like 'when there is a rule with if'
        end
      end

      context 'with when: <invalid string>' do
        let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: 'on_success' }] }

        it 'raises an error' do
          expect { result }.to raise_error(described_class::InvalidIncludeRulesError,
                                           'invalid include rule: {:if=>"$MY_VAR == \"hello\"", :when=>"on_success"}')
        end
      end

      context 'with when: null' do
        let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: nil }] }

        it_behaves_like 'when there is a rule with if'
      end
    end

    context 'when there is a rule with exists and when' do
      context 'with when: never' do
        let(:rule_hashes) { [{ exists: 'Dockerfile', when: 'never' }] }

        it_behaves_like 'when there is a rule with exists', false, false

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

          it_behaves_like 'when there is a rule with exists'
        end
      end

      context 'with when: always' do
        let(:rule_hashes) { [{ exists: 'Dockerfile', when: 'always' }] }

        it_behaves_like 'when there is a rule with exists'

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

          it_behaves_like 'when there is a rule with exists'
        end
      end

      context 'with when: <invalid string>' do
        let(:rule_hashes) { [{ exists: 'Dockerfile', when: 'on_success' }] }

        it 'raises an error' do
          expect { result }.to raise_error(described_class::InvalidIncludeRulesError,
                                           'invalid include rule: {:exists=>"Dockerfile", :when=>"on_success"}')
        end
      end

      context 'with when: null' do
        let(:rule_hashes) { [{ exists: 'Dockerfile', when: nil }] }

        it_behaves_like 'when there is a rule with exists'
      end
    end

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

      it 'raises an error' do
        expect { result }.to raise_error(described_class::InvalidIncludeRulesError,
                                         'invalid include rule: {:changes=>["$MY_VAR"]}')
      end
    end
  end
end