Commit 227e18ea authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add support for evaluating pipeline expression variables

parent 8c7374ca
...@@ -11,7 +11,7 @@ module Gitlab ...@@ -11,7 +11,7 @@ module Gitlab
@right = right @right = right
end end
def evaluate(**variables) def evaluate(variables)
@left.evaluate(variables) == @right.evaluate(variables) @left.evaluate(variables) == @right.evaluate(variables)
end end
......
...@@ -10,9 +10,13 @@ module Gitlab ...@@ -10,9 +10,13 @@ module Gitlab
@value = value @value = value
end end
def evaluate(**_) def evaluate(_)
nil nil
end end
def self.build(value)
new(value)
end
end end
end end
end end
......
...@@ -10,7 +10,7 @@ module Gitlab ...@@ -10,7 +10,7 @@ module Gitlab
@value = value @value = value
end end
def evaluate(**_) def evaluate(_)
@value.to_s @value.to_s
end end
......
...@@ -10,7 +10,8 @@ module Gitlab ...@@ -10,7 +10,8 @@ module Gitlab
@name = name @name = name
end end
def evaluate(**variables) def evaluate(variables)
variables[@name]
end end
def self.build(string) def self.build(string)
......
...@@ -6,6 +6,7 @@ module Gitlab ...@@ -6,6 +6,7 @@ module Gitlab
LEXEMES = [ LEXEMES = [
Expression::Lexeme::Variable, Expression::Lexeme::Variable,
Expression::Lexeme::String, Expression::Lexeme::String,
Expression::Lexeme::Null,
Expression::Lexeme::Equals Expression::Lexeme::Equals
].freeze ].freeze
......
...@@ -18,7 +18,7 @@ module Gitlab ...@@ -18,7 +18,7 @@ module Gitlab
@lexer = Expression::Lexer.new(statement) @lexer = Expression::Lexer.new(statement)
@variables = pipeline.variables.map do |variable| @variables = pipeline.variables.map do |variable|
{ variable.key => variable.value } [variable.key, variable.value]
end end
end end
...@@ -33,7 +33,7 @@ module Gitlab ...@@ -33,7 +33,7 @@ module Gitlab
end end
def evaluate def evaluate
parse_tree.evaluate(**@variables) parse_tree.evaluate(@variables.to_h)
end end
end end
end end
......
...@@ -60,4 +60,24 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -60,4 +60,24 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
end end
end end
end end
describe '#evaluate' do
statements = [
['$VARIABLE == "my variable"', true],
['"my variable" == $VARIABLE', true],
['$VARIABLE == null', false],
['$VAR == null', true],
['null == $VAR', true],
['$VARIABLE', 'my variable'],
['$VAR', nil],
]
statements.each do |expression, value|
it "evaluates `#{expression}` to `#{value}`" do
statement = described_class.new(expression, pipeline)
expect(statement.evaluate).to eq value
end
end
end
end end
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