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

#started

Constructor Details

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

Returns a new instance of JUnitFormatter.



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

def initialize(output, options = {})
  super

  @test_case_elements = []
  @config_store = nil

  reset_count
end

Instance Method Details

#file_finished(file, offenses) ⇒ Object



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

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

  cops_for(file).each do |cop|
    target_offenses = offenses_for_cop(offenses, cop)

    next unless relevant_for_output?(options, target_offenses)

    add_testcase_element_to_testsuite_element(file, target_offenses, cop)
  end
end

#file_started(_file, options) ⇒ Object



33
34
35
# File 'lib/rubocop/formatter/junit_formatter.rb', line 33

def file_started(_file, options)
  @config_store = options[:config_store]
end

#finished(_inspected_files) ⇒ Object

rubocop:disable-next 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