From d5eff68576361f43e5979bb713ecb823323c24bb Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 17 Aug 2018 12:53:04 +0200 Subject: [PATCH] Improve extended CI/CD config specs and fix a bug --- lib/gitlab/ci/config/extendable/collection.rb | 2 +- lib/gitlab/ci/config/extendable/entry.rb | 9 +++++---- .../gitlab/ci/config/extendable/collection_spec.rb | 2 +- spec/lib/gitlab/ci/config/extendable/entry_spec.rb | 11 ++++++----- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/gitlab/ci/config/extendable/collection.rb b/lib/gitlab/ci/config/extendable/collection.rb index b57b3aa5d47..123219c90f2 100644 --- a/lib/gitlab/ci/config/extendable/collection.rb +++ b/lib/gitlab/ci/config/extendable/collection.rb @@ -10,7 +10,7 @@ module Gitlab CircularDependencyError = Class.new(ExtensionError) def initialize(hash) - @hash = hash.deep_dup + @hash = hash.to_h.deep_dup each { |entry| entry.extend! if entry.extensible? } end diff --git a/lib/gitlab/ci/config/extendable/entry.rb b/lib/gitlab/ci/config/extendable/entry.rb index 5b26faaa806..f14d2c1817c 100644 --- a/lib/gitlab/ci/config/extendable/entry.rb +++ b/lib/gitlab/ci/config/extendable/entry.rb @@ -40,16 +40,17 @@ module Gitlab if unknown_extension? raise Extendable::Collection::InvalidExtensionError, - 'Unknown extension!' + "Unknown extends key in extended `#{key}`!" end if invalid_base? raise Extendable::Collection::InvalidExtensionError, - 'Invalid base hash!' + "Invalid base hash in extended `#{key}`!" end if circular_dependency? - raise Extendable::Collection::CircularDependencyError + raise Extendable::Collection::CircularDependencyError, + "Circular dependency detected in extended `#{key}`!" end @context[key] = base_hash!.deep_merge(value) @@ -62,7 +63,7 @@ module Gitlab end def unknown_extension? - !@context.key?(key) + !@context.key?(extends_key) end def invalid_base? diff --git a/spec/lib/gitlab/ci/config/extendable/collection_spec.rb b/spec/lib/gitlab/ci/config/extendable/collection_spec.rb index 20483800315..a47a49fc03a 100644 --- a/spec/lib/gitlab/ci/config/extendable/collection_spec.rb +++ b/spec/lib/gitlab/ci/config/extendable/collection_spec.rb @@ -179,7 +179,7 @@ describe Gitlab::Ci::Config::Extendable::Collection do end end - context 'when extensible entry has non-hash inheritace defined' do + context 'when extensible entry has non-hash inheritance defined' do let(:hash) do { test: { diff --git a/spec/lib/gitlab/ci/config/extendable/entry_spec.rb b/spec/lib/gitlab/ci/config/extendable/entry_spec.rb index 7cc6e3f01f1..39b3aec3165 100644 --- a/spec/lib/gitlab/ci/config/extendable/entry_spec.rb +++ b/spec/lib/gitlab/ci/config/extendable/entry_spec.rb @@ -115,13 +115,13 @@ describe Gitlab::Ci::Config::Extendable::Entry do let(:hash) do { first: 'my value', - second: { extends: 'first' }, - test: { extends: 'second' } + test: { extends: 'first' } } end it 'raises an error' do - expect { subject.extend! }.to raise_error(StandardError) + expect { subject.extend! } + .to raise_error(StandardError, /Invalid base hash/) end end @@ -131,7 +131,8 @@ describe Gitlab::Ci::Config::Extendable::Entry do end it 'raises an error' do - expect { subject.extend! }.to raise_error(StandardError) + expect { subject.extend! } + .to raise_error(StandardError, /Unknown extends key/) end end @@ -177,7 +178,7 @@ describe Gitlab::Ci::Config::Extendable::Entry do end it 'does not mutate orignal context' do - original = hash.dup + original = hash.deep_dup subject.extend!