Fix error formatting in debug mode

Backtrace filtering was only disabled for exception cause, this change
ensure no backtrace filtering at all in debug mode.

* Prevent main exception filtering when debug mode is enabled;
* Test when exception cause must be displayed;
* Test how exception cause must be displayed.
This commit is contained in:
Thibault Jouan 2015-04-03 23:02:10 +00:00
parent b93e5717ce
commit 1ccd95b80f
5 changed files with 54 additions and 25 deletions

View File

@ -1,11 +1,13 @@
Feature: CLI debug option Feature: CLI debug option
Background: Scenario: reports recipe errors with their cause
Given a recipe with: Given a recipe with an error
"""
task(:trigger_error) { fail 'some error' }
"""
Scenario: reports recipe errors
When I execute the recipe with option -d When I execute the recipe with option -d
Then the output must match /\ARuntimeError:.*\n\ncause:\nRuntimeError:/ Then the output must match /\ARuntimeError:.*\n\ncause:\nRuntimeError:/
Scenario: does not exclude anything from backtrace
Given a recipe using a remote
When I execute the recipe on unknown remote target with option -d
Then the output must contain "producer-core"
And the output must contain "net-ssh"
And the output must contain ruby lib directory

View File

@ -26,6 +26,10 @@ Then /^the error output must contain exactly "([^"]+)"$/ do |content|
assert_exact_output content, all_stderr assert_exact_output content, all_stderr
end end
Then /^the output must contain ruby lib directory$/ do
assert_partial_output RbConfig::CONFIG['rubylibdir'], all_output
end
Then /^the output must not contain ruby lib directory$/ do Then /^the output must not contain ruby lib directory$/ do
assert_no_partial_output RbConfig::CONFIG['rubylibdir'], all_output assert_no_partial_output RbConfig::CONFIG['rubylibdir'], all_output
end end

View File

@ -27,6 +27,11 @@ When /^I execute the recipe on unknown remote target$/ do
assert_matching_output '\ASocketError', all_output assert_matching_output '\ASocketError', all_output
end end
When /^I execute the recipe on unknown remote target with option (-.+)$/ do |option|
run_simple "producer recipe.rb #{option} -t #unknown_host.test", false
assert_matching_output '\ASocketError', all_output
end
When /^I successfully execute the recipe$/ do When /^I successfully execute the recipe$/ do
step 'I execute the recipe' step 'I execute the recipe'
assert_exit_status 0 assert_exit_status 0

View File

@ -18,7 +18,7 @@ module Producer
end end
def format(exception) def format(exception)
lines = format_exception exception lines = format_exception exception, filter: !debug?
if debug? && exception.cause if debug? && exception.cause
lines << '' lines << ''

View File

@ -25,9 +25,10 @@ module Producer
describe '#format' do describe '#format' do
let(:rubylibdir) { RbConfig::CONFIG['rubylibdir'] } let(:rubylibdir) { RbConfig::CONFIG['rubylibdir'] }
let(:bt) { %W[backtrace /producer-core /net-ssh #{rubylibdir}] } let(:bt) { %W[backtrace /producer-core /net-ssh #{rubylibdir}] }
let(:exception) { RuntimeError.new('some exception').tap { |o| o.set_backtrace bt } }
def exception def exception_with_cause
begin fail 'original exception' rescue fail 'some exception' end begin fail 'exception cause' rescue fail 'some exception' end
rescue => e rescue => e
e.tap { |o| o.set_backtrace bt } e.tap { |o| o.set_backtrace bt }
end end
@ -41,7 +42,6 @@ module Producer
expect(formatter.format exception).to match /^\s+backtrace$/ expect(formatter.format exception).to match /^\s+backtrace$/
end end
context 'filtering' do
it 'excludes producer code from the backtrace' do it 'excludes producer code from the backtrace' do
expect(formatter.format exception).not_to include 'producer-core' expect(formatter.format exception).not_to include 'producer-core'
end end
@ -54,12 +54,30 @@ module Producer
expect(formatter.format exception).not_to include rubylibdir expect(formatter.format exception).not_to include rubylibdir
end end
context 'when exception has a cause' do
it 'does not include the cause' do
expect(formatter.format exception_with_cause)
.not_to include 'exception cause'
end
end
context 'when debug is enabled' do context 'when debug is enabled' do
let(:debug) { true } let(:debug) { true }
it 'does not exclude producer code from the backtrace' do it 'does not filter the backtrace' do
expect(formatter.format exception).to include 'producer-core' expect(formatter.format exception).to include 'producer-core'
end end
context 'when exception has a cause' do
it 'includes the exception cause' do
expect(formatter.format exception_with_cause)
.to include 'exception cause'
end
it 'formats the cause' do
expect(formatter.format exception_with_cause)
.to match /^cause:\nRuntimeError: exception cause/
end
end end
end end
end end