Class: Jshint::Reporters::Junit

Inherits:
Object
  • Object
show all
Defined in:
lib/jshint/reporters/junit.rb

Overview

Outputs a lint report in JUnit XML formt

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results = {}) ⇒ Junit

Sets up the output string for the final report

Parameters:

  • results (Hash) (defaults to: {})

    Key value pairs containing the filename and associated errors



12
13
14
15
# File 'lib/jshint/reporters/junit.rb', line 12

def initialize(results = {})
  @results = results
  @output = ''
end

Instance Attribute Details

#outputString (readonly)

Returns the report output.

Returns:

  • (String)

    the report output



7
8
9
# File 'lib/jshint/reporters/junit.rb', line 7

def output
  @output
end

Instance Method Details

#escape(text) ⇒ String

Escapes the text given for XML.

Parameters:

  • text (String)

    The text to escape

Returns:

  • (String)


88
89
90
# File 'lib/jshint/reporters/junit.rb', line 88

def escape(text)
  REXML::Text.new(text, false, nil, false).to_s
end

This method returns an undefined value.

Appends new error elements to the Report output

</testcase>

Examples:

<testcase classname="JUnitXmlReporter.constructor" name="should default path to an empty string" time="0.006">
  <failure message="test failure">Assertion failed</failure>

Parameters:

  • code (String)

    The error code

  • errors (Array)

    The errors for the code



74
75
76
77
78
79
80
81
82
# File 'lib/jshint/reporters/junit.rb', line 74

def print_errors_for_code(code, errors)
  name = fetch_error_messages(code, errors)
  output << "    <testcase classname=\"jshint.#{code}\" name=\"#{escape(name)}\">\n"
  errors.each do |error|
    output << add_error_message(code, error)
  end
  output << "    </testcase>\n"
  output
end

#reportString

Loops through all the errors and generates the report

Examples:

foo/bar/baz.js: line 4, col 46, Bad operand.
foo/bar/baz.js: line 39, col 7, Missing semicolon.

2 errors

Returns:

  • (String)

    The default report



26
27
28
29
30
31
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
# File 'lib/jshint/reporters/junit.rb', line 26

def report
  @output = <<-TEMPLATE
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
  <testsuite name="#{File.basename(Dir.pwd)}" timestamp="#{DateTime.now}">
TEMPLATE

  # Group according to the errors.
  error_groups = {}
  @results.each do |file, errors|
    errors.each do |error|
      next unless error && error['code']

      error_groups[error['code']] ||= []
      error_groups[error['code']] << {
        file: file,
        line: error['line'],
        character: error['character'],
        message: error['reason']
      }
    end
  end

  # Combine all the errors and the tests for which we have messages. Combine both together.
  all_codes = error_groups.keys + TESTS.keys

  all_codes.each do |code|
    print_errors_for_code(code, error_groups.fetch(code, []))
  end

  @output <<= <<-TEMPLATE
  </testsuite>
</testsuites>
TEMPLATE

  output
end