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