From 2d4e04b2e5e3fecbae91fa16d4154507a1b4dd1b Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 26 Sep 2014 00:24:19 +0000 Subject: [PATCH] Fail when accessing registry with invalid key --- features/registry.feature | 12 ++++++++++++ lib/producer/core/env.rb | 4 +++- lib/producer/core/errors.rb | 1 + spec/producer/core/env_spec.rb | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/features/registry.feature b/features/registry.feature index 8802624..b11f610 100644 --- a/features/registry.feature +++ b/features/registry.feature @@ -34,3 +34,15 @@ Feature: key/value registry """ When I successfully execute the recipe Then the output must contain "some_value" + + Scenario: `get' keyword should trigger an error when given invalid key + Given a recipe with: + """ + task :some_task do + echo get :no_key + echo 'after_fail' + end + """ + When I execute the recipe + Then the output must not contain "after_fail" + And the output must match /\A\w+Error:\s+:no_key/ diff --git a/lib/producer/core/env.rb b/lib/producer/core/env.rb index 30a64dd..6530760 100644 --- a/lib/producer/core/env.rb +++ b/lib/producer/core/env.rb @@ -18,7 +18,9 @@ module Producer end def [](key) - @registry[key] + @registry.fetch key + rescue KeyError + fail RegistryKeyError, key.inspect end alias get [] diff --git a/lib/producer/core/errors.rb b/lib/producer/core/errors.rb index e3eb8b3..329bc52 100644 --- a/lib/producer/core/errors.rb +++ b/lib/producer/core/errors.rb @@ -4,5 +4,6 @@ module Producer RuntimeError = Class.new(RuntimeError) ConditionNotMetError = Class.new(Error) RemoteCommandExecutionError = Class.new(RuntimeError) + RegistryKeyError = Class.new(RuntimeError) end end diff --git a/spec/producer/core/env_spec.rb b/spec/producer/core/env_spec.rb index f55ab28..d595013 100644 --- a/spec/producer/core/env_spec.rb +++ b/spec/producer/core/env_spec.rb @@ -107,6 +107,10 @@ module Producer::Core it 'returns the value indexed by given key from the registry' do expect(env[:some_key]).to eq :some_value end + + it 'raises an error when given invalid key' do + expect { env[:no_key] }.to raise_error RegistryKeyError + end end describe '#[]=' do