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.

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.


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubocop/formatter/junit_formatter.rb', line 18

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

Instance Method Details

#classname_attribute_value(file) ⇒ Object


61
62
63
# File 'lib/rubocop/formatter/junit_formatter.rb', line 61

def classname_attribute_value(file)
  file.gsub(/\.rb\Z/, '').gsub("#{Dir.pwd}/", '').tr('/', '.')
end

#file_finished(file, offenses) ⇒ Object


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

def file_finished(file, offenses)
  # 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)

    next unless relevant_for_output?(options, target_offenses)

    REXML::Element.new('testcase', @testsuite).tap do |testcase|
      testcase.attributes['classname'] = classname_attribute_value(file)
      testcase.attributes['name'] = cop.cop_name

      add_failure_to(testcase, target_offenses, cop.cop_name)
    end
  end
end

#finished(_inspected_files) ⇒ Object


65
66
67
# File 'lib/rubocop/formatter/junit_formatter.rb', line 65

def finished(_inspected_files)
  @document.write(output, 2)
end

#offenses_for_cop(all_offenses, cop) ⇒ Object


55
56
57
58
59
# File 'lib/rubocop/formatter/junit_formatter.rb', line 55

def offenses_for_cop(all_offenses, cop)
  all_offenses.select do |offense|
    offense.cop_name == cop.cop_name
  end
end

#relevant_for_output?(options, target_offenses) ⇒ Boolean

Returns:

  • (Boolean)

51
52
53
# File 'lib/rubocop/formatter/junit_formatter.rb', line 51

def relevant_for_output?(options, target_offenses)
  !options[:display_only_failed] || target_offenses.any?
end