You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.7 KiB

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