Print a warning when in dry run mode

This commit is contained in:
Thibault Jouan 2014-07-01 18:15:55 +00:00
parent aac5f603e3
commit 1188b2008f
7 changed files with 52 additions and 7 deletions

View File

@ -1,11 +1,17 @@
Feature: CLI dry run option Feature: CLI dry run option
Scenario: perfoms a trial run without applying actions Background:
Given a recipe with: Given a recipe with:
""" """
task :say_hello do task :say_hello do
echo 'hello' echo 'hello'
end end
""" """
Scenario: perfoms a trial run without applying actions
When I successfully execute the recipe with option -n When I successfully execute the recipe with option -n
Then the output must not contain "hello" Then the output must not contain "hello"
Scenario: prints a warning before any output
When I successfully execute the recipe with option -n
Then the output must match /\AWarning: running in dry run mode, actions will NOT be applied/

View File

@ -27,14 +27,14 @@ module Producer
def logger def logger
@logger ||= begin @logger ||= begin
logger = Logger.new(output) logger = Logger.new(output)
logger.level = verbose? ? Logger::INFO : Logger::ERROR logger.level = verbose? ? Logger::INFO : Logger::WARN
logger.formatter = LoggerFormatter.new logger.formatter = LoggerFormatter.new
logger logger
end end
end end
def log(message) def log(message, severity = :info)
logger.info message logger.send severity, message
end end
def verbose? def verbose?

View File

@ -2,7 +2,14 @@ module Producer
module Core module Core
class LoggerFormatter < Logger::Formatter class LoggerFormatter < Logger::Formatter
def call(severity, datetime, progname, message) def call(severity, datetime, progname, message)
message + "\n" prefix(severity) + message + "\n"
end
private
def prefix(severity)
severity == 'WARN' ? 'Warning: ' : ''
end end
end end
end end

View File

@ -1,6 +1,9 @@
module Producer module Producer
module Core module Core
class Worker class Worker
DRY_RUN_WARNING =
'running in dry run mode, actions will NOT be applied'.freeze
attr_accessor :env attr_accessor :env
def initialize(env) def initialize(env)
@ -8,6 +11,8 @@ module Producer
end end
def process(tasks) def process(tasks)
env.log DRY_RUN_WARNING, :warn if env.dry_run?
tasks.each { |t| process_task t } tasks.each { |t| process_task t }
end end

View File

@ -113,8 +113,8 @@ module Producer::Core
expect(output.string).to include 'some message' expect(output.string).to include 'some message'
end end
it 'has a log level of ERROR' do it 'has a log level of WARN' do
expect(env.logger.level).to eq Logger::ERROR expect(env.logger.level).to eq Logger::WARN
end end
it 'uses our formatter' do it 'uses our formatter' do
@ -135,6 +135,13 @@ module Producer::Core
expect(env.logger).to receive(:info).with 'message' expect(env.logger).to receive(:info).with 'message'
env.log 'message' env.log 'message'
end end
context 'when second argument is :warn' do
it 'logs a warning message through the assigned logger' do
expect(env.logger).to receive(:warn).with 'message'
env.log 'message', :warn
end
end
end end
describe '#verbose?' do describe '#verbose?' do

View File

@ -13,6 +13,14 @@ module Producer::Core
it 'returns the given message with a line separator' do it 'returns the given message with a line separator' do
expect(subject).to eq "#{message}\n" expect(subject).to eq "#{message}\n"
end end
context 'when severity is WARN' do
let(:severity) { 'WARN' }
it 'prefix the message with `Warning:\'' do
expect(subject).to match /\AWarning: #{message}/
end
end
end end
end end
end end

View File

@ -10,6 +10,18 @@ module Producer::Core
expect(worker).to receive(:process_task).with(:some_task) expect(worker).to receive(:process_task).with(:some_task)
worker.process [:some_task] worker.process [:some_task]
end end
context 'when dry run is enabled' do
before { allow(env).to receive(:dry_run?) { true } }
it 'warns dry run is enabled' do
expect(env).to receive(:log).with(
/\Arunning in dry run mode, actions will NOT be applied\z/,
:warn
)
worker.process []
end
end
end end
describe '#process_task' do describe '#process_task' do