-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlinter.ts
More file actions
288 lines (231 loc) · 8.47 KB
/
linter.ts
File metadata and controls
288 lines (231 loc) · 8.47 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import anyTest, {TestFn} from "ava";
import sinonGlobal, {SinonStub} from "sinon";
import path from "node:path";
import {fileURLToPath} from "node:url";
import {
createTestsForFixtures, assertExpectedLintResults,
esmockDeprecationText, preprocessLintResultsForSnapshot,
} from "./_linterHelper.js";
import {LinterOptions, LintResult} from "../../../src/linter/LinterContext.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const fixturesBasePath = path.join(__dirname, "..", "..", "fixtures", "linter");
const fixturesGeneralPath = path.join(fixturesBasePath, "general");
const fixturesProjectsPath = path.join(fixturesBasePath, "projects");
const test = anyTest as TestFn<{
sinon: sinonGlobal.SinonSandbox;
lintProject: SinonStub<[LinterOptions], Promise<LintResult[]>>;
}>;
test.before(async (t) => {
t.context.sinon = sinonGlobal.createSandbox();
const {lintModule: {lintProject}} = await esmockDeprecationText();
t.context.lintProject = lintProject;
});
test.after.always((t) => {
t.context.sinon.restore();
});
// Define tests for reach file in the fixtures/linter/general directory
createTestsForFixtures(fixturesGeneralPath);
// Test project fixtures individually
test.serial("lint: All files of com.ui5.troublesome.app", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app");
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: [],
coverage: true,
details: true,
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: Some files of com.ui5.troublesome.app (without details / coverage)", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app");
const filePaths = [
// Minimatch requires POSIX
path.posix.join("webapp", "controller", "BaseController.js"),
path.posix.join("webapp", "controller", "App.controller.js"),
path.posix.join("webapp", "Component.js"),
];
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: filePaths,
});
assertExpectedLintResults(t, res, projectPath, [
path.join("webapp", "model", "models.js"),
// Comparing files requires platform specific separators,
// so, translating POSIX to platform specific.
...filePaths.map((filename) => filename.replaceAll("/", path.sep)),
]);
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: Only /webapp folder from com.ui5.troublesome.app (without details / coverage)", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app");
const filePaths = [
// Minimatch requires POSIX
"webapp/",
];
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: filePaths,
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: One file of com.ui5.troublesome.app (without details / coverage)", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app");
const filePaths = [
// Minimatch requires POSIX
path.posix.join("webapp", "controller", "App.controller.js"),
];
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: filePaths,
});
assertExpectedLintResults(t, res, projectPath, [
// Comparing files requires platform specific separators,
// so, translating POSIX to platform specific.
...filePaths.map((filename) => filename.replaceAll("/", path.sep)),
]);
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: com.ui5.troublesome.app with unmatched patterns", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app");
const {lintProject} = t.context;
await t.throwsAsync(lintProject({
rootDir: projectPath,
configPath: "ui5lint.config.unmatched-patterns.mjs",
}), {
message: `Specified file patterns 'unmatched-pattern1', ` +
`'unmatched-pattern2', 'unmatched-pattern3' did not match any resource`,
});
});
test.serial("lint: com.ui5.troublesome.app with unmatched files", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app");
const {lintProject} = t.context;
const res = await lintProject({
filePatterns: ["webapp/i18n/*"],
rootDir: projectPath,
});
// TODO: when logging is implemented, check for log messages.
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: com.ui5.troublesome.app with files property in ui5lint.config", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app");
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
configPath: "ui5lint.config.matched-patterns.mjs",
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: All files of library.with.custom.paths", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "library.with.custom.paths");
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: [],
coverage: true,
details: true,
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: Ignore files from library.with.custom.paths", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "library.with.custom.paths");
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: [],
coverage: true,
details: true,
ignorePatterns: [
"src/",
"!src/main/",
"ui5.yaml",
],
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: All files of library with sap.f namespace", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "sap.f");
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: [],
coverage: true,
details: true,
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: All files of mocked minimal sap.ui.core library", async (t) => {
// This sap.ui.core library contains a minimal version of the base classes of UI5
// so that the linter can be tested with having them available in the project instead
// of just having the type definitions.
const projectPath = path.join(fixturesProjectsPath, "sap.ui.core");
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: [],
coverage: true,
details: true,
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: File out of the namespace of sap.ui.core", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "sap.ui.core");
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: ["src/ui5loader.js"],
coverage: true,
details: true,
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: All files of library with sap.ui.suite namespace", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "sap.ui.suite");
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: [],
coverage: true,
details: true,
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: All files of com.ui5.troublesome.app with custom config", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app");
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: [],
coverage: true,
details: true,
configPath: "./ui5lint-custom.config.cjs",
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: com.ui5.troublesome.app with custom UI5 config", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app");
const {lintProject} = t.context;
const res = await lintProject({
rootDir: projectPath,
filePatterns: [],
coverage: true,
details: true,
ui5Config: "./configs/ui5-custom.yaml",
});
t.snapshot(preprocessLintResultsForSnapshot(res));
});
test.serial("lint: com.ui5.troublesome.app with custom UI5 config which does NOT exist", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app");
const {lintProject} = t.context;
const ui5Config = "./configs/ui5-DOES-NOT-EXIST.yaml";
await t.throwsAsync(lintProject({
rootDir: projectPath,
filePatterns: [],
coverage: true,
details: true,
ui5Config,
}), {message: `Unable to find UI5 config file '${ui5Config}'`});
});