junitFormatter.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. var abstractFormatter_1 = require("../language/formatter/abstractFormatter");
  21. var Utils = require("../utils");
  22. var Formatter = /** @class */ (function (_super) {
  23. tslib_1.__extends(Formatter, _super);
  24. function Formatter() {
  25. return _super !== null && _super.apply(this, arguments) || this;
  26. }
  27. /* tslint:enable:object-literal-sort-keys */
  28. Formatter.prototype.format = function (failures) {
  29. var output = '<?xml version="1.0" encoding="utf-8"?><testsuites package="tslint">';
  30. if (failures.length !== 0) {
  31. var failuresSorted = failures.sort(function (a, b) { return a.getFileName().localeCompare(b.getFileName()); });
  32. var previousFilename = null;
  33. for (var _i = 0, failuresSorted_1 = failuresSorted; _i < failuresSorted_1.length; _i++) {
  34. var failure = failuresSorted_1[_i];
  35. var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
  36. var message = this.escapeXml(failure.getFailure());
  37. var rule = this.escapeXml(failure.getRuleName());
  38. var severity = failure.getRuleSeverity();
  39. if (failure.getFileName() !== previousFilename) {
  40. if (previousFilename !== null) {
  41. output += "</testsuite>";
  42. }
  43. previousFilename = failure.getFileName();
  44. output += "<testsuite name=\"" + this.escapeXml(failure.getFileName()) + "\">";
  45. }
  46. output += "<testcase name=\"Line " + (lineAndCharacter.line + 1) + ", ";
  47. output += "Column " + (lineAndCharacter.character + 1) + ": " + rule + "\">";
  48. output += "<failure type=\"" + severity + "\">" + message + "</failure>";
  49. output += "</testcase>";
  50. }
  51. if (previousFilename !== null) {
  52. output += "</testsuite>";
  53. }
  54. }
  55. output += "</testsuites>";
  56. return output;
  57. };
  58. Formatter.prototype.escapeXml = function (str) {
  59. return str
  60. .replace(/&/g, "&amp;")
  61. .replace(/</g, "&lt;")
  62. .replace(/>/g, "&gt;")
  63. .replace(/'/g, "&#39;")
  64. .replace(/"/g, "&quot;");
  65. };
  66. /* tslint:disable:object-literal-sort-keys */
  67. Formatter.metadata = {
  68. formatterName: "junit",
  69. description: "Formats errors as through they were JUnit output.",
  70. descriptionDetails: Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Imitates the JUnit XML Output"], ["\n Imitates the JUnit XML Output"]))),
  71. sample: Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n <?xml version=\"1.0\" encoding=\"utf-8\"?>\n <testsuites package=\"tslint\">\n <testsuite name=\"myFile.ts\">\n <testcase name=\"Line 1, Column 14: semicolon\">\n <failure type=\"warning\">Missing semicolon</failure>\n </testcase>\n </testsuite>\n </testsuites>\n "], ["\n <?xml version=\"1.0\" encoding=\"utf-8\"?>\n <testsuites package=\"tslint\">\n <testsuite name=\"myFile.ts\">\n <testcase name=\"Line 1, Column 14: semicolon\">\n <failure type=\"warning\">Missing semicolon</failure>\n </testcase>\n </testsuite>\n </testsuites>\n "]))),
  72. consumer: "machine",
  73. };
  74. return Formatter;
  75. }(abstractFormatter_1.AbstractFormatter));
  76. exports.Formatter = Formatter;
  77. var templateObject_1, templateObject_2;