Commit b0ae0d73 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Use only node factory to create CI config entries

parent 6f02da2c
......@@ -21,25 +21,29 @@ module Gitlab
def create!
raise InvalidFactory unless @attributes.has_key?(:value)
fabricate.tap do |entry|
entry.key = @attributes[:key]
entry.parent = @attributes[:parent]
entry.description = @attributes[:description]
end
end
private
def fabricate
##
# We assume that unspecified entry is undefined.
# See issue #18775.
#
if @attributes[:value].nil?
Node::Undefined.new(@node)
fabricate(Node::Undefined, @node)
else
@node.new(@attributes[:value])
fabricate(@node, @attributes[:value])
end
end
def self.fabricate(node, value, **attributes)
node.new(value).tap do |entry|
entry.key = attributes[:key]
entry.parent = attributes[:parent]
entry.description = attributes[:description]
end
end
private
def fabricate(node, value)
self.class.fabricate(node, value, @attributes)
end
end
end
......
......@@ -42,8 +42,8 @@ module Gitlab
def initialize(config)
return super unless config.is_a?(Hash)
jobs = config.except(*self.class.nodes.keys)
global = config.slice(*self.class.nodes.keys)
jobs = config.except(*nodes.keys)
global = config.slice(*nodes.keys)
super(global.merge(jobs: jobs))
end
......
......@@ -33,20 +33,14 @@ module Gitlab
private
def create_node(key, essence)
fabricate_job(key, essence).tap do |job|
job.key = key
job.parent = self
job.description = "#{key} job definition."
end
end
def create_node(key, value)
node = key.to_s.start_with?('.') ? Node::HiddenJob : Node::Job
def fabricate_job(key, essence)
if key.to_s.start_with?('.')
Node::HiddenJob.new(essence)
else
Node::Job.new(essence)
end
attributes = { key: key,
parent: self,
description: "#{key} job definition." }
Node::Factory.fabricate(node, value, attributes)
end
end
end
......
......@@ -5,6 +5,18 @@ describe Gitlab::Ci::Config::Node::Factory do
let(:factory) { described_class.new(entry_class) }
let(:entry_class) { Gitlab::Ci::Config::Node::Script }
describe '.fabricate' do
it 'fabricates entry with attributes set' do
fabricated = described_class
.fabricate(entry_class, ['ls'],
parent: factory, key: :test)
expect(fabricated.parent).to be factory
expect(fabricated.key).to eq :test
expect(fabricated.value).to eq ['ls']
end
end
context 'when setting up a value' do
it 'creates entry with valid value' do
entry = factory
......
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