Skip to content

Commit

Permalink
Merge pull request #321 from mikoto2000/add-junit-reporter
Browse files Browse the repository at this point in the history
Add junit reporter
  • Loading branch information
etiennebarrie committed Jun 7, 2023
2 parents 6f06dc1 + 9ef15e2 commit b8b1b86
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,28 @@ app/views/users/_graph.html.erb:27:37: Extra space detected where there should b
2 error(s) were found in ERB files
```

### JUnit

```sh
erblint --format junit
<?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/application/index.html.erb" file="app/views/subscriptions/_menu.html.erb"/>
</testsuite>
```
## Caching
The cache is currently opt-in - to turn it on, use the `--cache` option:
Expand Down
112 changes: 112 additions & 0 deletions lib/erb_lint/reporters/junit_reporter.rb
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
5 changes: 4 additions & 1 deletion spec/erb_lint/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def run(processed_source)

it "shows format instructions" do
expect { subject }.to(
output(/Report offenses in the given format: \(compact, json, multiline\) \(default: multiline\)/).to_stdout
output(Regexp.new("Report offenses in the given format: " \
"\\(compact, json, junit, multiline\\) " \
"\\(default: multiline\\)")).to_stdout
)
end

Expand Down Expand Up @@ -503,6 +505,7 @@ def run(processed_source)
nonexistentformat: is not a valid format. Available formats:
- compact
- json
- junit
- multiline
EOF
end
Expand Down
21 changes: 21 additions & 0 deletions spec/erb_lint/fixtures/junit.xml
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 `%%&gt;` 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>
61 changes: 61 additions & 0 deletions spec/erb_lint/reporters/junit_reporter_spec.rb
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

0 comments on commit b8b1b86

Please sign in to comment.