• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @fileoverview jUnit Reporter
3 * @author Jamund Ferguson
4 */
5"use strict";
6
7const xmlEscape = require("../xml-escape");
8const path = require("path");
9
10//------------------------------------------------------------------------------
11// Helper Functions
12//------------------------------------------------------------------------------
13
14/**
15 * Returns the severity of warning or error
16 * @param {Object} message message object to examine
17 * @returns {string} severity level
18 * @private
19 */
20function getMessageType(message) {
21    if (message.fatal || message.severity === 2) {
22        return "Error";
23    }
24    return "Warning";
25
26}
27
28/**
29 * Returns a full file path without extension
30 * @param {string} filePath input file path
31 * @returns {string} file path without extension
32 * @private
33 */
34function pathWithoutExt(filePath) {
35    return path.join(path.dirname(filePath), path.basename(filePath, path.extname(filePath)));
36}
37
38//------------------------------------------------------------------------------
39// Public Interface
40//------------------------------------------------------------------------------
41
42module.exports = function(results) {
43
44    let output = "";
45
46    output += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
47    output += "<testsuites>\n";
48
49    results.forEach(result => {
50
51        const messages = result.messages;
52        const classname = pathWithoutExt(result.filePath);
53
54        if (messages.length > 0) {
55            output += `<testsuite package="org.eslint" time="0" tests="${messages.length}" errors="${messages.length}" name="${result.filePath}">\n`;
56            messages.forEach(message => {
57                const type = message.fatal ? "error" : "failure";
58
59                output += `<testcase time="0" name="org.eslint.${message.ruleId || "unknown"}" classname="${classname}">`;
60                output += `<${type} message="${xmlEscape(message.message || "")}">`;
61                output += "<![CDATA[";
62                output += `line ${message.line || 0}, col `;
63                output += `${message.column || 0}, ${getMessageType(message)}`;
64                output += ` - ${xmlEscape(message.message || "")}`;
65                output += (message.ruleId ? ` (${message.ruleId})` : "");
66                output += "]]>";
67                output += `</${type}>`;
68                output += "</testcase>\n";
69            });
70            output += "</testsuite>\n";
71        } else {
72            output += `<testsuite package="org.eslint" time="0" tests="1" errors="0" name="${result.filePath}">\n`;
73            output += `<testcase time="0" name="${result.filePath}" classname="${classname}" />\n`;
74            output += "</testsuite>\n";
75        }
76
77    });
78
79    output += "</testsuites>\n";
80
81    return output;
82};
83