Implement dry run mode
This commit is contained in:
parent
332bb1a416
commit
87a3ead082
11
features/cli/dry_run.feature
Normal file
11
features/cli/dry_run.feature
Normal 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"
|
@ -5,5 +5,5 @@ Feature: CLI usage
|
||||
Then the exit status must be 64
|
||||
And the output must contain exactly:
|
||||
"""
|
||||
Usage: producer [-v] recipe_file
|
||||
Usage: producer [-v] [-n] recipe_file
|
||||
"""
|
||||
|
@ -3,7 +3,7 @@ module Producer
|
||||
class CLI
|
||||
ArgumentError = Class.new(::ArgumentError)
|
||||
|
||||
USAGE = "Usage: #{File.basename $0} [-v] recipe_file"
|
||||
USAGE = "Usage: #{File.basename $0} [-v] [-n] recipe_file"
|
||||
|
||||
EX_USAGE = 64
|
||||
|
||||
@ -33,6 +33,8 @@ module Producer
|
||||
case e
|
||||
when '-v'
|
||||
env.log_level = Logger::INFO
|
||||
when '-n'
|
||||
env.dry_run = true
|
||||
else
|
||||
m << e
|
||||
end
|
||||
|
@ -2,13 +2,14 @@ module Producer
|
||||
module Core
|
||||
class Env
|
||||
attr_reader :input, :output, :registry, :logger
|
||||
attr_accessor :target
|
||||
attr_accessor :target, :dry_run
|
||||
|
||||
def initialize(input: $stdin, output: $stdout, remote: nil, registry: {})
|
||||
@input = input
|
||||
@output = output
|
||||
@registry = registry
|
||||
@remote = remote
|
||||
@dry_run = false
|
||||
end
|
||||
|
||||
def remote
|
||||
@ -43,6 +44,10 @@ module Producer
|
||||
def log_level=(level)
|
||||
logger.level = level
|
||||
end
|
||||
|
||||
def dry_run?
|
||||
@dry_run
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -17,7 +17,7 @@ module Producer
|
||||
env.log ' condition: met'
|
||||
task.actions.each do |e|
|
||||
env.log " action: #{e} applying"
|
||||
e.apply
|
||||
e.apply unless env.dry_run?
|
||||
end
|
||||
else
|
||||
env.log ' condition: NOT met'
|
||||
|
@ -6,7 +6,8 @@ module Producer::Core
|
||||
include FixturesHelpers
|
||||
|
||||
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 }
|
||||
|
||||
subject(:cli) { CLI.new(arguments, stdout: stdout) }
|
||||
@ -36,7 +37,7 @@ module Producer::Core
|
||||
|
||||
context 'when an argument error is raised' do
|
||||
before do
|
||||
allow(CLI).to receive(:new) { cli }
|
||||
allow(described_class).to receive(:new) { cli }
|
||||
allow(cli).to receive(:parse_arguments!)
|
||||
.and_raise described_class::ArgumentError
|
||||
end
|
||||
@ -60,7 +61,7 @@ module Producer::Core
|
||||
end
|
||||
|
||||
context 'without options' do
|
||||
subject(:cli) { CLI.new(arguments) }
|
||||
subject(:cli) { described_class.new(arguments) }
|
||||
|
||||
it 'assigns $stdout as the default standard output' do
|
||||
expect(cli.stdout).to be $stdout
|
||||
@ -82,7 +83,7 @@ module Producer::Core
|
||||
|
||||
describe '#parse_arguments!' do
|
||||
context 'with options' do
|
||||
let(:arguments) { ['-v', recipe_file] }
|
||||
let(:options) { %w[-v] }
|
||||
|
||||
before { cli.parse_arguments! }
|
||||
|
||||
@ -95,6 +96,14 @@ module Producer::Core
|
||||
expect(cli.env.log_level).to eq Logger::INFO
|
||||
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
|
||||
|
||||
context 'without arguments' do
|
||||
|
@ -18,6 +18,10 @@ module Producer::Core
|
||||
expect(env.registry).to be_empty
|
||||
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
|
||||
subject(:env) { Env.new }
|
||||
|
||||
@ -133,5 +137,13 @@ module Producer::Core
|
||||
expect(env.logger.level).to eq Logger::DEBUG
|
||||
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
|
||||
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
||||
|
||||
module Producer::Core
|
||||
describe Worker do
|
||||
let(:env) { double 'env', log: nil }
|
||||
let(:env) { double 'env', log: nil, dry_run?: false }
|
||||
subject(:worker) { described_class.new(env) }
|
||||
|
||||
describe '#process' do
|
||||
@ -37,6 +37,15 @@ module Producer::Core
|
||||
expect(env).to receive(:log).with /\A action: echo/
|
||||
worker.process_task task
|
||||
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
|
||||
|
||||
context 'when task condition is not met' do
|
||||
|
Loading…
x
Reference in New Issue
Block a user