summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-02-23 10:13:43 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-02-23 10:13:43 +0100
commit8c7374cafcfe77f880d45f32ff11b38d257f5373 (patch)
treee086920a91b6b8e65f460b7450e664062bfe4644
parent8517627410499486e2bb09d8a55ba6f0088d7503 (diff)
downloadgitlab-ce-8c7374cafcfe77f880d45f32ff11b38d257f5373.tar.gz
Add operator / value types in pipeline expressions
-rw-r--r--lib/gitlab/ci/pipeline/expression/lexeme/equals.rb3
-rw-r--r--lib/gitlab/ci/pipeline/expression/lexeme/null.rb3
-rw-r--r--lib/gitlab/ci/pipeline/expression/lexeme/operator.rb15
-rw-r--r--lib/gitlab/ci/pipeline/expression/lexeme/string.rb3
-rw-r--r--lib/gitlab/ci/pipeline/expression/lexeme/value.rb15
-rw-r--r--lib/gitlab/ci/pipeline/expression/lexeme/variable.rb3
-rw-r--r--lib/gitlab/ci/pipeline/expression/lexer.rb2
-rw-r--r--lib/gitlab/ci/pipeline/expression/parser.rb2
-rw-r--r--spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb19
9 files changed, 50 insertions, 15 deletions
diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb b/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb
index 0cb21131d69..b499d41a509 100644
--- a/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb
+++ b/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb
@@ -3,9 +3,8 @@ module Gitlab
module Pipeline
module Expression
module Lexeme
- class Equals < Lexeme::Base
+ class Equals < Lexeme::Operator
PATTERN = /==/.freeze
- TYPE = :operator
def initialize(left, right)
@left = left
diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/null.rb b/lib/gitlab/ci/pipeline/expression/lexeme/null.rb
index 21286151d79..5bf06a9eee2 100644
--- a/lib/gitlab/ci/pipeline/expression/lexeme/null.rb
+++ b/lib/gitlab/ci/pipeline/expression/lexeme/null.rb
@@ -3,9 +3,8 @@ module Gitlab
module Pipeline
module Expression
module Lexeme
- class Null < Lexeme::Base
+ class Null < Lexeme::Value
PATTERN = /null/.freeze
- TYPE = :value
def initialize(value = nil)
@value = value
diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/operator.rb b/lib/gitlab/ci/pipeline/expression/lexeme/operator.rb
new file mode 100644
index 00000000000..f640d0b5855
--- /dev/null
+++ b/lib/gitlab/ci/pipeline/expression/lexeme/operator.rb
@@ -0,0 +1,15 @@
+module Gitlab
+ module Ci
+ module Pipeline
+ module Expression
+ module Lexeme
+ class Operator < Lexeme::Base
+ def self.type
+ :operator
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/string.rb b/lib/gitlab/ci/pipeline/expression/lexeme/string.rb
index 470a5a15cc1..6f77d443c3c 100644
--- a/lib/gitlab/ci/pipeline/expression/lexeme/string.rb
+++ b/lib/gitlab/ci/pipeline/expression/lexeme/string.rb
@@ -3,9 +3,8 @@ module Gitlab
module Pipeline
module Expression
module Lexeme
- class String < Lexeme::Base
+ class String < Lexeme::Value
PATTERN = /"(?<string>.+?)"/.freeze
- TYPE = :value
def initialize(value)
@value = value
diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/value.rb b/lib/gitlab/ci/pipeline/expression/lexeme/value.rb
new file mode 100644
index 00000000000..f2611d65faf
--- /dev/null
+++ b/lib/gitlab/ci/pipeline/expression/lexeme/value.rb
@@ -0,0 +1,15 @@
+module Gitlab
+ module Ci
+ module Pipeline
+ module Expression
+ module Lexeme
+ class Value < Lexeme::Base
+ def self.type
+ :value
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb b/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb
index 5fb5dd4bbbe..01eea6b19a6 100644
--- a/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb
+++ b/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb
@@ -3,9 +3,8 @@ module Gitlab
module Pipeline
module Expression
module Lexeme
- class Variable < Lexeme::Base
+ class Variable < Lexeme::Value
PATTERN = /\$(?<name>\w+)/.freeze
- TYPE = :value
def initialize(name)
@name = name
diff --git a/lib/gitlab/ci/pipeline/expression/lexer.rb b/lib/gitlab/ci/pipeline/expression/lexer.rb
index 038c0e7e330..a0d1d7cd87d 100644
--- a/lib/gitlab/ci/pipeline/expression/lexer.rb
+++ b/lib/gitlab/ci/pipeline/expression/lexer.rb
@@ -10,7 +10,7 @@ module Gitlab
].freeze
MAX_CYCLES = 5
- SyntaxError = Class.new(StandardError)
+ SyntaxError = Class.new(Statement::StatementError)
def initialize(statement)
@scanner = StringScanner.new(statement)
diff --git a/lib/gitlab/ci/pipeline/expression/parser.rb b/lib/gitlab/ci/pipeline/expression/parser.rb
index b395e4692f1..1f6a3602b31 100644
--- a/lib/gitlab/ci/pipeline/expression/parser.rb
+++ b/lib/gitlab/ci/pipeline/expression/parser.rb
@@ -22,7 +22,7 @@ module Gitlab
end
end
rescue StopIteration
- @nodes.last || Expression::Lexeme::Null.new
+ @nodes.last || Lexeme::Null.new
end
def self.seed(statement)
diff --git a/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb b/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb
index 672ee9d75c0..e8a1fc0579e 100644
--- a/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb
+++ b/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb
@@ -23,11 +23,20 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
end
context 'when expression grammar is incorrect' do
- let(:text) { '$VAR "text"' }
-
- it 'raises an error' do
- expect { subject.parse_tree }
- .to raise_error described_class::StatementError
+ table = [
+ '$VAR "text"', # missing operator
+ '== "123"', # invalid right side
+ "'single quotes'", # single quotes string
+ '$VAR ==', # invalid right side
+ '12345', # unknown syntax
+ '' # empty statement
+ ]
+
+ table.each do |syntax|
+ it "raises an error when syntax is `#{syntax}`" do
+ expect { described_class.new(syntax, pipeline).parse_tree }
+ .to raise_error described_class::StatementError
+ end
end
end