Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #7686. This PR Adds new `JUnitFormatter` formatter based on rubocop-junit-formatter gem. https://github.com/mikian/rubocop-junit-formatter And this PR includes the following patch to implementation of `JUnitFormatter`. mikian/rubocop-junit-formatter#11. REXML gem has been bundled gem since Ruby 2.8.0-dev (Ruby 3.0), it is added to depend on gemspec. - https://bugs.ruby-lang.org/issues/16485 - ruby/ruby@c3ccf23
- Loading branch information
Showing
10 changed files
with
158 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rexml/document' | ||
|
||
# | ||
# This code is based on https://github.com/mikian/rubocop-junit-formatter. | ||
# | ||
# Copyright (c) 2015 Mikko Kokkonen | ||
# | ||
# MIT License | ||
# | ||
# https://github.com/mikian/rubocop-junit-formatter/blob/master/LICENSE.txt | ||
# | ||
module RuboCop | ||
module Formatter | ||
# This formatter formats the report data in JUnit format. | ||
class JUnitFormatter < BaseFormatter | ||
def initialize(output, options = {}) | ||
super | ||
|
||
@document = REXML::Document.new.tap do |document| | ||
document << REXML::XMLDecl.new | ||
end | ||
testsuites = REXML::Element.new('testsuites', @document) | ||
testsuite = REXML::Element.new('testsuite', testsuites) | ||
@testsuite = testsuite.tap do |element| | ||
element.add_attributes('name' => 'rubocop') | ||
end | ||
end | ||
|
||
def file_finished(file, offenses) | ||
offenses.group_by(&:cop_name).each do |cop_name, grouped_offenses| | ||
REXML::Element.new('testcase', @testsuite).tap do |testcase| | ||
testcase.attributes['classname'] = file.gsub( | ||
/\.rb\Z/, '' | ||
).gsub("#{Dir.pwd}/", '').tr('/', '.') | ||
testcase.attributes['name'] = cop_name | ||
|
||
add_failure_to(testcase, grouped_offenses, cop_name) | ||
end | ||
end | ||
end | ||
|
||
def finished(_inspected_files) | ||
@document.write(output, 2) | ||
end | ||
|
||
private | ||
|
||
def add_failure_to(testcase, offenses, cop_name) | ||
# One failure per offense. Zero failures is a passing test case, | ||
# for most surefire/nUnit parsers. | ||
offenses.each do |offense| | ||
REXML::Element.new('failure', testcase).tap do |failure| | ||
failure.attributes['type'] = cop_name | ||
failure.attributes['message'] = offense.message | ||
failure.add_text(offense.location.to_s) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Formatter::JUnitFormatter do | ||
subject(:formatter) { described_class.new(output) } | ||
|
||
let(:output) { StringIO.new } | ||
|
||
describe '#file_finished' do | ||
it 'displays parsable text' do | ||
cop = RuboCop::Cop::Cop.new | ||
source_buffer = Parser::Source::Buffer.new('test', 1) | ||
source_buffer.source = %w[foo bar baz].join("\n") | ||
|
||
cop.add_offense( | ||
nil, | ||
location: Parser::Source::Range.new(source_buffer, 0, 1), | ||
message: 'message 1' | ||
) | ||
cop.add_offense( | ||
nil, | ||
location: Parser::Source::Range.new(source_buffer, 9, 10), | ||
message: 'message 2' | ||
) | ||
|
||
formatter.file_finished('test_1', cop.offenses) | ||
formatter.file_finished('test_2', cop.offenses) | ||
|
||
formatter.finished(nil) | ||
|
||
expect(output.string).to eq(<<~XML.chop) | ||
<?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> | ||
</testsuite> | ||
</testsuites> | ||
XML | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ def abs(path) | |
[fu]ubar | ||
[h]tml | ||
[j]son | ||
[ju]nit | ||
[o]ffenses | ||
[pa]cman | ||
[p]rogress | ||
|