BigW Consortium Gitlab

Commit 5065612a by Grzegorz Bizon

Add minor improvements in new Ci config design

parent 87fe50f2
...@@ -12,22 +12,16 @@ module Gitlab ...@@ -12,22 +12,16 @@ module Gitlab
@nodes = {} @nodes = {}
@errors = [] @errors = []
unless leaf? || value.is_a?(Hash) unless leaf? || has_config?
@errors << 'should be a configuration entry with hash value' @errors << 'should be a configuration entry with hash value'
end end
end end
def process! def process!
return if leaf? || !valid? return if leaf? || invalid?
keys.each do |key, entry_class| keys.each do |key, entry_class|
if @value.has_key?(key) add_node(key, entry_class)
entry = entry_class.new(@value[key], @root, self)
else
entry = Node::Null.new(nil, @root, self)
end
@nodes[key] = entry
end end
nodes.each(&:process!) nodes.each(&:process!)
...@@ -38,22 +32,30 @@ module Gitlab ...@@ -38,22 +32,30 @@ module Gitlab
@nodes.values @nodes.values
end end
def errors
@errors + nodes.map(&:errors).flatten
end
def valid? def valid?
errors.none? errors.none?
end end
def invalid?
!valid?
end
def leaf? def leaf?
keys.none? keys.none?
end end
def has_config?
@value.is_a?(Hash)
end
def keys def keys
self.class.nodes || {} self.class.nodes || {}
end end
def errors
@errors + nodes.map(&:errors).flatten
end
def method_missing(name, *args) def method_missing(name, *args)
super unless keys.has_key?(name) super unless keys.has_key?(name)
raise InvalidError unless valid? raise InvalidError unless valid?
...@@ -73,6 +75,18 @@ module Gitlab ...@@ -73,6 +75,18 @@ module Gitlab
raise NotImplementedError raise NotImplementedError
end end
private
def add_node(key, entry_class)
if @value.has_key?(key)
entry = entry_class.new(@value[key], @root, self)
else
entry = Node::Null.new(nil, @root, self)
end
@nodes[key] = entry
end
class << self class << self
attr_reader :nodes attr_reader :nodes
......
...@@ -8,6 +8,7 @@ module Gitlab ...@@ -8,6 +8,7 @@ module Gitlab
end end
def validate! def validate!
nil
end end
def method_missing(*) def method_missing(*)
......
...@@ -18,6 +18,12 @@ describe Gitlab::Ci::Config::Node::BeforeScript do ...@@ -18,6 +18,12 @@ describe Gitlab::Ci::Config::Node::BeforeScript do
expect(entry.errors).to be_empty expect(entry.errors).to be_empty
end end
end end
describe '#has_config?' do
it 'does not have config' do
expect(entry).not_to have_config
end
end
end end
context 'when entry value is not correct' do context 'when entry value is not correct' do
...@@ -29,5 +35,11 @@ describe Gitlab::Ci::Config::Node::BeforeScript do ...@@ -29,5 +35,11 @@ describe Gitlab::Ci::Config::Node::BeforeScript do
.to include /should be an array of strings/ .to include /should be an array of strings/
end end
end end
describe '#invalid?' do
it 'is not valid' do
expect(entry).to be_invalid
end
end
end end
end end
...@@ -35,6 +35,12 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -35,6 +35,12 @@ describe Gitlab::Ci::Config::Node::Global do
end end
end end
describe '#has_config?' do
it 'has config' do
expect(global).to have_config
end
end
describe '#leaf?' do describe '#leaf?' do
it 'is not leaf' do it 'is not leaf' do
expect(global).not_to be_leaf expect(global).not_to be_leaf
...@@ -59,6 +65,12 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -59,6 +65,12 @@ describe Gitlab::Ci::Config::Node::Global do
end end
end end
describe '#invalid?' do
it 'is not valid' do
expect(global).to be_invalid
end
end
describe '#errors' do describe '#errors' do
it 'reports errors from child nodes' do it 'reports errors from child nodes' do
expect(global.errors) expect(global.errors)
......
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