Implement `-t' CLI option switch to specify target

This commit is contained in:
Thibault Jouan 2014-06-27 17:32:42 +00:00
parent e423a66d5c
commit e6ee3d5f77
6 changed files with 44 additions and 7 deletions

View File

@ -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"

View File

@ -11,7 +11,7 @@ When /^I successfully execute the recipe$/ do
assert_exit_status 0 assert_exit_status 0
end 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 run_simple "producer #{option} recipe.rb", false
assert_exit_status 0 assert_exit_status 0
end end

View File

@ -34,12 +34,14 @@ module Producer
end end
def parse_arguments! def parse_arguments!
@arguments = arguments.inject([]) do |m, e| @arguments = arguments.each_with_index.inject([]) do |m, (e, i)|
case e case e
when '-v' when '-v'
env.verbose = true env.verbose = true
when '-n' when '-n'
env.dry_run = true env.dry_run = true
when '-t'
env.target = arguments.delete_at i + 1
else else
m << e m << e
end end

View File

@ -25,7 +25,7 @@ module Producer
end end
def target(hostname) def target(hostname)
env.target = hostname env.target ||= hostname
end end
def task(name, *args, &block) def task(name, *args, &block)

View File

@ -122,7 +122,7 @@ module Producer::Core
describe '#parse_arguments!' do describe '#parse_arguments!' do
context 'with options' do context 'with options' do
let(:options) { %w[-v] } let(:options) { %w[-v -t some_host.example] }
before { cli.parse_arguments! } before { cli.parse_arguments! }
@ -131,6 +131,8 @@ module Producer::Core
end end
context 'verbose' do context 'verbose' do
let(:options) { %w[-v] }
it 'enables env verbose mode' do it 'enables env verbose mode' do
expect(cli.env).to be_verbose expect(cli.env).to be_verbose
end end
@ -143,6 +145,14 @@ module Producer::Core
expect(cli.env).to be_dry_run expect(cli.env).to be_dry_run
end end
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 end
context 'without arguments' do context 'without arguments' do

View File

@ -65,9 +65,19 @@ module Producer::Core
describe '#target' do describe '#target' do
let(:host) { 'some_host.example' } let(:host) { 'some_host.example' }
it 'registers the target host in the env' do context 'when env has no assigned target' do
dsl.target host it 'registers the target host in the env' do
expect(env.target).to eq host 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
end end