From b11d0b6950a24535475db6e064f7ca72230fa573 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Thu, 25 Sep 2014 14:57:27 +0000 Subject: [PATCH] Allow `target' recipe keyword to return target --- features/recipe_target.feature | 12 +++++++++++- lib/producer/core/recipe.rb | 4 ++-- spec/producer/core/recipe_spec.rb | 7 +++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/features/recipe_target.feature b/features/recipe_target.feature index 07b3452..e17744d 100644 --- a/features/recipe_target.feature +++ b/features/recipe_target.feature @@ -10,4 +10,14 @@ Feature: `target' recipe keyword end """ When I successfully execute the recipe - Then the output must contain exactly "some_host.example\n" + Then the output must contain "some_host.example" + + Scenario: returns current target when no arguments are provided + Given a recipe with: + """ + target 'some_host.example' + + env.output.puts target + """ + When I successfully execute the recipe + Then the output must contain "some_host.example" diff --git a/lib/producer/core/recipe.rb b/lib/producer/core/recipe.rb index 47eb479..35e06a2 100644 --- a/lib/producer/core/recipe.rb +++ b/lib/producer/core/recipe.rb @@ -26,8 +26,8 @@ module Producer instance_eval File.read("./#{filepath}.rb"), "#{filepath}.rb" end - def target(hostname) - env.target ||= hostname + def target(hostname = nil) + if hostname then env.target ||= hostname else env.target end end def task(name, *args, &block) diff --git a/spec/producer/core/recipe_spec.rb b/spec/producer/core/recipe_spec.rb index 0846322..5b50d0a 100644 --- a/spec/producer/core/recipe_spec.rb +++ b/spec/producer/core/recipe_spec.rb @@ -38,6 +38,13 @@ module Producer::Core expect { recipe.target host }.not_to change { env.target } end end + + context 'when no arguments are provided' do + it 'returns current target' do + recipe.target host + expect(recipe.target).to eq host + end + end end describe '#task' do