Commit 1926eca0 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add method that checks if pipeline expression is truthy

parent 64e86dab
...@@ -38,6 +38,10 @@ module Gitlab ...@@ -38,6 +38,10 @@ module Gitlab
parse_tree.evaluate(@variables.to_h) parse_tree.evaluate(@variables.to_h)
end end
def truthful?
evaluate.present?
end
def inspect def inspect
"syntax: #{@lexer.lexemes.join(' ')}" "syntax: #{@lexer.lexemes.join(' ')}"
end end
......
...@@ -8,13 +8,16 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -8,13 +8,16 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
end end
before do before do
pipeline.variables.build([key: 'VARIABLE', value: 'my variable']) variables = [{ key: 'PRESENT_VARIABLE', value: 'my variable' },
{ key: 'EMPTY_VARIABLE', value: '' }]
pipeline.variables.build(variables)
end end
describe '.new' do describe '.new' do
context 'when pipeline is not provided' do context 'when pipeline is not provided' do
it 'allows to properly initialize the statement' do it 'allows to properly initialize the statement' do
statement = described_class.new('$VARIABLE') statement = described_class.new('$PRESENT_VARIABLE')
expect(statement.evaluate).to be_nil expect(statement.evaluate).to be_nil
end end
...@@ -72,7 +75,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -72,7 +75,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
end end
context 'when using a single token' do context 'when using a single token' do
let(:text) { '$VARIABLE' } let(:text) { '$PRESENT_VARIABLE' }
it 'returns a single token instance' do it 'returns a single token instance' do
expect(subject.parse_tree) expect(subject.parse_tree)
...@@ -84,14 +87,17 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -84,14 +87,17 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
describe '#evaluate' do describe '#evaluate' do
statements = [ statements = [
['$VARIABLE == "my variable"', true], ['$PRESENT_VARIABLE == "my variable"', true],
["$VARIABLE == 'my variable'", true], ["$PRESENT_VARIABLE == 'my variable'", true],
['"my variable" == $VARIABLE', true], ['"my variable" == $PRESENT_VARIABLE', true],
['$VARIABLE == null', false], ['$PRESENT_VARIABLE == null', false],
['$VAR == null', true], ['$EMPTY_VARIABLE == null', false],
['null == $VAR', true], ['"" == $EMPTY_VARIABLE', true],
['$VARIABLE', 'my variable'], ['$EMPTY_VARIABLE', ''],
['$VAR', nil] ['$UNDEFINED_VARIABLE == null', true],
['null == $UNDEFINED_VARIABLE', true],
['$PRESENT_VARIABLE', 'my variable'],
['$UNDEFINED_VARIABLE', nil]
] ]
statements.each do |expression, value| statements.each do |expression, value|
...@@ -104,4 +110,25 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -104,4 +110,25 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
end end
end end
end end
describe '#truthful?' do
statements = [
['$PRESENT_VARIABLE == "my variable"', true],
["$PRESENT_VARIABLE == 'no match'", false],
['$UNDEFINED_VARIABLE == null', true],
['$PRESENT_VARIABLE', true],
['$UNDEFINED_VARIABLE', false],
['$EMPTY_VARIABLE', false]
]
statements.each do |expression, value|
context "when using expression `#{expression}`" do
let(:text) { expression }
it "returns `#{value.inspect}`" do
expect(subject.truthful?).to eq value
end
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