Skip to content

Commit

Permalink
Use fully_formatted_lines to get detail for aggregated failures
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncodes committed Oct 15, 2019
1 parent a65e873 commit ba98856
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions example/spec/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
end
end

it "should support multiple failures", aggregate_failures: true do
expect('foo').to eq 1
expect('bar').to eq 2
end

it "shows diffs cleanly" do
expect({a: "b", c: "d"}).to eql({a: 2, c: 4})
end
Expand Down
15 changes: 14 additions & 1 deletion lib/rspec_junit_formatter/rspec3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,20 @@ def failure_message_for(example)
end

def failure_for(notification)
lines = notification.message_lines + [''] + notification.formatted_backtrace
lines = notification.fully_formatted_lines(nil, RSpec::Core::Notifications::NullColorizer).map(&:to_s)

unless lines.first.empty?
raise 'Expected first line to be empty'
end
lines.shift

indentation = lines.reject(&:empty?).map { |line| line[/^[ \t]*/] }.min
lines = lines.map { |line| line.sub(/^#{Regexp.escape indentation}/, '') }

unless lines.first == notification.description
raise 'Expected second line to be description'
end
lines.shift

strip_diff_colors(lines.join("\n"))
end
Expand Down
16 changes: 12 additions & 4 deletions spec/rspec_junit_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def execute_example_spec
let(:failed_testcases) { doc.xpath("/testsuite/testcase[failure]") }
let(:shared_testcases) { doc.xpath("/testsuite/testcase[contains(@name, 'shared example')]") }
let(:failed_shared_testcases) { doc.xpath("/testsuite/testcase[contains(@name, 'shared example')][failure]") }
let(:failed_multiple_testcases) { doc.xpath("/testsuite/testcase[contains(@name, 'multiple')][failure]") }

# Combined into a single example so we don't have to re-run the example rspec
# process over and over. (We need to change the parameters in later specs so
Expand All @@ -57,17 +58,17 @@ def execute_example_spec
expect(testsuite).not_to be(nil)

expect(testsuite["name"]).to eql("rspec")
expect(testsuite["tests"]).to eql("12")
expect(testsuite["tests"]).to eql("13")
expect(testsuite["skipped"]).to eql("1")
expect(testsuite["failures"]).to eql("8")
expect(testsuite["failures"]).to eql("9")
expect(testsuite["errors"]).to eql("0")
expect(Time.parse(testsuite["timestamp"])).to be_within(60).of(Time.now)
expect(testsuite["time"].to_f).to be > 0
expect(testsuite["hostname"]).not_to be_empty

# it has some test cases

expect(testcases.size).to eql(12)
expect(testcases.size).to eql(13)

testcases.each do |testcase|
expect(testcase["classname"]).to eql("spec.example_spec")
Expand Down Expand Up @@ -101,7 +102,7 @@ def execute_example_spec

# it has failed test cases

expect(failed_testcases.size).to eql(8)
expect(failed_testcases.size).to eql(9)

failed_testcases.each do |testcase|
expect(testcase).not_to be(nil)
Expand All @@ -128,6 +129,13 @@ def execute_example_spec
expect(testcase.text).to include("shared_examples.rb")
end

# it has detail for an aggregate_failures example
expect(failed_multiple_testcases.size).to eql(1)
failed_multiple_testcases.each do |testcase|
expect(testcase.text).to include("foo")
expect(testcase.text).to include("bar")
end

# it cleans up diffs

diff_testcase_failure = doc.xpath("//testcase[contains(@name, 'diffs')]/failure").first
Expand Down

0 comments on commit ba98856

Please sign in to comment.