forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
120 lines (103 loc) · 4.26 KB
/
test.ts
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
/**
* @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.
*/
import * as colors from "colors";
import * as diff from "diff";
import * as fs from "fs";
import * as glob from "glob";
import * as path from "path";
import * as Linter from "./tslint";
import * as parse from "./test/parse";
import {LintError} from "./test/lintError";
const FILE_EXTENSION = ".lint";
export interface TestResult {
directory: string;
results: {
[fileName: string]: {
errorsFromMarkup: LintError[];
errorsFromLinter: LintError[];
markupFromLinter: string;
markupFromMarkup: string;
}
};
}
export function runTest(testDirectory: string, rulesDirectory?: string | string[]): TestResult {
const filesToLint = glob.sync(path.join(testDirectory, `**/*${FILE_EXTENSION}`));
const tslintConfig = Linter.findConfiguration(path.join(testDirectory, "tslint.json"), null);
const results: TestResult = { directory: testDirectory, results: {} };
for (const fileToLint of filesToLint) {
const fileBasename = path.basename(fileToLint, FILE_EXTENSION);
const fileText = fs.readFileSync(fileToLint, "utf8");
const fileTextWithoutMarkup = parse.removeErrorMarkup(fileText);
const errorsFromMarkup = parse.parseErrorsFromMarkup(fileText);
const lintOptions = {
configuration: tslintConfig,
formatter: "prose",
formattersDirectory: "",
rulesDirectory,
};
const linter = new Linter(fileBasename, fileTextWithoutMarkup, lintOptions);
const errorsFromLinter: LintError[] = linter.lint().failures.map((failure) => {
const startLineAndCharacter = failure.getStartPosition().getLineAndCharacter();
const endLineAndCharacter = failure.getEndPosition().getLineAndCharacter();
return {
endPos: {
col: endLineAndCharacter.character,
line: endLineAndCharacter.line,
},
message: failure.getFailure(),
startPos: {
col: startLineAndCharacter.character,
line: startLineAndCharacter.line,
},
};
});
results.results[fileToLint] = {
errorsFromMarkup,
errorsFromLinter,
markupFromLinter: parse.createMarkupFromErrors(fileTextWithoutMarkup, errorsFromMarkup),
markupFromMarkup: parse.createMarkupFromErrors(fileTextWithoutMarkup, errorsFromLinter),
};
}
return results;
}
export function consoleTestResultHandler(testResult: TestResult): boolean {
let didAllTestsPass = true;
for (const fileName of Object.keys(testResult.results)) {
const results = testResult.results[fileName];
process.stdout.write(`${fileName}:`);
const diffResults = diff.diffLines(results.markupFromMarkup, results.markupFromLinter);
const didTestPass = !diffResults.some((diff) => diff.added || diff.removed);
if (didTestPass) {
console.log(colors.green(" Passed"));
} else {
console.log(colors.red(" Failed!"));
console.log(colors.green(`Expected (from ${FILE_EXTENSION} file)`));
console.log(colors.red("Actual (from TSLint)"));
didAllTestsPass = false;
for (const diffResult of diffResults) {
let color = colors.grey;
if (diffResult.added) {
color = colors.green;
} else if (diffResult.removed) {
color = colors.red;
}
process.stdout.write(color(diffResult.value));
}
}
}
return didAllTestsPass;
}