diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 033f9f0e3d1e13b49594e3c9c7dba14d3fa75c5b..8fda37a8922ab764ae9ab4e0a63c7e649539baf8 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -14,6 +14,7 @@ module Gitlab def initialize(config) @config = config @nodes = {} + @validator = self.class.validator.new(self) @validator.validate end @@ -71,6 +72,10 @@ module Gitlab true end + def attributes + { key: @key, parent: @parent, description: @description } + end + def self.default end diff --git a/lib/gitlab/ci/config/node/undefined.rb b/lib/gitlab/ci/config/node/undefined.rb index 699605e1e3aef2ceffc324b5ba92463ca1a15135..f152c433c42c6669d3fa78f2f1b30dbdfb24a430 100644 --- a/lib/gitlab/ci/config/node/undefined.rb +++ b/lib/gitlab/ci/config/node/undefined.rb @@ -5,8 +5,9 @@ module Gitlab ## # This class represents an undefined entry node. # - # It takes original entry class as configuration and returns default - # value of original entry as self value. + # It takes original entry class as configuration and creates an object + # if original entry has a default value. If there is default value + # some methods are delegated to it. # # class Undefined < Entry @@ -16,13 +17,35 @@ module Gitlab validates :config, type: Class end + def initialize(node) + super + + unless node.default.nil? + @default = fabricate_default(node) + end + end + def value - @config.default + @default.value if @default + end + + def valid? + @default ? @default.valid? : true + end + + def errors + @default ? @default.errors : [] end def defined? false end + + private + + def fabricate_default(node) + Node::Factory.fabricate(node, node.default, attributes) + end end end end diff --git a/spec/lib/gitlab/ci/config/node/factory_spec.rb b/spec/lib/gitlab/ci/config/node/factory_spec.rb index 5a26bb6df02c4ba2e970304e5c96dd56dc7c7268..5b856d4498940a00269f7117f9587c1598592c7d 100644 --- a/spec/lib/gitlab/ci/config/node/factory_spec.rb +++ b/spec/lib/gitlab/ci/config/node/factory_spec.rb @@ -9,11 +9,13 @@ describe Gitlab::Ci::Config::Node::Factory do it 'fabricates entry with attributes set' do fabricated = described_class .fabricate(entry_class, ['ls'], - parent: factory, key: :test) + parent: true, key: :test) - expect(fabricated.parent).to be factory + expect(fabricated.parent).to be true expect(fabricated.key).to eq :test expect(fabricated.value).to eq ['ls'] + expect(fabricated.attributes) + .to eq(parent: true, key: :test, description: nil) end end diff --git a/spec/lib/gitlab/ci/config/node/undefined_spec.rb b/spec/lib/gitlab/ci/config/node/undefined_spec.rb index 0c6608d906d6e2a59862757a5cf24e1d6cd3eb7f..417b4a0ad6fc16ab139b8d42dd4d9a7feb9aa6cd 100644 --- a/spec/lib/gitlab/ci/config/node/undefined_spec.rb +++ b/spec/lib/gitlab/ci/config/node/undefined_spec.rb @@ -2,33 +2,62 @@ require 'spec_helper' describe Gitlab::Ci::Config::Node::Undefined do let(:undefined) { described_class.new(entry) } - let(:entry) { Class.new } + let(:entry) { spy('Entry') } - describe '#leaf?' do - it 'is leaf node' do - expect(undefined).to be_leaf + context 'when entry does not have a default value' do + before { allow(entry).to receive(:default).and_return(nil) } + + describe '#leaf?' do + it 'is leaf node' do + expect(undefined).to be_leaf + end end - end - describe '#valid?' do - it 'is always valid' do - expect(undefined).to be_valid + describe '#valid?' do + it 'is always valid' do + expect(undefined).to be_valid + end end - end - describe '#errors' do - it 'is does not contain errors' do - expect(undefined.errors).to be_empty + describe '#errors' do + it 'is does not contain errors' do + expect(undefined.errors).to be_empty + end + end + + describe '#value' do + it 'returns nil' do + expect(undefined.value).to eq nil + end end end - describe '#value' do + context 'when entry has a default value' do before do allow(entry).to receive(:default).and_return('some value') + allow(entry).to receive(:value).and_return('some value') end - it 'returns default value for entry' do - expect(undefined.value).to eq 'some value' + describe '#value' do + it 'returns default value for entry' do + expect(undefined.value).to eq 'some value' + end + end + + describe '#errors' do + it 'delegates errors to default entry' do + expect(entry).to receive(:errors) + + undefined.errors + end + end + + describe '#valid?' do + it 'delegates valid? to default entry' do + expect(entry).to receive(:valid?) + + undefined.valid? + end end end