Enable verbose mode when PRODUCER_VERBOSE is set

This commit is contained in:
Thibault Jouan 2015-04-06 13:45:57 +00:00
parent cd389a3209
commit bd754f2bf6
4 changed files with 32 additions and 5 deletions

View File

@ -41,3 +41,8 @@ Feature: CLI verbose option
""" """
When I successfully execute the recipe with option -v When I successfully execute the recipe with option -v
Then the output must match /action: .{,70}$/ Then the output must match /action: .{,70}$/
Scenario: enables verbose mode from the environment
Given I set the environment variable "PRODUCER_VERBOSE"
When I successfully execute the recipe
Then the output must contain "Task"

View File

@ -0,0 +1,3 @@
Given /^I set the environment variable "([^"]+)"$/ do |variable|
set_env variable, 'yes'
end

View File

@ -10,9 +10,13 @@ module Producer
ARGUMENTS_SEPARATOR = '--'.freeze ARGUMENTS_SEPARATOR = '--'.freeze
ENV_VERBOSE_KEY = 'PRODUCER_VERBOSE'.freeze
class << self class << self
def run!(arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr) def run!(arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr)
cli = new(arguments, stdin: stdin, stdout: stdout, stderr: stderr) cli = new(
arguments, ENV, stdin: stdin, stdout: stdout, stderr: stderr
)
begin begin
cli.parse_arguments! cli.parse_arguments!
cli.run cli.run
@ -32,12 +36,14 @@ module Producer
attr_reader :arguments, :stdin, :stdout, :stderr, :env attr_reader :arguments, :stdin, :stdout, :stderr, :env
def initialize(args, stdin: $stdin, stdout: $stdout, stderr: $stderr) def initialize(args, environment, stdin: $stdin, stdout: $stdout, stderr: $stderr)
@arguments = args @arguments = args
@stdin = stdin @stdin = stdin
@stdout = stdout @stdout = stdout
@stderr = stderr @stderr = stderr
@env = build_env @env = build_env
configure_environment! environment
end end
def parse_arguments! def parse_arguments!
@ -65,6 +71,10 @@ module Producer
Env.new(input: @stdin, output: @stdout, error_output: @stderr) Env.new(input: @stdin, output: @stdout, error_output: @stderr)
end end
def configure_environment!(environment)
@env.verbose = true if environment.key? ENV_VERBOSE_KEY
end
def split_arguments_lists(arguments) def split_arguments_lists(arguments)
arguments arguments
.chunk { |e| e == ARGUMENTS_SEPARATOR } .chunk { |e| e == ARGUMENTS_SEPARATOR }

View File

@ -8,15 +8,16 @@ module Producer::Core
let(:options) { [] } let(:options) { [] }
let(:recipe_file) { fixture_path_for 'recipes/some_recipe.rb' } let(:recipe_file) { fixture_path_for 'recipes/some_recipe.rb' }
let(:arguments) { [*options, recipe_file] } let(:arguments) { [*options, recipe_file] }
let(:environment) { {} }
subject(:cli) { described_class.new(arguments) } subject(:cli) { described_class.new(arguments, environment) }
describe '.run!' do describe '.run!' do
subject(:run!) { described_class.run! arguments } subject(:run!) { described_class.run! arguments }
it 'builds a new CLI instance with given arguments' do it 'builds a new CLI instance with given arguments and environment' do
expect(described_class) expect(described_class)
.to receive(:new).with(arguments, anything).and_call_original .to receive(:new).with(arguments, ENV, anything).and_call_original
run! run!
end end
@ -75,6 +76,14 @@ module Producer::Core
it 'assigns CLI stderr as the env error output' do it 'assigns CLI stderr as the env error output' do
expect(cli.env.error_output).to be cli.stderr expect(cli.env.error_output).to be cli.stderr
end end
context 'when PRODUCER_VERBOSE environment variable is set' do
before { environment['PRODUCER_VERBOSE'] = 'yes' }
it 'enables env verbose mode' do
expect(cli.env).to be_verbose
end
end
end end
describe '#parse_arguments!' do describe '#parse_arguments!' do