Raise variables statement exception if pattern is invalid

This commit is contained in:
Grzegorz Bizon 2018-05-14 14:38:08 +02:00
parent b784a985f2
commit ac65257c40
5 changed files with 17 additions and 6 deletions

View file

@ -11,8 +11,10 @@ module Gitlab
end
def evaluate(variables = {})
Gitlab::UntrustedRegexp.new(@value.to_s)
# TODO raise LexerError / ParserError in case of RegexpError
# TODO multiline support
@regexp = Gitlab::UntrustedRegexp.new(@value)
rescue RegexpError
raise Parser::ParserError, 'Invalid regular expression!'
end
def self.build(string)

View file

@ -3,6 +3,8 @@ module Gitlab
module Pipeline
module Expression
class Parser
ParserError = Class.new(Statement::StatementError)
def initialize(tokens)
@tokens = tokens.to_enum
@nodes = []

View file

@ -47,9 +47,16 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
describe '#evaluate' do
it 'returns a regular expression' do
string = described_class.new('abc')
regexp = described_class.new('abc')
expect(string.evaluate).to eq Gitlab::UntrustedRegexp.new('abc')
expect(regexp.evaluate).to eq Gitlab::UntrustedRegexp.new('abc')
end
it 'raises error if evaluated regexp is not valid' do
regexp = described_class.new('invalid ( .*')
expect { regexp.evaluate }
.to raise_error(Gitlab::Ci::Pipeline::Expression::Parser::ParserError)
end
end
end

View file

@ -1,4 +1,4 @@
require 'spec_helper'
require 'fast_spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Parser do
describe '#tree' do

View file

@ -1,4 +1,4 @@
require 'spec_helper'
require 'fast_spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Token do
let(:value) { '$VARIABLE' }