Class: RuboCop::Formatter::JUnitFormatter

Inherits:
BaseFormatter show all
Defined in:
lib/rubocop/formatter/junit_formatter.rb

Overview

This formatter formats the report data in JUnit format.

Defined Under Namespace

Classes: FailureElement, TestCaseElement

Constant Summary collapse

ESCAPE_MAP =
{
  '"' => '"',
  "'" => ''',
  '<' => '&lt;',
  '>' => '&gt;',
  '&' => '&amp;'
}.freeze

Instance Attribute Summary

Attributes inherited from BaseFormatter

#options, #output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#file_started, #started

Constructor Details

#initialize(output, options = {}) ⇒ JUnitFormatter

Returns a new instance of JUnitFormatter.



24
25
26
27
28
29
30
# File 'lib/rubocop/formatter/junit_formatter.rb', line 24

def initialize(output, options = {})
  super

  @test_case_elements = []

  reset_count
end

Instance Method Details

#file_finished(file, offenses) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/formatter/junit_formatter.rb', line 32

def file_finished(file, offenses)
  @inspected_file_count += 1

  # 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::Registry.all.each do |cop|
    target_offenses = offenses_for_cop(offenses, cop)
    @offense_count += target_offenses.count

    next unless relevant_for_output?(options, target_offenses)

    add_testcase_element_to_testsuite_element(file, target_offenses, cop)
  end
end

#finished(_inspected_files) ⇒ Object

rubocop:disable Layout/LineLength,Metrics/AbcSize,Metrics/MethodLength



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubocop/formatter/junit_formatter.rb', line 51

def finished(_inspected_files)
  output.puts %(<?xml version='1.0'?>)
  output.puts %(<testsuites>)
  output.puts %(  <testsuite name='rubocop' tests='#{@inspected_file_count}' failures='#{@offense_count}'>)

  @test_case_elements.each do |test_case_element|
    if test_case_element.failures.empty?
      output.puts %(    <testcase classname='#{xml_escape test_case_element.classname}' name='#{test_case_element.name}'/>)
    else
      output.puts %(    <testcase classname='#{xml_escape test_case_element.classname}' name='#{test_case_element.name}'>)
      test_case_element.failures.each do |failure_element|
        output.puts %(      <failure type='#{failure_element.type}' message='#{xml_escape failure_element.message}'>)
        output.puts %(        #{xml_escape failure_element.text})
        output.puts %(      </failure>)
      end
      output.puts %(    </testcase>)
    end
  end

  output.puts %(  </testsuite>)
  output.puts %(</testsuites>)
end