summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/expression/lexeme/matches_spec.rb
blob: 83742699d3d6858b2b2a44f94b578f7af5ca9497 (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
176
177
178
179
180
# frozen_string_literal: true

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

RSpec.describe Gitlab::Ci::Pipeline::Expression::Lexeme::Matches do
  include StubFeatureFlags

  let(:left) { double('left') }
  let(:right) { double('right') }

  describe '.build' do
    context 'with non-evaluable operands' do
      it 'creates a new instance of the token' do
        expect { described_class.build('=~', left, right) }
          .to raise_error Gitlab::Ci::Pipeline::Expression::Lexeme::Operator::OperatorError
      end
    end

    context 'with evaluable operands' do
      it 'creates a new instance of the token' do
        allow(left).to receive(:evaluate).and_return('my-string')
        allow(right).to receive(:evaluate).and_return('/my-string/')

        expect(described_class.build('=~', left, right))
          .to be_a(described_class)
      end
    end
  end

  describe '.type' do
    it 'is an operator' do
      expect(described_class.type).to eq :logical_operator
    end
  end

  describe '.precedence' do
    it 'has a precedence' do
      expect(described_class.precedence).to be_an Integer
    end
  end

  describe '#evaluate' do
    let(:operator) { described_class.new(left, right) }

    subject { operator.evaluate }

    before do
      allow(left).to receive(:evaluate).and_return(left_value)
      allow(right).to receive(:evaluate).and_return(right_value)
    end

    context 'when left and right do not match' do
      let(:left_value)  { 'my-string' }
      let(:right_value) { Gitlab::UntrustedRegexp.new('something') }

      it { is_expected.to eq(false) }
    end

    context 'when left and right match' do
      let(:left_value)  { 'my-awesome-string' }
      let(:right_value) { Gitlab::UntrustedRegexp.new('awesome.string$') }

      it { is_expected.to eq(true) }
    end

    context 'when left is nil' do
      let(:left_value)  { nil }
      let(:right_value) { Gitlab::UntrustedRegexp.new('pattern') }

      it { is_expected.to eq(false) }
    end

    context 'when right is nil' do
      let(:left_value)  { 'my-awesome-string' }
      let(:right_value) { nil }

      it { is_expected.to eq(false) }
    end

    context 'when left and right are nil' do
      let(:left_value)  { nil }
      let(:right_value) { nil }

      it { is_expected.to eq(false) }
    end

    context 'when left is an empty string' do
      let(:left_value)  { '' }
      let(:right_value) { Gitlab::UntrustedRegexp.new('pattern') }

      it { is_expected.to eq(false) }
    end

    context 'when left and right are empty strings' do
      let(:left_value)  { '' }
      let(:right_value) { Gitlab::UntrustedRegexp.new('') }

      it { is_expected.to eq(true) }
    end

    context 'when left is a multiline string and matches right' do
      let(:left_value) do
        <<~TEXT
          My awesome contents

          My-text-string!
        TEXT
      end

      let(:right_value) { Gitlab::UntrustedRegexp.new('text-string') }

      it { is_expected.to eq(true) }
    end

    context 'when left is a multiline string and does not match right' do
      let(:left_value) do
        <<~TEXT
          My awesome contents

          My-terrible-string!
        TEXT
      end

      let(:right_value) { Gitlab::UntrustedRegexp.new('text-string') }

      it { is_expected.to eq(false) }
    end

    context 'when a matching pattern uses regex flags' do
      let(:left_value) do
        <<~TEXT
          My AWESOME content
        TEXT
      end

      let(:right_value) { Gitlab::UntrustedRegexp.new('(?i)awesome') }

      it { is_expected.to eq(true) }
    end

    context 'when a non-matching pattern uses regex flags' do
      let(:left_value) do
        <<~TEXT
          My AWESOME content
        TEXT
      end

      let(:right_value) { Gitlab::UntrustedRegexp.new('(?i)terrible') }

      it { is_expected.to eq(false) }
    end

    context 'when right value is a regexp string' do
      let(:right_value) { '/^ab.*/' }

      context 'when matching' do
        let(:left_value) { 'abcde' }

        it { is_expected.to eq(true) }

        context 'when the FF ci_fix_rules_if_comparison_with_regexp_variable is disabled' do
          before do
            stub_feature_flags(ci_fix_rules_if_comparison_with_regexp_variable: false)
          end

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

      context 'when not matching' do
        let(:left_value) { 'dfg' }

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