diff --git a/lib/gitlab/ci/pipeline/expression/null.rb b/lib/gitlab/ci/pipeline/expression/null.rb index a70cdd5fc73..31dcba05b48 100644 --- a/lib/gitlab/ci/pipeline/expression/null.rb +++ b/lib/gitlab/ci/pipeline/expression/null.rb @@ -6,7 +6,7 @@ module Gitlab PATTERN = /null/.freeze TYPE = :value - def initialize(value) + def initialize(value = nil) @value = value end diff --git a/lib/gitlab/ci/pipeline/expression/parser.rb b/lib/gitlab/ci/pipeline/expression/parser.rb index 6eb77351dcf..350c9e543ac 100644 --- a/lib/gitlab/ci/pipeline/expression/parser.rb +++ b/lib/gitlab/ci/pipeline/expression/parser.rb @@ -4,9 +4,7 @@ module Gitlab module Expression class Parser def initialize(tokens) - # raise ArgumentError unless tokens.enumerator? - - @tokens = tokens + @tokens = tokens.to_enum @nodes = [] end @@ -14,10 +12,7 @@ module Gitlab while token = @tokens.next case token.type when :operator - lookbehind = @nodes.last - lookahead = Parser.new(@tokens).tree - - token.build(lookbehind, lookahead).tap do |node| + token.build(@nodes.last, tree).tap do |node| @nodes.push(node) end when :value @@ -27,7 +22,11 @@ module Gitlab end end rescue StopIteration - @nodes.last + @nodes.last || Expression::Null.new + end + + def self.seed(statement) + new(Expression::Lexer.new(statement).tokens) end end end diff --git a/lib/gitlab/ci/pipeline/expression/statement.rb b/lib/gitlab/ci/pipeline/expression/statement.rb index 07b84c89899..cb76bc00764 100644 --- a/lib/gitlab/ci/pipeline/expression/statement.rb +++ b/lib/gitlab/ci/pipeline/expression/statement.rb @@ -29,7 +29,7 @@ module Gitlab raise StatementError, 'Unknown pipeline expression!' end - Expression::Parser.new(@lexer.tokens.to_enum).tree + Expression::Parser.new(@lexer.tokens).tree end def evaluate diff --git a/spec/lib/gitlab/ci/pipeline/expression/lexer_spec.rb b/spec/lib/gitlab/ci/pipeline/expression/lexer_spec.rb index 464b74c54d8..0d70ca6c906 100644 --- a/spec/lib/gitlab/ci/pipeline/expression/lexer_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/expression/lexer_spec.rb @@ -67,5 +67,4 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do expect(lexer.lexemes).to eq %w[variable string] end end - end diff --git a/spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb b/spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb index c70bcc8438c..63c3393f07a 100644 --- a/spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb @@ -2,22 +2,25 @@ require 'spec_helper' describe Gitlab::Ci::Pipeline::Expression::Parser do describe '#tree' do - context 'when using an operator' do + context 'when using operators' do it 'returns a reverse descent parse tree' do - expect(described_class.new(tokens('$VAR == "123"')).tree) + expect(described_class.seed('$VAR1 == "123" == $VAR2').tree) .to be_a Gitlab::Ci::Pipeline::Expression::Equals end end context 'when using a single token' do it 'returns a single token instance' do - expect(described_class.new(tokens('$VAR')).tree) + expect(described_class.seed('$VAR').tree) .to be_a Gitlab::Ci::Pipeline::Expression::Variable end end - end - def tokens(statement) - Gitlab::Ci::Pipeline::Expression::Lexer.new(statement).tokens.to_enum + context 'when expression is empty' do + it 'returns a null token' do + expect(described_class.seed('').tree) + .to be_a Gitlab::Ci::Pipeline::Expression::Null + end + end end end