Commit ac65257c authored by Grzegorz Bizon's avatar Grzegorz Bizon

Raise variables statement exception if pattern is invalid

parent b784a985
...@@ -11,8 +11,10 @@ module Gitlab ...@@ -11,8 +11,10 @@ module Gitlab
end end
def evaluate(variables = {}) def evaluate(variables = {})
Gitlab::UntrustedRegexp.new(@value.to_s) # TODO multiline support
# TODO raise LexerError / ParserError in case of RegexpError @regexp = Gitlab::UntrustedRegexp.new(@value)
rescue RegexpError
raise Parser::ParserError, 'Invalid regular expression!'
end end
def self.build(string) def self.build(string)
......
...@@ -3,6 +3,8 @@ module Gitlab ...@@ -3,6 +3,8 @@ module Gitlab
module Pipeline module Pipeline
module Expression module Expression
class Parser class Parser
ParserError = Class.new(Statement::StatementError)
def initialize(tokens) def initialize(tokens)
@tokens = tokens.to_enum @tokens = tokens.to_enum
@nodes = [] @nodes = []
......
...@@ -47,9 +47,16 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do ...@@ -47,9 +47,16 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
describe '#evaluate' do describe '#evaluate' do
it 'returns a regular expression' 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 end
end end
require 'spec_helper' require 'fast_spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Parser do describe Gitlab::Ci::Pipeline::Expression::Parser do
describe '#tree' do describe '#tree' do
......
require 'spec_helper' require 'fast_spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Token do describe Gitlab::Ci::Pipeline::Expression::Token do
let(:value) { '$VARIABLE' } let(:value) { '$VARIABLE' }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment