Ohm-Management - Projektarbeit B-ME
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.

html.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @fileoverview HTML reporter
  3. * @author Julian Laval
  4. */
  5. "use strict";
  6. const lodash = require("lodash");
  7. const fs = require("fs");
  8. const path = require("path");
  9. //------------------------------------------------------------------------------
  10. // Helpers
  11. //------------------------------------------------------------------------------
  12. const pageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-page.html"), "utf-8"));
  13. const messageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-message.html"), "utf-8"));
  14. const resultTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-result.html"), "utf-8"));
  15. /**
  16. * Given a word and a count, append an s if count is not one.
  17. * @param {string} word A word in its singular form.
  18. * @param {int} count A number controlling whether word should be pluralized.
  19. * @returns {string} The original word with an s on the end if count is not one.
  20. */
  21. function pluralize(word, count) {
  22. return (count === 1 ? word : `${word}s`);
  23. }
  24. /**
  25. * Renders text along the template of x problems (x errors, x warnings)
  26. * @param {string} totalErrors Total errors
  27. * @param {string} totalWarnings Total warnings
  28. * @returns {string} The formatted string, pluralized where necessary
  29. */
  30. function renderSummary(totalErrors, totalWarnings) {
  31. const totalProblems = totalErrors + totalWarnings;
  32. let renderedText = `${totalProblems} ${pluralize("problem", totalProblems)}`;
  33. if (totalProblems !== 0) {
  34. renderedText += ` (${totalErrors} ${pluralize("error", totalErrors)}, ${totalWarnings} ${pluralize("warning", totalWarnings)})`;
  35. }
  36. return renderedText;
  37. }
  38. /**
  39. * Get the color based on whether there are errors/warnings...
  40. * @param {string} totalErrors Total errors
  41. * @param {string} totalWarnings Total warnings
  42. * @returns {int} The color code (0 = green, 1 = yellow, 2 = red)
  43. */
  44. function renderColor(totalErrors, totalWarnings) {
  45. if (totalErrors !== 0) {
  46. return 2;
  47. }
  48. if (totalWarnings !== 0) {
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. /**
  54. * Get HTML (table rows) describing the messages.
  55. * @param {Array} messages Messages.
  56. * @param {int} parentIndex Index of the parent HTML row.
  57. * @returns {string} HTML (table rows) describing the messages.
  58. */
  59. function renderMessages(messages, parentIndex) {
  60. /**
  61. * Get HTML (table row) describing a message.
  62. * @param {Object} message Message.
  63. * @returns {string} HTML (table row) describing a message.
  64. */
  65. return lodash.map(messages, message => {
  66. const lineNumber = message.line || 0;
  67. const columnNumber = message.column || 0;
  68. return messageTemplate({
  69. parentIndex,
  70. lineNumber,
  71. columnNumber,
  72. severityNumber: message.severity,
  73. severityName: message.severity === 1 ? "Warning" : "Error",
  74. message: message.message,
  75. ruleId: message.ruleId
  76. });
  77. }).join("\n");
  78. }
  79. /**
  80. * @param {Array} results Test results.
  81. * @returns {string} HTML string describing the results.
  82. */
  83. function renderResults(results) {
  84. return lodash.map(results, (result, index) => resultTemplate({
  85. index,
  86. color: renderColor(result.errorCount, result.warningCount),
  87. filePath: result.filePath,
  88. summary: renderSummary(result.errorCount, result.warningCount)
  89. }) + renderMessages(result.messages, index)).join("\n");
  90. }
  91. //------------------------------------------------------------------------------
  92. // Public Interface
  93. //------------------------------------------------------------------------------
  94. module.exports = function(results) {
  95. let totalErrors,
  96. totalWarnings;
  97. totalErrors = 0;
  98. totalWarnings = 0;
  99. // Iterate over results to get totals
  100. results.forEach(result => {
  101. totalErrors += result.errorCount;
  102. totalWarnings += result.warningCount;
  103. });
  104. return pageTemplate({
  105. date: new Date(),
  106. reportColor: renderColor(totalErrors, totalWarnings),
  107. reportSummary: renderSummary(totalErrors, totalWarnings),
  108. results: renderResults(results)
  109. });
  110. };