Implement verbose mode
This commit is contained in:
parent
2b86bbf112
commit
8291f1bcfd
@ -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 recipe_file
|
Usage: producer [-v] recipe_file
|
||||||
"""
|
"""
|
||||||
|
33
features/cli/verbose.feature
Normal file
33
features/cli/verbose.feature
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
Feature: CLI verbose option
|
||||||
|
|
||||||
|
Scenario: prints tasks name
|
||||||
|
Given a recipe with:
|
||||||
|
"""
|
||||||
|
task :say_hello do
|
||||||
|
end
|
||||||
|
"""
|
||||||
|
When I successfully execute the recipe with option -v
|
||||||
|
Then the output must match /Task:.+say_hello/
|
||||||
|
|
||||||
|
Scenario: prints whether condition is met
|
||||||
|
Given a recipe with:
|
||||||
|
"""
|
||||||
|
task :task_ok do
|
||||||
|
condition { true }
|
||||||
|
end
|
||||||
|
task :task_ko do
|
||||||
|
condition { false }
|
||||||
|
end
|
||||||
|
"""
|
||||||
|
When I successfully execute the recipe with option -v
|
||||||
|
Then the output must match /task_ok.+ condition: met.*task_ko.* condition: NOT met/
|
||||||
|
|
||||||
|
Scenario: prints actions info
|
||||||
|
Given a recipe with:
|
||||||
|
"""
|
||||||
|
task :say_hello do
|
||||||
|
echo 'hello message'
|
||||||
|
end
|
||||||
|
"""
|
||||||
|
When I successfully execute the recipe with option -v
|
||||||
|
Then the output must match /say_hello.+ action: echo/
|
@ -11,6 +11,11 @@ When /^I successfully execute the recipe$/ do
|
|||||||
assert_exit_status 0
|
assert_exit_status 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
When /^I successfully execute the recipe with option (-\w)$/ do |option|
|
||||||
|
run_simple "producer #{option} recipe.rb", false
|
||||||
|
assert_exit_status 0
|
||||||
|
end
|
||||||
|
|
||||||
When /^I execute the recipe interactively$/ do
|
When /^I execute the recipe interactively$/ do
|
||||||
run_interactive 'producer recipe.rb'
|
run_interactive 'producer recipe.rb'
|
||||||
end
|
end
|
||||||
|
@ -11,6 +11,14 @@ module Producer
|
|||||||
@env = env
|
@env = env
|
||||||
@arguments = args
|
@arguments = args
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def name
|
||||||
|
self.class.name.split('::').last.downcase
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,6 +2,10 @@ module Producer
|
|||||||
module Core
|
module Core
|
||||||
module Actions
|
module Actions
|
||||||
class Echo < Action
|
class Echo < Action
|
||||||
|
def name
|
||||||
|
'echo'
|
||||||
|
end
|
||||||
|
|
||||||
def apply
|
def apply
|
||||||
output.puts arguments.first
|
output.puts arguments.first
|
||||||
end
|
end
|
||||||
|
@ -2,6 +2,10 @@ module Producer
|
|||||||
module Core
|
module Core
|
||||||
module Actions
|
module Actions
|
||||||
class FileAppend < Action
|
class FileAppend < Action
|
||||||
|
def name
|
||||||
|
'file_append'
|
||||||
|
end
|
||||||
|
|
||||||
def apply
|
def apply
|
||||||
fs.file_write path, combined_content
|
fs.file_write path, combined_content
|
||||||
end
|
end
|
||||||
|
@ -2,6 +2,10 @@ module Producer
|
|||||||
module Core
|
module Core
|
||||||
module Actions
|
module Actions
|
||||||
class FileReplaceContent < Action
|
class FileReplaceContent < Action
|
||||||
|
def name
|
||||||
|
'file_replace_content'
|
||||||
|
end
|
||||||
|
|
||||||
def apply
|
def apply
|
||||||
fs.file_write path, replaced_content
|
fs.file_write path, replaced_content
|
||||||
end
|
end
|
||||||
|
@ -2,6 +2,10 @@ module Producer
|
|||||||
module Core
|
module Core
|
||||||
module Actions
|
module Actions
|
||||||
class FileWriter < Action
|
class FileWriter < Action
|
||||||
|
def name
|
||||||
|
'file_write'
|
||||||
|
end
|
||||||
|
|
||||||
def apply
|
def apply
|
||||||
case arguments.size
|
case arguments.size
|
||||||
when 2
|
when 2
|
||||||
|
@ -2,6 +2,10 @@ module Producer
|
|||||||
module Core
|
module Core
|
||||||
module Actions
|
module Actions
|
||||||
class Mkdir < Action
|
class Mkdir < Action
|
||||||
|
def name
|
||||||
|
'mkdir'
|
||||||
|
end
|
||||||
|
|
||||||
def apply
|
def apply
|
||||||
case arguments.size
|
case arguments.size
|
||||||
when 1
|
when 1
|
||||||
|
@ -2,6 +2,10 @@ module Producer
|
|||||||
module Core
|
module Core
|
||||||
module Actions
|
module Actions
|
||||||
class ShellCommand < Action
|
class ShellCommand < Action
|
||||||
|
def name
|
||||||
|
'sh'
|
||||||
|
end
|
||||||
|
|
||||||
def apply
|
def apply
|
||||||
remote.execute(arguments.first, output)
|
remote.execute(arguments.first, output)
|
||||||
end
|
end
|
||||||
|
@ -3,7 +3,7 @@ module Producer
|
|||||||
class CLI
|
class CLI
|
||||||
ArgumentError = Class.new(::ArgumentError)
|
ArgumentError = Class.new(::ArgumentError)
|
||||||
|
|
||||||
USAGE = "Usage: #{File.basename $0} recipe_file"
|
USAGE = "Usage: #{File.basename $0} [-v] recipe_file"
|
||||||
|
|
||||||
EX_USAGE = 64
|
EX_USAGE = 64
|
||||||
|
|
||||||
@ -11,6 +11,7 @@ module Producer
|
|||||||
def run!(arguments, output: $stderr)
|
def run!(arguments, output: $stderr)
|
||||||
begin
|
begin
|
||||||
cli = new(arguments)
|
cli = new(arguments)
|
||||||
|
cli.parse_arguments!
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
output.puts USAGE
|
output.puts USAGE
|
||||||
exit EX_USAGE
|
exit EX_USAGE
|
||||||
@ -25,6 +26,19 @@ module Producer
|
|||||||
@arguments = args
|
@arguments = args
|
||||||
@stdout = stdout
|
@stdout = stdout
|
||||||
@env = Env.new(output: stdout)
|
@env = Env.new(output: stdout)
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_arguments!
|
||||||
|
@arguments = arguments.inject([]) do |m, e|
|
||||||
|
case e
|
||||||
|
when '-v'
|
||||||
|
env.log_level = Logger::INFO
|
||||||
|
else
|
||||||
|
m << e
|
||||||
|
end
|
||||||
|
m
|
||||||
|
end
|
||||||
|
|
||||||
raise ArgumentError unless arguments.any?
|
raise ArgumentError unless arguments.any?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -12,7 +12,16 @@ module Producer
|
|||||||
end
|
end
|
||||||
|
|
||||||
def process_task(task)
|
def process_task(task)
|
||||||
task.actions.each(&:apply) if task.condition_met?
|
env.log "Task: #{task} applying"
|
||||||
|
if task.condition_met?
|
||||||
|
env.log ' condition: met'
|
||||||
|
task.actions.each do |e|
|
||||||
|
env.log " action: #{e} applying"
|
||||||
|
e.apply
|
||||||
|
end
|
||||||
|
else
|
||||||
|
env.log ' condition: NOT met'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,5 +3,13 @@ require 'spec_helper'
|
|||||||
module Producer::Core
|
module Producer::Core
|
||||||
describe Action do
|
describe Action do
|
||||||
it_behaves_like 'action'
|
it_behaves_like 'action'
|
||||||
|
|
||||||
|
describe '#name' do
|
||||||
|
subject(:action) { described_class.new(double 'env') }
|
||||||
|
|
||||||
|
it 'infers action name from class name' do
|
||||||
|
expect(action.name).to eq 'action'
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -12,23 +12,34 @@ module Producer::Core
|
|||||||
subject(:cli) { CLI.new(arguments, stdout: stdout) }
|
subject(:cli) { CLI.new(arguments, stdout: stdout) }
|
||||||
|
|
||||||
describe '.run!' do
|
describe '.run!' do
|
||||||
|
let(:cli) { double('cli').as_null_object }
|
||||||
let(:output) { StringIO.new }
|
let(:output) { StringIO.new }
|
||||||
subject(:run) { described_class.run! arguments, output: output }
|
subject(:run) { described_class.run! arguments, output: output }
|
||||||
|
|
||||||
it 'builds a new CLI with given arguments' do
|
it 'builds a new CLI with given arguments' do
|
||||||
expect(CLI).to receive(:new).with(arguments).and_call_original
|
expect(described_class)
|
||||||
|
.to receive(:new).with(arguments).and_call_original
|
||||||
run
|
run
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'runs the CLI' do
|
it 'runs the CLI' do
|
||||||
cli = double 'cli'
|
allow(described_class).to receive(:new) { cli }
|
||||||
allow(CLI).to receive(:new) { cli }
|
|
||||||
expect(cli).to receive :run
|
expect(cli).to receive :run
|
||||||
run
|
run
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when recipe argument is missing' do
|
it 'parses CLI arguments' do
|
||||||
let(:arguments) { [] }
|
allow(described_class).to receive(:new) { cli }
|
||||||
|
expect(cli).to receive :parse_arguments!
|
||||||
|
run
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when an argument error is raised' do
|
||||||
|
before do
|
||||||
|
allow(CLI).to receive(:new) { cli }
|
||||||
|
allow(cli).to receive(:parse_arguments!)
|
||||||
|
.and_raise described_class::ArgumentError
|
||||||
|
end
|
||||||
|
|
||||||
it 'exits with a return status of 64' do
|
it 'exits with a return status of 64' do
|
||||||
expect { run }.to raise_error(SystemExit) { |e|
|
expect { run }.to raise_error(SystemExit) { |e|
|
||||||
@ -55,14 +66,6 @@ module Producer::Core
|
|||||||
expect(cli.stdout).to be $stdout
|
expect(cli.stdout).to be $stdout
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'without arguments' do
|
|
||||||
let(:arguments) { [] }
|
|
||||||
|
|
||||||
it 'raises our ArgumentError exception' do
|
|
||||||
expect { cli }.to raise_error described_class::ArgumentError
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#arguments' do
|
describe '#arguments' do
|
||||||
@ -77,6 +80,32 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#parse_arguments!' do
|
||||||
|
context 'with options' do
|
||||||
|
let(:arguments) { ['-v', recipe_file] }
|
||||||
|
|
||||||
|
before { cli.parse_arguments! }
|
||||||
|
|
||||||
|
it 'removes options from arguments' do
|
||||||
|
expect(cli.arguments).to eq [recipe_file]
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'verbose' do
|
||||||
|
it 'sets env logger level to INFO' do
|
||||||
|
expect(cli.env.log_level).to eq Logger::INFO
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'without arguments' do
|
||||||
|
let(:arguments) { [] }
|
||||||
|
|
||||||
|
it 'raises the argument error exception' do
|
||||||
|
expect { cli.parse_arguments! }.to raise_error described_class::ArgumentError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#run' do
|
describe '#run' do
|
||||||
it 'loads the recipe' do
|
it 'loads the recipe' do
|
||||||
cli.run
|
cli.run
|
||||||
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||||||
|
|
||||||
module Producer::Core
|
module Producer::Core
|
||||||
describe Worker do
|
describe Worker do
|
||||||
let(:env) { Env.new }
|
let(:env) { double 'env', log: nil }
|
||||||
subject(:worker) { described_class.new(env) }
|
subject(:worker) { described_class.new(env) }
|
||||||
|
|
||||||
describe '#process' do
|
describe '#process' do
|
||||||
@ -13,23 +13,44 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#process_task' do
|
describe '#process_task' do
|
||||||
let(:action) { double 'action' }
|
let(:action) { double('action', to_s: 'echo').as_null_object }
|
||||||
let(:task) { double('task', actions: [action]).as_null_object }
|
let(:task_name) { 'some_task' }
|
||||||
|
let(:task) { Task.new(task_name, [action]) }
|
||||||
|
|
||||||
|
it 'logs task info' do
|
||||||
|
expect(env).to receive(:log).with /\ATask: #{task_name}/
|
||||||
|
worker.process_task task
|
||||||
|
end
|
||||||
|
|
||||||
context 'when task condition is met' do
|
context 'when task condition is met' do
|
||||||
it 'applies the actions' do
|
it 'applies the actions' do
|
||||||
expect(action).to receive :apply
|
expect(action).to receive :apply
|
||||||
worker.process_task task
|
worker.process_task task
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'logs condition info' do
|
||||||
|
expect(env).to receive(:log).with(' condition: met')
|
||||||
|
worker.process_task task
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'logs action info' do
|
||||||
|
expect(env).to receive(:log).with /\A action: echo/
|
||||||
|
worker.process_task task
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when task condition is not met' do
|
context 'when task condition is not met' do
|
||||||
before { allow(task).to receive(:condition_met?) { false } }
|
let(:task) { Task.new(task_name, [action], false) }
|
||||||
|
|
||||||
it 'does not apply the actions' do
|
it 'does not apply the actions' do
|
||||||
expect(action).not_to receive :apply
|
expect(action).not_to receive :apply
|
||||||
worker.process_task task
|
worker.process_task task
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'logs condition info' do
|
||||||
|
expect(env).to receive(:log).with(' condition: NOT met')
|
||||||
|
worker.process_task task
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,5 +40,17 @@ module Producer::Core
|
|||||||
expect(action.fs).to be env.remote.fs
|
expect(action.fs).to be env.remote.fs
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#name' do
|
||||||
|
it 'returns a word' do
|
||||||
|
expect(action.name).to match /\A\w+\z/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#to_s' do
|
||||||
|
it 'returns a word' do
|
||||||
|
expect(action.to_s).to eq action.name
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user