diff --git a/features/cli/verbose.feature b/features/cli/verbose.feature index ab3cd26..7c34d63 100644 --- a/features/cli/verbose.feature +++ b/features/cli/verbose.feature @@ -41,3 +41,8 @@ Feature: CLI verbose option """ When I successfully execute the recipe with option -v 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" diff --git a/features/steps/environment_steps.rb b/features/steps/environment_steps.rb new file mode 100644 index 0000000..b111468 --- /dev/null +++ b/features/steps/environment_steps.rb @@ -0,0 +1,3 @@ +Given /^I set the environment variable "([^"]+)"$/ do |variable| + set_env variable, 'yes' +end diff --git a/lib/producer/core/cli.rb b/lib/producer/core/cli.rb index 4ff76a5..b64f25b 100644 --- a/lib/producer/core/cli.rb +++ b/lib/producer/core/cli.rb @@ -10,9 +10,13 @@ module Producer ARGUMENTS_SEPARATOR = '--'.freeze + ENV_VERBOSE_KEY = 'PRODUCER_VERBOSE'.freeze + class << self 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 cli.parse_arguments! cli.run @@ -32,12 +36,14 @@ module Producer 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 @stdin = stdin @stdout = stdout @stderr = stderr @env = build_env + + configure_environment! environment end def parse_arguments! @@ -65,6 +71,10 @@ module Producer Env.new(input: @stdin, output: @stdout, error_output: @stderr) end + def configure_environment!(environment) + @env.verbose = true if environment.key? ENV_VERBOSE_KEY + end + def split_arguments_lists(arguments) arguments .chunk { |e| e == ARGUMENTS_SEPARATOR } diff --git a/spec/producer/core/cli_spec.rb b/spec/producer/core/cli_spec.rb index 7b75366..ee55b48 100644 --- a/spec/producer/core/cli_spec.rb +++ b/spec/producer/core/cli_spec.rb @@ -8,15 +8,16 @@ module Producer::Core let(:options) { [] } let(:recipe_file) { fixture_path_for 'recipes/some_recipe.rb' } let(:arguments) { [*options, recipe_file] } + let(:environment) { {} } - subject(:cli) { described_class.new(arguments) } + subject(:cli) { described_class.new(arguments, environment) } describe '.run!' do 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) - .to receive(:new).with(arguments, anything).and_call_original + .to receive(:new).with(arguments, ENV, anything).and_call_original run! end @@ -75,6 +76,14 @@ module Producer::Core it 'assigns CLI stderr as the env error output' do expect(cli.env.error_output).to be cli.stderr 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 describe '#parse_arguments!' do