Class: RuboCop::Formatter::JUnitFormatter

Inherits:
ClangStyleFormatter
  • Object
show all
Defined in:
lib/rubocop/formatter/junit_formatter.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of JUnitFormatter.



8
9
10
11
# File 'lib/rubocop/formatter/junit_formatter.rb', line 8

def initialize(output, options = {})
  super
  @failures = 0
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rubocop/formatter/junit_formatter.rb', line 32

def file_finished(file, offenses)
  time = Time.now - @files[file]
  @failures += 1 if offenses.any?

  file_smart_path = smart_path(file)

  testcase = @testsuite.add_element("testcase")
  testcase.add_attributes(
    "name" => file_smart_path,
    "file" => file_smart_path,
    "failures" => offenses.size,
    "time" => time.round(6)
  )

  offenses.each do |offense|
    failure = testcase.add_element("failure")
    failure.add_attributes(
      "message" => offense.message,
      "type" => offense.severity.to_s
    )

      text = sprintf(
        "%s:%d:%d: %s: %s\n",
        file_smart_path,
        offense.line,
        offense.real_column,
        offense.severity.code,
        offense.message
      )

    begin
      if valid_line?(offense)
        source_line = offense.location.source_line

        if offense.location.first_line == offense.location.last_line
          text << "#{source_line}\n"
        else
          text << "#{source_line} #{yellow(ELLIPSES)}\n"
        end

        text << "#{' ' * offense.highlighted_area.begin_pos}" \
                "#{'^' * offense.highlighted_area.size}"
      end
    rescue IndexError
      # range is not on a valid line; perhaps the source file is empty
    end

    failure.add_text(text)
  end
end

#file_started(file, options) ⇒ Object



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

def file_started(file, options)
  @files ||= {}
  @files[file] = Time.now
end

#finished(inspected_files) ⇒ Object



83
84
85
86
# File 'lib/rubocop/formatter/junit_formatter.rb', line 83

def finished(inspected_files)
  @testsuite.add_attribute("failures", @failures)
  @document.write(output)
end

#started(target_files) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubocop/formatter/junit_formatter.rb', line 13

def started(target_files)
  @document = REXML::Document.new
  @document << REXML::XMLDecl.new.tap do |declaration|
    declaration.encoding = "UTF-8"
  end

  @testsuite = @document.add_element("testsuite")
  @testsuite.add_attributes(
    "name" => "RuboCop",
    "tests" => target_files.size,
    "timestamp" => DateTime.now.new_offset(0)
  )
end