summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/expression/lexeme/pattern_spec.rb
blob: 6ce4b321397c21670187d32144daac0d44cc5183 (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
require 'spec_helper'

describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
  describe '.build' do
    it 'creates a new instance of the token' do
      expect(described_class.build('/.*/'))
        .to be_a(described_class)
    end

    it 'raises an error if pattern is invalid' do
      expect { described_class.build('/ some ( thin/i') }
        .to raise_error(Gitlab::Ci::Pipeline::Expression::Lexer::SyntaxError)
    end
  end

  describe '.type' do
    it 'is a value lexeme' do
      expect(described_class.type).to eq :value
    end
  end

  describe '.scan' do
    it 'correctly identifies a pattern token' do
      scanner = StringScanner.new('/pattern/')

      token = described_class.scan(scanner)

      expect(token).not_to be_nil
      expect(token.build.evaluate)
        .to eq Gitlab::UntrustedRegexp.new('pattern')
    end

    it 'does not allow to use an empty pattern' do
      scanner = StringScanner.new(%(//))

      token = described_class.scan(scanner)

      expect(token).to be_nil
    end

    it 'support single flag' do
      scanner = StringScanner.new('/pattern/i')

      token = described_class.scan(scanner)

      expect(token).not_to be_nil
      expect(token.build.evaluate)
        .to eq Gitlab::UntrustedRegexp.new('(?i)pattern')
    end

    it 'support multiple flags' do
      scanner = StringScanner.new('/pattern/im')

      token = described_class.scan(scanner)

      expect(token).not_to be_nil
      expect(token.build.evaluate)
        .to eq Gitlab::UntrustedRegexp.new('(?im)pattern')
    end

    it 'ignores unsupported flags' do
      scanner = StringScanner.new('/pattern/x')

      token = described_class.scan(scanner)

      expect(token).not_to be_nil
      expect(token.build.evaluate)
        .to eq Gitlab::UntrustedRegexp.new('pattern')
    end

    it 'is a eager scanner for regexp boundaries' do
      scanner = StringScanner.new('/some .* / pattern/')

      token = described_class.scan(scanner)

      expect(token).not_to be_nil
      expect(token.build.evaluate)
        .to eq Gitlab::UntrustedRegexp.new('some .* ')
    end

    it 'does not match on escaped regexp boundaries' do
      scanner = StringScanner.new('/some .* \/ pattern/')

      token = described_class.scan(scanner)

      expect(token).not_to be_nil
      expect(token.build.evaluate)
        .to eq Gitlab::UntrustedRegexp.new('some .* / pattern')
    end

    it 'recognizes \ as an escape character for /' do
      scanner = StringScanner.new('/some numeric \/$ pattern/')

      token = described_class.scan(scanner)

      expect(token).not_to be_nil
      expect(token.build.evaluate)
        .to eq Gitlab::UntrustedRegexp.new('some numeric /$ pattern')
    end

    it 'does not recognize \ as an escape character for $' do
      scanner = StringScanner.new('/some numeric \$ pattern/')

      token = described_class.scan(scanner)

      expect(token).not_to be_nil
      expect(token.build.evaluate)
        .to eq Gitlab::UntrustedRegexp.new('some numeric \$ pattern')
    end
  end

  describe '#evaluate' do
    it 'returns a regular expression' do
      regexp = described_class.new('/abc/')

      expect(regexp.evaluate).to eq Gitlab::UntrustedRegexp.new('abc')
    end

    it 'raises error if evaluated regexp is not valid' do
      allow(Gitlab::UntrustedRegexp::RubySyntax).to receive(:valid?).and_return(true)

      regexp = described_class.new('/invalid ( .*/')

      expect { regexp.evaluate }
        .to raise_error(Gitlab::Ci::Pipeline::Expression::RuntimeError)
    end
  end
end