1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
"use strict";
/**
* @license
* Copyright 2016 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var colors = require("colors");
var diff = require("diff");
var fs = require("fs");
var glob = require("glob");
var path = require("path");
var semver = require("semver");
var ts = require("typescript");
var rule_1 = require("./language/rule/rule");
var Linter = require("./linter");
var parse = require("./test/parse");
var utils_1 = require("./utils");
var MARKUP_FILE_EXTENSION = ".lint";
var FIXES_FILE_EXTENSION = ".fix";
function runTests(patterns, rulesDirectory) {
var files = [];
for (var _i = 0, patterns_1 = patterns; _i < patterns_1.length; _i++) {
var pattern = patterns_1[_i];
files.push.apply(files, glob.sync(pattern + "/tslint.json"));
}
return files.map(function (directory) { return runTest(path.dirname(directory), rulesDirectory); });
}
exports.runTests = runTests;
function runTest(testDirectory, rulesDirectory) {
// needed to get colors to show up when passing through Grunt
colors.enabled = true;
var filesToLint = glob.sync(path.join(testDirectory, "**/*" + MARKUP_FILE_EXTENSION));
var tslintConfig = Linter.findConfiguration(path.join(testDirectory, "tslint.json"), "").results;
var tsConfig = path.join(testDirectory, "tsconfig.json");
var compilerOptions = { allowJs: true };
var hasConfig = fs.existsSync(tsConfig);
if (hasConfig) {
var _a = ts.readConfigFile(tsConfig, ts.sys.readFile), config = _a.config, error = _a.error;
if (error !== undefined) {
throw new Error(JSON.stringify(error));
}
var parseConfigHost = {
fileExists: fs.existsSync,
readDirectory: ts.sys.readDirectory,
readFile: function (file) { return fs.readFileSync(file, "utf8"); },
useCaseSensitiveFileNames: true,
};
compilerOptions = ts.parseJsonConfigFileContent(config, parseConfigHost, testDirectory).options;
}
var results = { directory: testDirectory, results: {} };
var _loop_1 = function (fileToLint) {
var isEncodingRule = path.basename(testDirectory) === "encoding";
var fileBasename = path.basename(fileToLint, MARKUP_FILE_EXTENSION);
var fileCompileName = fileBasename.replace(/\.lint$/, "");
var fileText = isEncodingRule ? utils_1.readBufferWithDetectedEncoding(fs.readFileSync(fileToLint)) : fs.readFileSync(fileToLint, "utf-8");
var tsVersionRequirement = parse.getTypescriptVersionRequirement(fileText);
if (tsVersionRequirement !== undefined) {
var tsVersion = new semver.SemVer(ts.version);
// remove prerelease suffix when matching to allow testing with nightly builds
if (!semver.satisfies(tsVersion.major + "." + tsVersion.minor + "." + tsVersion.patch, tsVersionRequirement)) {
results.results[fileToLint] = {
requirement: tsVersionRequirement,
skipped: true,
};
return "continue";
}
// remove the first line from the file before continuing
var lineBreak = fileText.search(/\n/);
fileText = lineBreak === -1 ? "" : fileText.substr(lineBreak + 1);
}
var fileTextWithoutMarkup = parse.removeErrorMarkup(fileText);
var errorsFromMarkup = parse.parseErrorsFromMarkup(fileText);
var program = void 0;
if (hasConfig) {
var compilerHost = {
fileExists: function () { return true; },
getCanonicalFileName: function (filename) { return filename; },
getCurrentDirectory: function () { return ""; },
getDefaultLibFileName: function () { return ts.getDefaultLibFileName(compilerOptions); },
getDirectories: function (_path) { return []; },
getNewLine: function () { return "\n"; },
getSourceFile: function (filenameToGet) {
var target = compilerOptions.target === undefined ? ts.ScriptTarget.ES5 : compilerOptions.target;
if (filenameToGet === ts.getDefaultLibFileName(compilerOptions)) {
var fileContent = fs.readFileSync(ts.getDefaultLibFilePath(compilerOptions), "utf8");
return ts.createSourceFile(filenameToGet, fileContent, target);
}
else if (filenameToGet === fileCompileName) {
return ts.createSourceFile(fileBasename, fileTextWithoutMarkup, target, true);
}
else if (fs.existsSync(path.resolve(path.dirname(fileToLint), filenameToGet))) {
var text = fs.readFileSync(path.resolve(path.dirname(fileToLint), filenameToGet), "utf8");
return ts.createSourceFile(filenameToGet, text, target, true);
}
throw new Error("Couldn't get source file '" + filenameToGet + "'");
},
readFile: function (x) { return x; },
useCaseSensitiveFileNames: function () { return true; },
writeFile: function () { return null; },
};
program = ts.createProgram([fileCompileName], compilerOptions, compilerHost);
}
var lintOptions = {
fix: false,
formatter: "prose",
formattersDirectory: "",
rulesDirectory: rulesDirectory,
};
var linter = new Linter(lintOptions, program);
// Need to use the true path (ending in '.lint') for "encoding" rule so that it can read the file.
linter.lint(isEncodingRule ? fileToLint : fileBasename, fileTextWithoutMarkup, tslintConfig);
var failures = linter.getResult().failures;
var errorsFromLinter = failures.map(function (failure) {
var startLineAndCharacter = failure.getStartPosition().getLineAndCharacter();
var endLineAndCharacter = failure.getEndPosition().getLineAndCharacter();
return {
endPos: {
col: endLineAndCharacter.character,
line: endLineAndCharacter.line,
},
message: failure.getFailure(),
startPos: {
col: startLineAndCharacter.character,
line: startLineAndCharacter.line,
},
};
});
// test against fixed files
var fixedFileText = "";
var newFileText = "";
try {
var fixedFile = fileToLint.replace(/\.lint$/, FIXES_FILE_EXTENSION);
var stat = fs.statSync(fixedFile);
if (stat.isFile()) {
fixedFileText = fs.readFileSync(fixedFile, "utf8");
var fixes = utils_1.mapDefined(failures, function (f) { return f.getFix(); });
newFileText = rule_1.Replacement.applyFixes(fileTextWithoutMarkup, fixes);
}
}
catch (e) {
fixedFileText = "";
newFileText = "";
}
results.results[fileToLint] = {
errorsFromLinter: errorsFromLinter,
errorsFromMarkup: errorsFromMarkup,
fixesFromLinter: newFileText,
fixesFromMarkup: fixedFileText,
markupFromLinter: parse.createMarkupFromErrors(fileTextWithoutMarkup, errorsFromMarkup),
markupFromMarkup: parse.createMarkupFromErrors(fileTextWithoutMarkup, errorsFromLinter),
skipped: false,
};
};
for (var _i = 0, filesToLint_1 = filesToLint; _i < filesToLint_1.length; _i++) {
var fileToLint = filesToLint_1[_i];
_loop_1(fileToLint);
}
return results;
}
exports.runTest = runTest;
function consoleTestResultsHandler(testResults) {
var didAllTestsPass = true;
for (var _i = 0, testResults_1 = testResults; _i < testResults_1.length; _i++) {
var testResult = testResults_1[_i];
if (!consoleTestResultHandler(testResult)) {
didAllTestsPass = false;
}
}
return didAllTestsPass;
}
exports.consoleTestResultsHandler = consoleTestResultsHandler;
function consoleTestResultHandler(testResult) {
var didAllTestsPass = true;
for (var _i = 0, _a = Object.keys(testResult.results); _i < _a.length; _i++) {
var fileName = _a[_i];
var results = testResult.results[fileName];
process.stdout.write(fileName + ":");
/* tslint:disable:no-console */
if (results.skipped) {
console.log(colors.yellow(" Skipped, requires typescript " + results.requirement));
}
else {
var markupDiffResults = diff.diffLines(results.markupFromMarkup, results.markupFromLinter);
var fixesDiffResults = diff.diffLines(results.fixesFromLinter, results.fixesFromMarkup);
var didMarkupTestPass = !markupDiffResults.some(function (diff) { return diff.added === true || diff.removed === true; });
var didFixesTestPass = !fixesDiffResults.some(function (diff) { return diff.added === true || diff.removed === true; });
if (didMarkupTestPass && didFixesTestPass) {
console.log(colors.green(" Passed"));
}
else {
console.log(colors.red(" Failed!"));
didAllTestsPass = false;
if (!didMarkupTestPass) {
displayDiffResults(markupDiffResults, MARKUP_FILE_EXTENSION);
}
if (!didFixesTestPass) {
displayDiffResults(fixesDiffResults, FIXES_FILE_EXTENSION);
}
}
}
/* tslint:enable:no-console */
}
return didAllTestsPass;
}
exports.consoleTestResultHandler = consoleTestResultHandler;
function displayDiffResults(diffResults, extension) {
/* tslint:disable:no-console */
console.log(colors.green("Expected (from " + extension + " file)"));
console.log(colors.red("Actual (from TSLint)"));
for (var _i = 0, diffResults_1 = diffResults; _i < diffResults_1.length; _i++) {
var diffResult = diffResults_1[_i];
var color = colors.grey;
if (diffResult.added === true) {
color = colors.green.underline;
}
else if (diffResult.removed === true) {
color = colors.red.underline;
}
process.stdout.write(color(diffResult.value));
}
/* tslint:enable:no-console */
}
|