From e6ee3d5f77d746619e2ffdc5aa25e0d4a0db3e40 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 27 Jun 2014 17:32:42 +0000 Subject: [PATCH] Implement `-t' CLI option switch to specify target --- features/cli_target.feature | 15 +++++++++++++++ features/steps/recipe_steps.rb | 2 +- lib/producer/core/cli.rb | 4 +++- lib/producer/core/recipe/dsl.rb | 2 +- spec/producer/core/cli_spec.rb | 12 +++++++++++- spec/producer/core/recipe/dsl_spec.rb | 16 +++++++++++++--- 6 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 features/cli_target.feature diff --git a/features/cli_target.feature b/features/cli_target.feature new file mode 100644 index 0000000..a3eadf7 --- /dev/null +++ b/features/cli_target.feature @@ -0,0 +1,15 @@ +Feature: CLI target option + + Background: + Given a recipe with: + """ + target 'some_host.example' + + task :some_task do + echo env.target + end + """ + + Scenario: prints tasks name + When I successfully execute the recipe with option -t other_host.example + Then the output must contain exactly "other_host.example\n" diff --git a/features/steps/recipe_steps.rb b/features/steps/recipe_steps.rb index 3c0fb21..ea40502 100644 --- a/features/steps/recipe_steps.rb +++ b/features/steps/recipe_steps.rb @@ -11,7 +11,7 @@ When /^I successfully execute the recipe$/ do assert_exit_status 0 end -When /^I successfully execute the recipe with option (-\w)$/ do |option| +When /^I successfully execute the recipe with option (-.+)$/ do |option| run_simple "producer #{option} recipe.rb", false assert_exit_status 0 end diff --git a/lib/producer/core/cli.rb b/lib/producer/core/cli.rb index 8d25dda..6de1c24 100644 --- a/lib/producer/core/cli.rb +++ b/lib/producer/core/cli.rb @@ -34,12 +34,14 @@ module Producer end def parse_arguments! - @arguments = arguments.inject([]) do |m, e| + @arguments = arguments.each_with_index.inject([]) do |m, (e, i)| case e when '-v' env.verbose = true when '-n' env.dry_run = true + when '-t' + env.target = arguments.delete_at i + 1 else m << e end diff --git a/lib/producer/core/recipe/dsl.rb b/lib/producer/core/recipe/dsl.rb index 6d61b00..bfb8612 100644 --- a/lib/producer/core/recipe/dsl.rb +++ b/lib/producer/core/recipe/dsl.rb @@ -25,7 +25,7 @@ module Producer end def target(hostname) - env.target = hostname + env.target ||= hostname end def task(name, *args, &block) diff --git a/spec/producer/core/cli_spec.rb b/spec/producer/core/cli_spec.rb index 3c160ce..9a2a84c 100644 --- a/spec/producer/core/cli_spec.rb +++ b/spec/producer/core/cli_spec.rb @@ -122,7 +122,7 @@ module Producer::Core describe '#parse_arguments!' do context 'with options' do - let(:options) { %w[-v] } + let(:options) { %w[-v -t some_host.example] } before { cli.parse_arguments! } @@ -131,6 +131,8 @@ module Producer::Core end context 'verbose' do + let(:options) { %w[-v] } + it 'enables env verbose mode' do expect(cli.env).to be_verbose end @@ -143,6 +145,14 @@ module Producer::Core expect(cli.env).to be_dry_run end end + + context 'target' do + let(:options) { %w[-t some_host.example] } + + it 'assigns the given target to the env' do + expect(cli.env.target).to eq 'some_host.example' + end + end end context 'without arguments' do diff --git a/spec/producer/core/recipe/dsl_spec.rb b/spec/producer/core/recipe/dsl_spec.rb index 884fb99..04445b5 100644 --- a/spec/producer/core/recipe/dsl_spec.rb +++ b/spec/producer/core/recipe/dsl_spec.rb @@ -65,9 +65,19 @@ module Producer::Core describe '#target' do let(:host) { 'some_host.example' } - it 'registers the target host in the env' do - dsl.target host - expect(env.target).to eq host + context 'when env has no assigned target' do + it 'registers the target host in the env' do + dsl.target host + expect(env.target).to eq host + end + end + + context 'when env has an assigned target' do + before { env.target = 'already_assigned_host.example' } + + it 'does not change env target' do + expect { dsl.target host }.not_to change { env.target } + end end end