BigW Consortium Gitlab

Commit 85333aed by Grzegorz Bizon

Add support for single quoted string in pipeline expressions

parent d3076ff0
...@@ -4,7 +4,7 @@ module Gitlab ...@@ -4,7 +4,7 @@ module Gitlab
module Expression module Expression
module Lexeme module Lexeme
class String < Lexeme::Value class String < Lexeme::Value
PATTERN = /"(?<string>.+?)"/.freeze PATTERN = /("(?<string>.+?)")|('(?<string>.+?)')/.freeze
def initialize(value) def initialize(value)
@value = value @value = value
......
...@@ -14,6 +14,68 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::String do ...@@ -14,6 +14,68 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::String do
end end
end end
describe '.scan' do
context 'when using double quotes' do
it 'correctly identifies string token' do
scanner = StringScanner.new('"some string"')
token = described_class.scan(scanner)
expect(token).not_to be_nil
expect(token.build.evaluate).to eq 'some string'
end
end
context 'when using single quotes' do
it 'correctly identifies string token' do
scanner = StringScanner.new("'some string 2'")
token = described_class.scan(scanner)
expect(token).not_to be_nil
expect(token.build.evaluate).to eq 'some string 2'
end
end
context 'when there are mixed quotes in the string' do
it 'is a greedy scanner for double quotes' do
scanner = StringScanner.new('"some string" "and another one"')
token = described_class.scan(scanner)
expect(token).not_to be_nil
expect(token.build.evaluate).to eq 'some string'
end
it 'is a greedy scanner for single quotes' do
scanner = StringScanner.new("'some string' 'and another one'")
token = described_class.scan(scanner)
expect(token).not_to be_nil
expect(token.build.evaluate).to eq 'some string'
end
it 'allows to use single quotes inside double quotes' do
scanner = StringScanner.new(%("some ' string"))
token = described_class.scan(scanner)
expect(token).not_to be_nil
expect(token.build.evaluate).to eq "some ' string"
end
it 'allow to use double quotes inside single quotes' do
scanner = StringScanner.new(%('some " string'))
token = described_class.scan(scanner)
expect(token).not_to be_nil
expect(token.build.evaluate).to eq 'some " string'
end
end
end
describe '#evaluate' do describe '#evaluate' do
it 'returns string value it is is present' do it 'returns string value it is is present' do
string = described_class.new('my string') string = described_class.new('my string')
......
...@@ -64,6 +64,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -64,6 +64,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
describe '#evaluate' do describe '#evaluate' do
statements = [ statements = [
['$VARIABLE == "my variable"', true], ['$VARIABLE == "my variable"', true],
["$VARIABLE == 'my variable'", true],
['"my variable" == $VARIABLE', true], ['"my variable" == $VARIABLE', true],
['$VARIABLE == null', false], ['$VARIABLE == null', false],
['$VAR == null', true], ['$VAR == null', true],
......
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