Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregate failures #1

Merged
merged 4 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 26 additions & 1 deletion lib/rspec_junit_formatter/rspec3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,32 @@ def failure_message_for(example)
end

def failure_for(notification)
strip_diff_colors(notification.message_lines.join("\n")) << "\n" << notification.formatted_backtrace.join("\n")
lines = if notification.respond_to?(:fully_formatted_lines)
notification.fully_formatted_lines(nil, RSpec::Core::Notifications::NullColorizer).map(&:to_s)
else
notification.fully_formatted(nil, RSpec::Core::Notifications::NullColorizer).split("\n")
end

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

unless notification.respond_to?(:fully_formatted_lines)
if lines[0][2] == ')'
lines[0][2] = ' '
end
end

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

def exception_for(notification)
Expand Down
20 changes: 16 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,17 @@ 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")
if Gem::Version.new(RSpec::Core::Version::STRING) >= Gem::Version.new('3.3')
expect(testcase.text).to include("bar")
else
expect(testcase.text).to_not include("bar")
end
end

# it cleans up diffs

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