summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/build/rules/rule/clause/if_spec.rb
blob: 31c7437cfe0e9ad914558cd0fc2080833e7ee2da (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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'support/helpers/stubbed_feature'
require 'support/helpers/stub_feature_flags'

RSpec.describe Gitlab::Ci::Build::Rules::Rule::Clause::If do
  include StubFeatureFlags

  subject(:if_clause) { described_class.new(expression) }

  describe '#satisfied_by?' do
    let(:context_class) { Gitlab::Ci::Build::Context::Base }
    let(:rules_context) { instance_double(context_class, variables_hash: {}) }

    subject(:satisfied_by?) { if_clause.satisfied_by?(nil, rules_context) }

    context 'when expression is a basic string comparison' do
      context 'when comparison is true' do
        let(:expression) { '"value" == "value"' }

        it { is_expected.to eq(true) }
      end

      context 'when comparison is false' do
        let(:expression) { '"value" == "other"' }

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

    context 'when expression is a regexp' do
      context 'when comparison is true' do
        let(:expression) { '"abcde" =~ /^ab.*/' }

        it { is_expected.to eq(true) }
      end

      context 'when comparison is false' do
        let(:expression) { '"abcde" =~ /^af.*/' }

        it { is_expected.to eq(false) }
      end

      context 'when both side of the expression are variables' do
        let(:expression) { '$teststring =~ $pattern' }

        context 'when comparison is true' do
          let(:rules_context) do
            instance_double(context_class, variables_hash: { 'teststring' => 'abcde', 'pattern' => '/^ab.*/' })
          end

          it { is_expected.to eq(true) }
        end

        context 'when comparison is false' do
          let(:rules_context) do
            instance_double(context_class, variables_hash: { 'teststring' => 'abcde', 'pattern' => '/^af.*/' })
          end

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