123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var fs = require("fs");
  4. var typescript_1 = require("typescript");
  5. var helpers = require("../util/helpers");
  6. var loggerDiagnostics = require("../logger/logger-diagnostics");
  7. var tsLogger = require("../logger/logger-typescript");
  8. var tsLintLogger = require("../logger/logger-tslint");
  9. var linter = require("./lint-factory");
  10. var utils = require("./lint-utils");
  11. describe('lint utils', function () {
  12. describe('lintFile()', function () {
  13. it('should return lint details', function () {
  14. var filePath = 'test.ts';
  15. var fileContent = "\n export const foo = 'bar';\n ";
  16. var context = {
  17. rootDir: ''
  18. };
  19. var mockLintResult = {
  20. errorCount: 0,
  21. warningCount: 0,
  22. failures: [],
  23. fixes: [],
  24. format: '',
  25. output: ''
  26. };
  27. spyOn(linter, linter.lint.name).and.returnValue(mockLintResult);
  28. spyOn(linter, linter.createProgram.name).and.returnValue({});
  29. spyOn(linter, linter.createLinter.name).and.returnValue({});
  30. // Mock the file read
  31. spyOn(helpers, helpers.readFileAsync.name).and.returnValue(Promise.resolve(fileContent));
  32. spyOn(fs, 'openSync').and.returnValue(null);
  33. spyOn(fs, 'readSync').and.returnValue(null);
  34. spyOn(fs, 'closeSync').and.returnValue(null);
  35. var mockProgram = linter.createProgram(context, '');
  36. var mockLinter = linter.createLinter(context, mockProgram);
  37. var mockConfig = {};
  38. return utils.lintFile(mockLinter, mockConfig, filePath)
  39. .then(function () {
  40. expect(linter.lint)
  41. .toHaveBeenCalledWith(mockLinter, mockConfig, filePath, fileContent);
  42. });
  43. });
  44. });
  45. describe('processTypeCheckDiagnostics()', function () {
  46. it('should not throw an error when there are no files with errors or warnings', function () {
  47. utils.processTypeCheckDiagnostics({}, []);
  48. });
  49. it('should throw an error when one or more file has failures', function () {
  50. var knownError = new Error('Should never get here');
  51. var results = [
  52. {
  53. file: {},
  54. start: 0,
  55. length: 10,
  56. messageText: 'Something failed',
  57. category: typescript_1.DiagnosticCategory.Warning,
  58. code: 100
  59. }
  60. ];
  61. spyOn(tsLogger, tsLogger.runTypeScriptDiagnostics.name).and.returnValue(null);
  62. spyOn(loggerDiagnostics, loggerDiagnostics.printDiagnostics.name).and.returnValue(null);
  63. try {
  64. utils.processTypeCheckDiagnostics({}, results);
  65. throw knownError;
  66. }
  67. catch (e) {
  68. expect(loggerDiagnostics.printDiagnostics).toHaveBeenCalledTimes(1);
  69. expect(e).not.toEqual(knownError);
  70. }
  71. });
  72. });
  73. describe('processLintResult()', function () {
  74. it('should not throw an error when there are no files with errors or warnings', function () {
  75. utils.processLintResult({}, {
  76. errorCount: 0,
  77. warningCount: 0,
  78. failures: [],
  79. fixes: [],
  80. format: '',
  81. output: ''
  82. });
  83. });
  84. it('should throw an error when one or more file has failures', function () {
  85. var knownError = new Error('Should never get here');
  86. var result = {
  87. errorCount: 1,
  88. warningCount: 0,
  89. failures: [
  90. {
  91. getFileName: function () {
  92. return 'test.ts';
  93. }
  94. }
  95. ],
  96. fixes: [],
  97. format: '',
  98. output: ''
  99. };
  100. spyOn(tsLintLogger, tsLintLogger.runTsLintDiagnostics.name).and.returnValue(null);
  101. spyOn(loggerDiagnostics, loggerDiagnostics.printDiagnostics.name).and.returnValue(null);
  102. try {
  103. utils.processLintResult({}, result);
  104. throw knownError;
  105. }
  106. catch (ex) {
  107. expect(loggerDiagnostics.printDiagnostics).toHaveBeenCalledTimes(1);
  108. expect(ex).not.toEqual(knownError);
  109. }
  110. });
  111. });
  112. describe('generateErrorMessageForFiles()', function () {
  113. it('should generate a string from an array of files', function () {
  114. expect(utils.generateErrorMessageForFiles(['test_one.ts', 'test_two.ts'], 'Just testing:'))
  115. .toEqual('Just testing:\ntest_one.ts\ntest_two.ts');
  116. });
  117. });
  118. describe('getFileNames()', function () {
  119. it('should retrieve file names from an array of RuleFailure objects', function () {
  120. var ruleFailures = [
  121. {
  122. getFileName: function () {
  123. return '/User/john/test.ts';
  124. }
  125. }
  126. ];
  127. var fileNames = utils.getFileNames({ rootDir: '/User/john' }, ruleFailures);
  128. expect(fileNames)
  129. .toEqual(['test.ts']);
  130. });
  131. });
  132. describe('removeDuplicateFileNames()', function () {
  133. it('should remove duplicate string entries in arrays', function () {
  134. expect(utils.removeDuplicateFileNames(['test.ts', 'test.ts']))
  135. .toEqual(['test.ts']);
  136. });
  137. });
  138. });