From 87a3ead0822fd390286fe57db8dec4e34b0d38cf Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Sun, 25 May 2014 12:11:38 +0000 Subject: [PATCH] Implement dry run mode --- features/cli/dry_run.feature | 11 +++++++++++ features/cli/usage.feature | 2 +- lib/producer/core/cli.rb | 4 +++- lib/producer/core/env.rb | 7 ++++++- lib/producer/core/worker.rb | 2 +- spec/producer/core/cli_spec.rb | 17 +++++++++++++---- spec/producer/core/env_spec.rb | 12 ++++++++++++ spec/producer/core/worker_spec.rb | 11 ++++++++++- 8 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 features/cli/dry_run.feature diff --git a/features/cli/dry_run.feature b/features/cli/dry_run.feature new file mode 100644 index 0000000..708b296 --- /dev/null +++ b/features/cli/dry_run.feature @@ -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" diff --git a/features/cli/usage.feature b/features/cli/usage.feature index 9da7ac5..2ad288b 100644 --- a/features/cli/usage.feature +++ b/features/cli/usage.feature @@ -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 """ diff --git a/lib/producer/core/cli.rb b/lib/producer/core/cli.rb index 4fddaa6..b7c5653 100644 --- a/lib/producer/core/cli.rb +++ b/lib/producer/core/cli.rb @@ -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 diff --git a/lib/producer/core/env.rb b/lib/producer/core/env.rb index 08044b7..99a7f78 100644 --- a/lib/producer/core/env.rb +++ b/lib/producer/core/env.rb @@ -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 diff --git a/lib/producer/core/worker.rb b/lib/producer/core/worker.rb index eab81b4..d57fe0e 100644 --- a/lib/producer/core/worker.rb +++ b/lib/producer/core/worker.rb @@ -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' diff --git a/spec/producer/core/cli_spec.rb b/spec/producer/core/cli_spec.rb index 11524ae..e5a324a 100644 --- a/spec/producer/core/cli_spec.rb +++ b/spec/producer/core/cli_spec.rb @@ -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 diff --git a/spec/producer/core/env_spec.rb b/spec/producer/core/env_spec.rb index 08b6fb5..fbee8fe 100644 --- a/spec/producer/core/env_spec.rb +++ b/spec/producer/core/env_spec.rb @@ -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 diff --git a/spec/producer/core/worker_spec.rb b/spec/producer/core/worker_spec.rb index afb2021..8ea8aa1 100644 --- a/spec/producer/core/worker_spec.rb +++ b/spec/producer/core/worker_spec.rb @@ -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