Print a warning when in dry run mode
This commit is contained in:
parent
aac5f603e3
commit
1188b2008f
@ -1,11 +1,17 @@
|
||||
Feature: CLI dry run option
|
||||
|
||||
Scenario: perfoms a trial run without applying actions
|
||||
Background:
|
||||
Given a recipe with:
|
||||
"""
|
||||
task :say_hello do
|
||||
echo 'hello'
|
||||
end
|
||||
"""
|
||||
|
||||
Scenario: perfoms a trial run without applying actions
|
||||
When I successfully execute the recipe with option -n
|
||||
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/
|
||||
|
@ -27,14 +27,14 @@ module Producer
|
||||
def logger
|
||||
@logger ||= begin
|
||||
logger = Logger.new(output)
|
||||
logger.level = verbose? ? Logger::INFO : Logger::ERROR
|
||||
logger.level = verbose? ? Logger::INFO : Logger::WARN
|
||||
logger.formatter = LoggerFormatter.new
|
||||
logger
|
||||
end
|
||||
end
|
||||
|
||||
def log(message)
|
||||
logger.info message
|
||||
def log(message, severity = :info)
|
||||
logger.send severity, message
|
||||
end
|
||||
|
||||
def verbose?
|
||||
|
@ -2,7 +2,14 @@ module Producer
|
||||
module Core
|
||||
class LoggerFormatter < Logger::Formatter
|
||||
def call(severity, datetime, progname, message)
|
||||
message + "\n"
|
||||
prefix(severity) + message + "\n"
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def prefix(severity)
|
||||
severity == 'WARN' ? 'Warning: ' : ''
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,6 +1,9 @@
|
||||
module Producer
|
||||
module Core
|
||||
class Worker
|
||||
DRY_RUN_WARNING =
|
||||
'running in dry run mode, actions will NOT be applied'.freeze
|
||||
|
||||
attr_accessor :env
|
||||
|
||||
def initialize(env)
|
||||
@ -8,6 +11,8 @@ module Producer
|
||||
end
|
||||
|
||||
def process(tasks)
|
||||
env.log DRY_RUN_WARNING, :warn if env.dry_run?
|
||||
|
||||
tasks.each { |t| process_task t }
|
||||
end
|
||||
|
||||
|
@ -113,8 +113,8 @@ module Producer::Core
|
||||
expect(output.string).to include 'some message'
|
||||
end
|
||||
|
||||
it 'has a log level of ERROR' do
|
||||
expect(env.logger.level).to eq Logger::ERROR
|
||||
it 'has a log level of WARN' do
|
||||
expect(env.logger.level).to eq Logger::WARN
|
||||
end
|
||||
|
||||
it 'uses our formatter' do
|
||||
@ -135,6 +135,13 @@ module Producer::Core
|
||||
expect(env.logger).to receive(:info).with 'message'
|
||||
env.log 'message'
|
||||
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
|
||||
|
||||
describe '#verbose?' do
|
||||
|
@ -13,6 +13,14 @@ module Producer::Core
|
||||
it 'returns the given message with a line separator' do
|
||||
expect(subject).to eq "#{message}\n"
|
||||
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
|
||||
|
@ -10,6 +10,18 @@ module Producer::Core
|
||||
expect(worker).to receive(:process_task).with(:some_task)
|
||||
worker.process [:some_task]
|
||||
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
|
||||
|
||||
describe '#process_task' do
|
||||
|
Loading…
x
Reference in New Issue
Block a user