Implement dry run mode

This commit is contained in:
Thibault Jouan 2014-05-25 12:11:38 +00:00
parent 332bb1a416
commit 87a3ead082
8 changed files with 57 additions and 9 deletions

View File

@ -0,0 +1,11 @@
Feature: CLI dry run option
Scenario: perfoms a trial run without applying actions
Given a recipe with:
"""
task :say_hello do
echo 'hello'
end
"""
When I successfully execute the recipe with option -n
Then the output must not contain "hello"

View File

@ -5,5 +5,5 @@ Feature: CLI usage
Then the exit status must be 64 Then the exit status must be 64
And the output must contain exactly: And the output must contain exactly:
""" """
Usage: producer [-v] recipe_file Usage: producer [-v] [-n] recipe_file
""" """

View File

@ -3,7 +3,7 @@ module Producer
class CLI class CLI
ArgumentError = Class.new(::ArgumentError) ArgumentError = Class.new(::ArgumentError)
USAGE = "Usage: #{File.basename $0} [-v] recipe_file" USAGE = "Usage: #{File.basename $0} [-v] [-n] recipe_file"
EX_USAGE = 64 EX_USAGE = 64
@ -33,6 +33,8 @@ module Producer
case e case e
when '-v' when '-v'
env.log_level = Logger::INFO env.log_level = Logger::INFO
when '-n'
env.dry_run = true
else else
m << e m << e
end end

View File

@ -2,13 +2,14 @@ module Producer
module Core module Core
class Env class Env
attr_reader :input, :output, :registry, :logger attr_reader :input, :output, :registry, :logger
attr_accessor :target attr_accessor :target, :dry_run
def initialize(input: $stdin, output: $stdout, remote: nil, registry: {}) def initialize(input: $stdin, output: $stdout, remote: nil, registry: {})
@input = input @input = input
@output = output @output = output
@registry = registry @registry = registry
@remote = remote @remote = remote
@dry_run = false
end end
def remote def remote
@ -43,6 +44,10 @@ module Producer
def log_level=(level) def log_level=(level)
logger.level = level logger.level = level
end end
def dry_run?
@dry_run
end
end end
end end
end end

View File

@ -17,7 +17,7 @@ module Producer
env.log ' condition: met' env.log ' condition: met'
task.actions.each do |e| task.actions.each do |e|
env.log " action: #{e} applying" env.log " action: #{e} applying"
e.apply e.apply unless env.dry_run?
end end
else else
env.log ' condition: NOT met' env.log ' condition: NOT met'

View File

@ -6,7 +6,8 @@ module Producer::Core
include FixturesHelpers include FixturesHelpers
let(:recipe_file) { fixture_path_for 'recipes/some_recipe.rb' } let(:recipe_file) { fixture_path_for 'recipes/some_recipe.rb' }
let(:arguments) { [recipe_file] } let(:options) { [] }
let(:arguments) { [*options, recipe_file] }
let(:stdout) { StringIO.new } let(:stdout) { StringIO.new }
subject(:cli) { CLI.new(arguments, stdout: stdout) } subject(:cli) { CLI.new(arguments, stdout: stdout) }
@ -36,7 +37,7 @@ module Producer::Core
context 'when an argument error is raised' do context 'when an argument error is raised' do
before do before do
allow(CLI).to receive(:new) { cli } allow(described_class).to receive(:new) { cli }
allow(cli).to receive(:parse_arguments!) allow(cli).to receive(:parse_arguments!)
.and_raise described_class::ArgumentError .and_raise described_class::ArgumentError
end end
@ -60,7 +61,7 @@ module Producer::Core
end end
context 'without options' do context 'without options' do
subject(:cli) { CLI.new(arguments) } subject(:cli) { described_class.new(arguments) }
it 'assigns $stdout as the default standard output' do it 'assigns $stdout as the default standard output' do
expect(cli.stdout).to be $stdout expect(cli.stdout).to be $stdout
@ -82,7 +83,7 @@ module Producer::Core
describe '#parse_arguments!' do describe '#parse_arguments!' do
context 'with options' do context 'with options' do
let(:arguments) { ['-v', recipe_file] } let(:options) { %w[-v] }
before { cli.parse_arguments! } before { cli.parse_arguments! }
@ -95,6 +96,14 @@ module Producer::Core
expect(cli.env.log_level).to eq Logger::INFO expect(cli.env.log_level).to eq Logger::INFO
end end
end end
context 'dry run' do
let(:options) { %w[-n] }
it 'enables env dry run' do
expect(cli.env).to be_dry_run
end
end
end end
context 'without arguments' do context 'without arguments' do

View File

@ -18,6 +18,10 @@ module Producer::Core
expect(env.registry).to be_empty expect(env.registry).to be_empty
end end
it 'assigns dry run as false' do
expect(env.dry_run).to be false
end
context 'when output is not given as argument' do context 'when output is not given as argument' do
subject(:env) { Env.new } subject(:env) { Env.new }
@ -133,5 +137,13 @@ module Producer::Core
expect(env.logger.level).to eq Logger::DEBUG expect(env.logger.level).to eq Logger::DEBUG
end end
end end
describe '#dry_run?' do
before { env.dry_run = true }
it 'returns true when dry run is enabled' do
expect(env.dry_run?).to be true
end
end
end end
end end

View File

@ -2,7 +2,7 @@ require 'spec_helper'
module Producer::Core module Producer::Core
describe Worker do describe Worker do
let(:env) { double 'env', log: nil } let(:env) { double 'env', log: nil, dry_run?: false }
subject(:worker) { described_class.new(env) } subject(:worker) { described_class.new(env) }
describe '#process' do describe '#process' do
@ -37,6 +37,15 @@ module Producer::Core
expect(env).to receive(:log).with /\A action: echo/ expect(env).to receive(:log).with /\A action: echo/
worker.process_task task worker.process_task task
end end
context 'when dry run is enabled' do
before { allow(env).to receive(:dry_run?) { true } }
it 'does not apply the actions' do
expect(action).not_to receive :apply
worker.process_task task
end
end
end end
context 'when task condition is not met' do context 'when task condition is not met' do