-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #321 from mikoto2000/add-junit-reporter
Add junit reporter
- Loading branch information
Showing
5 changed files
with
220 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rexml/document" | ||
require "rexml/formatters/pretty" | ||
|
||
module ERBLint | ||
module Reporters | ||
class JunitReporter < Reporter | ||
def preview; end | ||
|
||
def show | ||
xml = create_junit_xml | ||
formatted_xml_string = StringIO.new | ||
REXML::Formatters::Pretty.new.write(xml, formatted_xml_string) | ||
puts formatted_xml_string.string | ||
end | ||
|
||
private | ||
|
||
CONTEXT = { | ||
prologue_quote: :quote, | ||
attribute_quote: :quote, | ||
} | ||
|
||
def create_junit_xml | ||
# create prologue | ||
xml = REXML::Document.new(nil, CONTEXT) | ||
xml << REXML::XMLDecl.new("1.0", "UTF-8") | ||
|
||
xml.add_element(create_testsuite_element) | ||
|
||
xml | ||
end | ||
|
||
def create_testsuite_element | ||
tests = stats.processed_files.size | ||
failures = stats.found | ||
testsuite_element = REXML::Element.new("testsuite", nil, CONTEXT) | ||
testsuite_element.add_attribute("name", "erblint") | ||
testsuite_element.add_attribute("tests", tests.to_s) | ||
testsuite_element.add_attribute("failures", failures.to_s) | ||
|
||
testsuite_element.add_element(create_properties) | ||
|
||
processed_files.each do |filename, offenses| | ||
if offenses.empty? | ||
testcase_element = REXML::Element.new("testcase", nil, CONTEXT) | ||
testcase_element.add_attribute("name", filename.to_s) | ||
testcase_element.add_attribute("file", filename.to_s) | ||
|
||
testsuite_element.add_element(testcase_element) | ||
end | ||
|
||
offenses.each do |offense| | ||
testsuite_element.add_element(create_testcase(filename, offense)) | ||
end | ||
end | ||
|
||
testsuite_element | ||
end | ||
|
||
def create_properties | ||
properties_element = REXML::Element.new("properties", nil, CONTEXT) | ||
|
||
[ | ||
["erb_lint_version", ERBLint::VERSION], | ||
["ruby_engine", RUBY_ENGINE], | ||
["ruby_version", RUBY_VERSION], | ||
["ruby_patchlevel", RUBY_PATCHLEVEL.to_s], | ||
["ruby_platform", RUBY_PLATFORM], | ||
].each do |property_attribute| | ||
properties_element.add_element(create_property(*property_attribute)) | ||
end | ||
|
||
properties_element | ||
end | ||
|
||
def create_property(name, value) | ||
property_element = REXML::Element.new("property") | ||
property_element.add_attribute("name", name) | ||
property_element.add_attribute("value", value) | ||
|
||
property_element | ||
end | ||
|
||
def create_testcase(filename, offense) | ||
testcase_element = REXML::Element.new("testcase", nil, CONTEXT) | ||
testcase_element.add_attribute("name", filename.to_s) | ||
testcase_element.add_attribute("file", filename.to_s) | ||
testcase_element.add_attribute("lineno", offense.line_number.to_s) | ||
|
||
testcase_element.add_element(create_failure(filename, offense)) | ||
|
||
testcase_element | ||
end | ||
|
||
def create_failure(filename, offense) | ||
message = offense.message | ||
type = offense.simple_name | ||
|
||
failure_element = REXML::Element.new("failure", nil, CONTEXT) | ||
failure_element.add_attribute("message", "#{type}: #{message}") | ||
failure_element.add_attribute("type", type.to_s) | ||
|
||
cdata_element = REXML::CData.new("#{type}: #{message} at #{filename}:#{offense.line_number}:#{offense.column}") | ||
failure_element.add_text(cdata_element) | ||
|
||
failure_element | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuite name="erblint" tests="2" failures="2"> | ||
<properties> | ||
<property name="erb_lint_version" value="%{erb_lint_version}"/> | ||
<property name="ruby_engine" value="%{ruby_engine}"/> | ||
<property name="ruby_version" value="%{ruby_version}"/> | ||
<property name="ruby_patchlevel" value="%{ruby_patchlevel}"/> | ||
<property name="ruby_platform" value="%{ruby_platform}"/> | ||
</properties> | ||
<testcase name="app/views/subscriptions/_loader.html.erb" file="app/views/subscriptions/_loader.html.erb" lineno="1"> | ||
<failure message="SpaceInHtmlTag: Extra space detected where there should be no space." type="SpaceInHtmlTag"> | ||
<![CDATA[SpaceInHtmlTag: Extra space detected where there should be no space. at app/views/subscriptions/_loader.html.erb:1:7]]> | ||
</failure> | ||
</testcase> | ||
<testcase name="app/views/subscriptions/_loader.html.erb" file="app/views/subscriptions/_loader.html.erb" lineno="52"> | ||
<failure message="ClosingErbTagIndent: Remove newline before `%%>` to match start of tag." type="ClosingErbTagIndent"> | ||
<![CDATA[ClosingErbTagIndent: Remove newline before `%%>` to match start of tag. at app/views/subscriptions/_loader.html.erb:52:10]]> | ||
</failure> | ||
</testcase> | ||
<testcase name="app/views/application/index.html.erb" file="app/views/application/index.html.erb"/> | ||
</testsuite> |
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,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe ERBLint::Reporters::JunitReporter do | ||
describe ".show" do | ||
subject { described_class.new(stats, false).show } | ||
|
||
let(:stats) do | ||
ERBLint::Stats.new( | ||
found: 2, | ||
processed_files: { | ||
"app/views/subscriptions/_loader.html.erb" => offenses, | ||
"app/views/application/index.html.erb" => [], | ||
}, | ||
corrected: 1 | ||
) | ||
end | ||
|
||
let(:offenses) do | ||
[ | ||
instance_double( | ||
ERBLint::Offense, | ||
message: "Extra space detected where there should be no space.", | ||
line_number: 1, | ||
column: 7, | ||
simple_name: "SpaceInHtmlTag", | ||
last_line: 1, | ||
last_column: 9, | ||
length: 2, | ||
), | ||
instance_double( | ||
ERBLint::Offense, | ||
message: "Remove newline before `%>` to match start of tag.", | ||
line_number: 52, | ||
column: 10, | ||
simple_name: "ClosingErbTagIndent", | ||
last_line: 54, | ||
last_column: 10, | ||
length: 10, | ||
), | ||
] | ||
end | ||
|
||
let(:expected_xml) do | ||
content = File.read(File.expand_path("./spec/erb_lint/fixtures/junit.xml")) | ||
format( | ||
content, | ||
erb_lint_version: ERBLint::VERSION, | ||
ruby_engine: RUBY_ENGINE, | ||
ruby_version: RUBY_VERSION, | ||
ruby_patchlevel: RUBY_PATCHLEVEL.to_s, | ||
ruby_platform: RUBY_PLATFORM | ||
) | ||
end | ||
|
||
it "displays formatted offenses output" do | ||
expect { subject }.to(output(expected_xml).to_stdout) | ||
end | ||
end | ||
end |