Commit 609cb3e0 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Simplify classes and exceptions of extendable config

parent afbe5490
......@@ -7,13 +7,13 @@ module Gitlab
ConfigError = Class.new(StandardError)
def initialize(config, opts = {})
@config = Extendable::Collection
@config = Config::Extendable
.new(build_config(config, opts))
.to_hash
@global = Entry::Global.new(@config)
@global.compose!
rescue Loader::FormatError, Extendable::Collection::ExtensionError => e
rescue Loader::FormatError, Extendable::ExtensionError => e
raise Config::ConfigError, e.message
end
......
module Gitlab
module Ci
class Config
class Extendable
include Enumerable
ExtensionError = Class.new(StandardError)
def initialize(hash)
@hash = hash.to_h.deep_dup
each { |entry| entry.extend! if entry.extensible? }
end
def each
@hash.each_key do |key|
yield Extendable::Entry.new(key, @hash)
end
end
def to_hash
@hash.to_h
end
end
end
end
end
module Gitlab
module Ci
class Config
module Extendable
class Collection
include Enumerable
ExtensionError = Class.new(StandardError)
InvalidExtensionError = Class.new(ExtensionError)
CircularDependencyError = Class.new(ExtensionError)
NestingTooDeepError = Class.new(ExtensionError)
def initialize(hash)
@hash = hash.to_h.deep_dup
each { |entry| entry.extend! if entry.extensible? }
end
def each
@hash.each_key do |key|
yield Extendable::Entry.new(key, @hash)
end
end
def to_hash
@hash.to_h
end
end
end
end
end
end
module Gitlab
module Ci
class Config
module Extendable
class Extendable
class Entry
MAX_NESTING_LEVELS = 10
InvalidExtensionError = Class.new(Extendable::ExtensionError)
CircularDependencyError = Class.new(Extendable::ExtensionError)
NestingTooDeepError = Class.new(Extendable::ExtensionError)
attr_reader :key
def initialize(key, context, parent = nil)
......@@ -43,22 +47,22 @@ module Gitlab
return value unless extensible?
if unknown_extension?
raise Extendable::Collection::InvalidExtensionError,
raise Entry::InvalidExtensionError,
"Unknown extends key in extended `#{key}`!"
end
if invalid_base?
raise Extendable::Collection::InvalidExtensionError,
raise Entry::InvalidExtensionError,
"Invalid base hash in extended `#{key}`!"
end
if nesting_too_deep?
raise Extendable::Collection::NestingTooDeepError,
raise Entry::NestingTooDeepError,
"`extends` nesting too deep in `#{key}`!"
end
if circular_dependency?
raise Extendable::Collection::CircularDependencyError,
raise Entry::CircularDependencyError,
"Circular dependency detected in extended `#{key}`!"
end
......
......@@ -198,7 +198,8 @@ describe Gitlab::Ci::Config::Extendable::Entry do
it 'raises an error' do
expect { subject.extend! }
.to raise_error(StandardError, /Circular dependency detected/)
.to raise_error(described_class::CircularDependencyError,
/Circular dependency detected/)
end
end
......@@ -217,7 +218,7 @@ describe Gitlab::Ci::Config::Extendable::Entry do
it 'raises an error' do
expect { subject.extend! }
.to raise_error(Gitlab::Ci::Config::Extendable::Collection::NestingTooDeepError)
.to raise_error(described_class::NestingTooDeepError)
end
end
end
......
require 'fast_spec_helper'
describe Gitlab::Ci::Config::Extendable::Collection do
describe Gitlab::Ci::Config::Extendable do
subject { described_class.new(hash) }
describe '#each' do
......@@ -147,7 +147,7 @@ describe Gitlab::Ci::Config::Extendable::Collection do
it 'raises an error about circular dependency' do
expect { subject.to_hash }
.to raise_error(described_class::CircularDependencyError)
.to raise_error(described_class::Entry::CircularDependencyError)
end
end
......@@ -164,7 +164,7 @@ describe Gitlab::Ci::Config::Extendable::Collection do
it 'raises an error about circular dependency' do
expect { subject.to_hash }
.to raise_error(described_class::CircularDependencyError)
.to raise_error(described_class::Entry::CircularDependencyError)
end
end
......@@ -175,7 +175,7 @@ describe Gitlab::Ci::Config::Extendable::Collection do
it 'raises an error about invalid extension' do
expect { subject.to_hash }
.to raise_error(described_class::InvalidExtensionError)
.to raise_error(described_class::Entry::InvalidExtensionError)
end
end
......@@ -194,7 +194,7 @@ describe Gitlab::Ci::Config::Extendable::Collection do
it 'raises an error about invalid base' do
expect { subject.to_hash }
.to raise_error(described_class::InvalidExtensionError)
.to raise_error(described_class::Entry::InvalidExtensionError)
end
end
end
......
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