This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathTestHelper.ts
185 lines (169 loc) · 7 KB
/
TestHelper.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
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
import * as Lint from 'tslint';
import * as fs from 'fs';
import * as chai from 'chai';
import { Utils } from '../utils/Utils';
import * as ts from 'typescript';
/**
* Test Utilities.
*/
export namespace TestHelper {
let program: ts.Program;
/* tslint:disable:variable-name */
/**
* This setting must point to your rule .js files. 3rd party libraries may reuse this class and change value.
*/
export let RULES_DIRECTORY: string = 'dist/src/';
/**
* This setting must point to your formatter .js files. 3rd party libraries may reuse this class and change value.
*/
export let FORMATTER_DIRECTORY: string = 'customFormatters/';
/**
* You must specify an encoding for file read/writes. 3rd party libraries may reuse this class and change value.
*/
export let FILE_ENCODING: string = 'utf8';
/* tslint:enable:variable-name */
export interface FailurePosition {
character: number;
line: number;
position?: number;
}
export interface Fix {
innerStart: number;
innerLength: number;
innerText: string;
}
export interface ExpectedFailure {
ruleName: string;
name: string;
failure?: string;
ruleSeverity?: string;
endPosition?: FailurePosition;
startPosition: FailurePosition;
fix?: Fix;
}
export function assertNoViolation(ruleName: string, inputFileOrScript: string, useTypeChecker: boolean = false) {
runRuleAndEnforceAssertions(ruleName, undefined, inputFileOrScript, [], useTypeChecker);
}
export function assertNoViolationWithOptions(
ruleName: string,
options: any[] | undefined, // tslint:disable-line:no-any
inputFileOrScript: string,
useTypeChecker: boolean = false
) {
runRuleAndEnforceAssertions(ruleName, options, inputFileOrScript, [], useTypeChecker);
}
export function assertViolationsWithOptions(
ruleName: string,
options: any[] | undefined, // tslint:disable-line:no-any
inputFileOrScript: string,
expectedFailures: ExpectedFailure[],
useTypeChecker: boolean = false
) {
runRuleAndEnforceAssertions(ruleName, options, inputFileOrScript, expectedFailures, useTypeChecker);
}
export function assertViolations(
ruleName: string,
inputFileOrScript: string,
expectedFailures: ExpectedFailure[],
useTypeChecker: boolean = false
) {
runRuleAndEnforceAssertions(ruleName, undefined, inputFileOrScript, expectedFailures, useTypeChecker);
}
export function assertViolationsWithTypeChecker(ruleName: string, inputFileOrScript: string, expectedFailures: ExpectedFailure[]) {
runRuleAndEnforceAssertions(ruleName, undefined, inputFileOrScript, expectedFailures, true);
}
export function runRule(
ruleName: string,
userOptions: any[] | undefined, // tslint:disable-line:no-any
inputFileOrScript: string,
useTypeChecker: boolean = false
): Lint.LintResult {
const configuration: Lint.Configuration.IConfigurationFile = {
extends: [],
jsRules: new Map<string, Partial<Lint.IOptions>>(),
linterOptions: {},
rules: new Map<string, Partial<Lint.IOptions>>(),
rulesDirectory: []
};
if (userOptions !== undefined && userOptions.length > 0) {
//options like `[4, 'something', false]` were passed, so prepend `true` to make the array like `[true, 4, 'something', false]`
configuration.rules.set(ruleName, {
ruleName,
ruleArguments: userOptions
});
} else {
configuration.rules.set(ruleName, {
ruleName
});
}
const options: Lint.ILinterOptions = {
formatter: 'json',
fix: false,
rulesDirectory: RULES_DIRECTORY,
formattersDirectory: FORMATTER_DIRECTORY
};
let result: Lint.LintResult;
if (useTypeChecker) {
//program = Lint.Linter.createProgram([ ], './dist/test-data');
program = ts.createProgram([inputFileOrScript], {});
}
if (inputFileOrScript.match(/.*\.ts(x)?$/)) {
// tslint:disable-next-line non-literal-fs-path
const contents = fs.readFileSync(inputFileOrScript, FILE_ENCODING);
const linter = new Lint.Linter(options, useTypeChecker ? program : undefined);
linter.lint(inputFileOrScript, contents, configuration);
result = linter.getResult();
} else {
let filename: string;
if (inputFileOrScript.indexOf('import React') > -1) {
filename = Utils.absolutePath('file.tsx');
} else {
filename = Utils.absolutePath('file.ts');
}
const linter = new Lint.Linter(options, useTypeChecker ? program : undefined);
linter.lint(filename, inputFileOrScript, configuration);
result = linter.getResult();
}
return result;
}
function runRuleAndEnforceAssertions(
ruleName: string,
userOptions: any[] | undefined, // tslint:disable-line:no-any
inputFileOrScript: string,
expectedFailures: ExpectedFailure[],
useTypeChecker: boolean = false
) {
const lintResult: Lint.LintResult = runRule(ruleName, userOptions, inputFileOrScript, useTypeChecker);
const actualFailures: ExpectedFailure[] = JSON.parse(lintResult.output);
// All the information we need is line and character of start position. For JSON comparison
// to work, we will delete the information that we are not interested in from both actual and
// expected failures.
actualFailures.forEach(
(actual: ExpectedFailure): void => {
delete actual.startPosition.position;
delete actual.endPosition;
// Editors start counting lines and characters from 1, but tslint does it from 0.
// To make thing easier to debug, align to editor values.
actual.startPosition.line = actual.startPosition.line + 1;
actual.startPosition.character = actual.startPosition.character + 1;
}
);
expectedFailures.forEach(
(expected: ExpectedFailure): void => {
delete expected.startPosition.position;
delete expected.endPosition;
if (!expected.ruleSeverity) {
expected.ruleSeverity = 'ERROR';
}
}
);
const errorMessage = `Wrong # of failures: \n${JSON.stringify(actualFailures, undefined, 2)}`;
chai.assert.equal(actualFailures.length, expectedFailures.length, errorMessage);
expectedFailures.forEach(
(expected: ExpectedFailure, index: number): void => {
const actual = actualFailures[index];
chai.assert.deepEqual(actual, expected);
}
);
}
}