From 886988c9e1ab6020da9636b935522fac55a19c1e Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 27 Feb 2018 12:36:30 +0100 Subject: Do not use keyword args to pass expression variables --- lib/gitlab/ci/pipeline/expression/lexeme/equals.rb | 2 +- lib/gitlab/ci/pipeline/expression/lexeme/null.rb | 2 +- lib/gitlab/ci/pipeline/expression/lexeme/string.rb | 2 +- lib/gitlab/ci/pipeline/expression/lexeme/variable.rb | 4 ++-- lib/gitlab/ci/pipeline/expression/statement.rb | 2 +- spec/lib/gitlab/ci/pipeline/expression/lexeme/variable_spec.rb | 7 +++++++ 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb b/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb index b499d41a509..3a2f0c6924e 100644 --- a/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb +++ b/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb @@ -11,7 +11,7 @@ module Gitlab @right = right end - def evaluate(**variables) + def evaluate(variables = {}) @left.evaluate(variables) == @right.evaluate(variables) end diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/null.rb b/lib/gitlab/ci/pipeline/expression/lexeme/null.rb index ee627d7066e..a2778716924 100644 --- a/lib/gitlab/ci/pipeline/expression/lexeme/null.rb +++ b/lib/gitlab/ci/pipeline/expression/lexeme/null.rb @@ -10,7 +10,7 @@ module Gitlab @value = nil end - def evaluate(**_) + def evaluate(variables = {}) nil end diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/string.rb b/lib/gitlab/ci/pipeline/expression/lexeme/string.rb index 486bfaf9017..48bde213d44 100644 --- a/lib/gitlab/ci/pipeline/expression/lexeme/string.rb +++ b/lib/gitlab/ci/pipeline/expression/lexeme/string.rb @@ -10,7 +10,7 @@ module Gitlab @value = value end - def evaluate(**_) + def evaluate(variables = {}) @value.to_s end diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb b/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb index ad002447b88..b781c15fd67 100644 --- a/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb +++ b/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb @@ -10,8 +10,8 @@ module Gitlab @name = name end - def evaluate(**variables) - variables[@name.to_sym] + def evaluate(variables = {}) + HashWithIndifferentAccess.new(variables).fetch(@name, nil) end def self.build(string) diff --git a/lib/gitlab/ci/pipeline/expression/statement.rb b/lib/gitlab/ci/pipeline/expression/statement.rb index 08e662eccf9..4f0e101b730 100644 --- a/lib/gitlab/ci/pipeline/expression/statement.rb +++ b/lib/gitlab/ci/pipeline/expression/statement.rb @@ -18,7 +18,7 @@ module Gitlab @lexer = Expression::Lexer.new(statement) @variables = pipeline.variables.map do |variable| - [variable.key.to_sym, variable.value] + [variable.key, variable.value] end end diff --git a/spec/lib/gitlab/ci/pipeline/expression/lexeme/variable_spec.rb b/spec/lib/gitlab/ci/pipeline/expression/lexeme/variable_spec.rb index 91f59669b02..599a5411881 100644 --- a/spec/lib/gitlab/ci/pipeline/expression/lexeme/variable_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/expression/lexeme/variable_spec.rb @@ -22,6 +22,13 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Variable do .to eq 'my variable' end + it 'allows to use a string as a variable key too' do + variable = described_class.new('VARIABLE') + + expect(variable.evaluate('VARIABLE' => 'my variable')) + .to eq 'my variable' + end + it 'returns nil if it is not defined' do variable = described_class.new('VARIABLE') -- cgit v1.2.1