summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/chain/skip_spec.rb
blob: ae427eaf403ab9f5983b93f5f16c82bae0eb28e1 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Pipeline::Chain::Skip do
  let_it_be(:project, reload: true) { create(:project) }
  let_it_be(:user) { create(:user) }
  let_it_be(:pipeline, reload: true) { create(:ci_pipeline, project: project) }

  let(:command) do
    Gitlab::Ci::Pipeline::Chain::Command.new(
      project: project,
      current_user: user,
      ignore_skip_ci: false,
      save_incompleted: true)
  end

  let(:step) { described_class.new(pipeline, command) }

  context 'when pipeline has been skipped by a user' do
    before do
      allow(pipeline).to receive(:git_commit_message)
        .and_return('commit message [ci skip]')
    end

    it 'breaks the chain' do
      step.perform!

      expect(step.break?).to be true
    end

    it 'skips the pipeline' do
      step.perform!

      expect(pipeline.reload).to be_skipped
    end

    it 'calls ensure_project_iid explicitly' do
      expect(pipeline).to receive(:ensure_project_iid!)

      step.perform!
    end

    context 'when the ci_pipeline_ensure_iid_on_save feature flag is off' do
      before do
        stub_feature_flags(ci_pipeline_ensure_iid_on_skip: false)
      end

      it 'does not call ensure_project_iid explicitly' do
        expect(pipeline).not_to receive(:ensure_project_iid!)

        step.perform!
      end
    end
  end

  context 'when pipeline has not been skipped' do
    before do
      step.perform!
    end

    it 'does not break the chain' do
      expect(step.break?).to be false
    end

    it 'does not skip a pipeline chain' do
      expect(pipeline.reload).not_to be_skipped
    end
  end

  context 'when [ci skip] should be ignored' do
    let(:command) do
      double('command', project: project,
                        current_user: user,
                        ignore_skip_ci: true)
    end

    it 'does not break the chain' do
      step.perform!

      expect(step.break?).to be false
    end
  end

  context 'when pipeline should be skipped but not persisted' do
    let(:command) do
      double('command', project: project,
                        current_user: user,
                        ignore_skip_ci: false,
                        save_incompleted: false)
    end

    before do
      allow(pipeline).to receive(:git_commit_message)
        .and_return('commit message [ci skip]')

      step.perform!
    end

    it 'breaks the chain' do
      expect(step.break?).to be true
    end

    it 'does not skip pipeline' do
      expect(pipeline.reload).not_to be_skipped
    end
  end
end