Skip to content

Commit

Permalink
[Fix rubocop#7733] Fix rubocop-junit-formatter imcompatibility XML
Browse files Browse the repository at this point in the history
## Summary

Fixes rubocop#7733.

This PR fixes rubocop-junit-formatter imcompatibility XML for JUnit formatter

The original rubocop-junit-formatter displayed all cops with or without offenses.
https://github.com/mikian/rubocop-junit-formatter/blob/v0.1.4/lib/rubocop/formatter/junit_formatter.rb#L9

It then displays all cops in XML elements, like the original rubocop-junit-formatter.

## Other Information

In the future, it would be preferable to display only enabled cops.
https://github.com/mikian/rubocop-junit-formatter/blob/v0.1.4/lib/rubocop/formatter/junit_formatter.rb#L7-L8
  • Loading branch information
koic committed Mar 7, 2020
1 parent 9687b88 commit 58f9cd4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,10 @@

* [#7654](https://github.com/rubocop-hq/rubocop/issues/7654): Support `with_fixed_indentation` option for `Layout/ArrayAlignment` cop. ([@nikitasakov][])

### Bug fixes

* [#7733](https://github.com/rubocop-hq/rubocop/issues/7733): Fix rubocop-junit-formatter imcompatibility XML for JUnit formatter. ([@koic][])

## 0.80.1 (2020-02-29)

### Bug fixes
Expand Down
23 changes: 17 additions & 6 deletions lib/rubocop/formatter/junit_formatter.rb
Expand Up @@ -29,18 +29,29 @@ def initialize(output, options = {})
end

def file_finished(file, offenses)
offenses.group_by(&:cop_name).each do |cop_name, grouped_offenses|
# TODO: Returns all cops with the same behavior as
# the original rubocop-junit-formatter.
# https://github.com/mikian/rubocop-junit-formatter/blob/v0.1.4/lib/rubocop/formatter/junit_formatter.rb#L9
#
# In the future, it would be preferable to return only enabled cops.
Cop::Cop.all.each do |cop|
REXML::Element.new('testcase', @testsuite).tap do |testcase|
testcase.attributes['classname'] = file.gsub(
/\.rb\Z/, ''
).gsub("#{Dir.pwd}/", '').tr('/', '.')
testcase.attributes['name'] = cop_name
testcase.attributes['classname'] = classname_attribute_value(file)
testcase.attributes['name'] = cop.cop_name

add_failure_to(testcase, grouped_offenses, cop_name)
target_offenses = offenses.select do |offense|
offense.cop_name == cop.cop_name
end

add_failure_to(testcase, target_offenses, cop.cop_name)
end
end
end

def classname_attribute_value(file)
file.gsub(/\.rb\Z/, '').gsub("#{Dir.pwd}/", '').tr('/', '.')
end

def finished(_inspected_files)
@document.write(output, 2)
end
Expand Down
61 changes: 42 additions & 19 deletions spec/rubocop/formatter/junit_formatter_spec.rb
Expand Up @@ -6,8 +6,8 @@
let(:output) { StringIO.new }

describe '#file_finished' do
it 'displays parsable text' do
cop = RuboCop::Cop::Cop.new
before do
cop = RuboCop::Cop::Layout::SpaceInsideBlockBraces.new
source_buffer = Parser::Source::Buffer.new('test', 1)
source_buffer.source = %w[foo bar baz].join("\n")

Expand All @@ -26,30 +26,53 @@
formatter.file_finished('test_2', cop.offenses)

formatter.finished(nil)
end

expect(output.string).to eq(<<~XML.chop)
it 'displays start of parsable text' do
expect(output.string).to start_with(<<~XML)
<?xml version='1.0'?>
<testsuites>
<testsuite name='rubocop'>
<testcase classname='test_1' name='Cop/Cop'>
<failure type='Cop/Cop' message='message 1'>
test:1:1
</failure>
<failure type='Cop/Cop' message='message 2'>
test:3:2
</failure>
</testcase>
<testcase classname='test_2' name='Cop/Cop'>
<failure type='Cop/Cop' message='message 1'>
test:1:1
</failure>
<failure type='Cop/Cop' message='message 2'>
test:3:2
</failure>
</testcase>
XML
end

it 'displays end of parsable text' do
expect(output.string).to end_with(<<~XML.chop)
</testsuite>
</testsuites>
XML
end

it "displays an offfense for `classname='test_1` in parsable text" do
expect(output.string).to include(<<-XML)
<testcase classname='test_1' name='Layout/SpaceInsideBlockBraces'>
<failure type='Layout/SpaceInsideBlockBraces' message='message 1'>
test:1:1
</failure>
<failure type='Layout/SpaceInsideBlockBraces' message='message 2'>
test:3:2
</failure>
</testcase>
XML
end

it "displays an offfense for `classname='test_2` in parsable text" do
expect(output.string).to include(<<-XML)
<testcase classname='test_2' name='Layout/SpaceInsideBlockBraces'>
<failure type='Layout/SpaceInsideBlockBraces' message='message 1'>
test:1:1
</failure>
<failure type='Layout/SpaceInsideBlockBraces' message='message 2'>
test:3:2
</failure>
</testcase>
XML
end

it 'displays a non-offfense element in parsable text' do
expect(output.string).to include(<<~XML)
<testcase classname='test_1' name='Style/Alias'/>
XML
end
end
end

0 comments on commit 58f9cd4

Please sign in to comment.