-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathide.test.ts
More file actions
269 lines (227 loc) · 11.6 KB
/
ide.test.ts
File metadata and controls
269 lines (227 loc) · 11.6 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
/*
* Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import { expect } from 'chai';
import * as fs from 'fs/promises';
import * as path from 'path';
import { TextEditor, ViewSection, VSBrowser, WebDriver, Workbench, until } from 'vscode-extension-tester';
import { Delays, IDEOperations, ignoreFails, printRascalOutputOnFailure, sleep, TestWorkspace } from './utils';
const protectFiles = [TestWorkspace.mainFile, TestWorkspace.libFile, TestWorkspace.libCallFile, TestWorkspace.manifest];
describe('IDE', function () {
let browser: VSBrowser;
let driver: WebDriver;
let bench: Workbench;
let ide: IDEOperations;
const originalFiles = new Map<string, Buffer>();
this.timeout(Delays.extremelySlow * 2);
printRascalOutputOnFailure('Rascal MPL');
before(async () => {
browser = VSBrowser.instance;
driver = browser.driver;
bench = new Workbench();
await browser.waitForWorkbench();
ide = new IDEOperations(browser);
await ide.load();
// trigger rascal type checker to be sure
for (const f of protectFiles) {
originalFiles.set(f, await fs.readFile(f));
}
await makeSureRascalModulesAreLoaded();
});
beforeEach(async function () {
if (this.test?.title) {
await ide.screenshot("IDE-" + this.test?.title);
}
});
afterEach(async function () {
if (this.test?.title) {
await ide.screenshot("IDE-" + this.test?.title);
}
await ide.cleanup();
for (const [f, b] of originalFiles) {
await fs.writeFile(f, b);
}
});
async function makeSureRascalModulesAreLoaded(delay = Delays.verySlow) {
try {
await ide.openModule(TestWorkspace.mainFile);
let statusBarSeen = false;
const checkRascalStatus = ide.statusContains("Loading Rascal");
for (let tries = 0; tries < 10 && !statusBarSeen; tries++) {
if (await checkRascalStatus()) {
statusBarSeen = true;
break;
}
await sleep(delay / 80);
}
if (statusBarSeen) {
console.log("Waiting for startup of rascal core");
for (let tries = 0; tries < 70; tries++) {
if (!await checkRascalStatus()) {
return;
}
await sleep(delay / 80);
}
console.log("*** warning, loading rascal-core is still running, but we will continue anyway");
}
}
finally {
await ide.cleanup();
}
}
function waitForActiveEditor(title: string, timeout: number, message: string) {
return driver.wait(async () =>
(await (await bench.getEditorView().getActiveTab())?.getTitle()) === title, timeout, message);
}
it("has syntax highlighting and parsing errors", async function () {
const editor = await ide.openModule(TestWorkspace.mainFile);
await ide.hasSyntaxHighlighting(editor, Delays.slow);
await editor.setTextAtLine(1, "this should not parse");
await ide.hasErrorSquiggly(editor);
}).retries(2);
it("error recovery works", async function() {
const editor = await ide.openModule(TestWorkspace.mainFile);
await ide.hasSyntaxHighlighting(editor);
// Introduce two parse errors
await editor.setTextAtLine(2, "1 2 3");
await editor.setTextAtLine(4, "1 2 3");
await ide.hasRecoveredErrors(editor, 2, Delays.slow);
await ide.hasSyntaxHighlighting(editor);
});
function triggerTypeChecker(editor: TextEditor, tplFile : string, waitForFinish = false) {
return ide.triggerTypeChecker(editor, {tplFile : tplFile, waitForFinish: waitForFinish });
}
it("save runs type checker", async function () {
const editor = await ide.openModule(TestWorkspace.mainFile);
await triggerTypeChecker(editor, TestWorkspace.mainFileTpl, true);
});
it("type checker runs on dependencies", async() => {
const editor = await ide.openModule(TestWorkspace.libCallFile);
await triggerTypeChecker(editor, TestWorkspace.libFileTpl, true);
});
it("go to definition works", async () => {
const selectId = "fileName";
const editor = await ide.openModule(TestWorkspace.mainFile);
await triggerTypeChecker(editor, TestWorkspace.mainFileTpl, true);
await editor.selectText("println");
await bench.executeCommand("Go to Definition");
await waitForActiveEditor("IO.rsc", Delays.extremelySlow, "IO.rsc should be opened for println");
// Warning: changes to `IO` might break the lookups/locations from here on.
// Update the test expectations when this is the case.
await editor.selectText(selectId, 6);
const defLoc = await editor.getCoordinates();
await editor.selectText(selectId, 7);
const useLoc = await editor.getCoordinates();
expect(useLoc).not.to.deep.equal(defLoc, "Expected cursor to be elsewhere - def and use location are equal");
await bench.executeCommand("Go to Definition");
await driver.wait(async () => {
const jumpLoc = await editor.getCoordinates();
return jumpLoc[0] === defLoc[0] // Jump to def line
&& jumpLoc[1] === defLoc[1] - selectId.length; // Jump to left side of def
}, Delays.normal, "Expected cursor to jump to the definition");
});
it("go to definition works across projects", async () => {
const editor = await ide.openModule(TestWorkspace.libCallFile);
await triggerTypeChecker(editor, TestWorkspace.libCallFileTpl, true);
await editor.selectText("fib");
await bench.executeCommand("Go to Definition");
await waitForActiveEditor(path.basename(TestWorkspace.libFile), Delays.slow, "Lib.rsc should be opened for fib");
});
it("outline works", async () => {
const editor = await ide.openModule(TestWorkspace.mainFile);
await editor.moveCursor(1,1);
const explorer = await (await bench.getActivityBar().getViewControl("Explorer"))!.openView();
const outline = await driver.wait(() => explorer.getContent().getSection("Outline"), Delays.normal) as ViewSection;
await outline.expand();
const mainItem = await driver.wait(async() => ignoreFails(outline.findItem("main()", 0)), Delays.slow, "Main function should show in the outline");
await driver.wait(async () => {
await driver.actions().doubleClick(mainItem!).perform();
return (await editor.getCoordinates())[0] === 5;
}, Delays.normal, "Cursor should have moved to line that contains the println function");
});
it ("rename works", async() => {
const editor = await ide.openModule(TestWorkspace.libFile);
await editor.moveCursor(7, 15);
// Before moving, check that Rascal is really loaded
const checkRascalStatus = ide.statusContains("Loading Rascal");
await driver.wait(async () => !(await checkRascalStatus()), Delays.extremelySlow, "Rascal evaluators have not finished loading");
await ide.renameSymbol(editor, bench, "i");
await driver.wait(() => (editor.isDirty()), Delays.extremelySlow, "Rename should have resulted in changes in the editor");
const editorText = await editor.getText();
expect(editorText).to.contain("int i");
expect(editorText).to.contain("i < 2");
expect(editorText).to.contain("i - 1");
expect(editorText).to.contain("i -2");
});
it("renaming files works", async() => {
const newDir = path.join(TestWorkspace.libProject, "src", "main", "rascal", "lib");
await fs.mkdir(newDir, {recursive: true});
// Open the lib file before moving it, so we have the editor ready to inspect afterwards
const libFile = await ide.openModule(TestWorkspace.libFile);
// Before moving, check that Rascal is really loaded
const checkRascalStatus = ide.statusContains("Loading Rascal");
await driver.wait(async () => !(await checkRascalStatus()), Delays.extremelySlow, "Rascal evaluators have not finished loading");
await ide.moveFile("Lib.rsc", "lib", bench);
await driver.wait(async() => {
const text = await libFile.getText();
return text.indexOf("module lib::Lib") !== -1;
}, Delays.extremelySlow, "Module name should have changed to `lib::Lib`", Delays.normal);
const callFile = await ide.openModule(TestWorkspace.libCallFile);
await driver.wait(async() => {
const text = await callFile.getText();
return text.indexOf("import lib::Lib") !== -1;
}, Delays.extremelySlow, "Import should have changed to `lib::Lib`", Delays.normal);
await fs.rm(newDir, {recursive: true, force: true});
});
it("code actions work", async() => {
const editor = await ide.openModule(TestWorkspace.libCallFile);
await editor.moveCursor(1,8); // in the module name
try {
await ide.triggerFirstCodeAction(editor, 'Add missing license header');
await ide.assertLineBecomes(editor, 1, "@license{", "license header should have been added", Delays.extremelySlow);
}
finally {
await ide.revertOpenChanges();
}
});
it("editor contents used for open files", async() => {
const importerEditor = await ide.openModule(TestWorkspace.importerFile);
const importeeEditor = await ide.openModule(TestWorkspace.importeeFile);
await importeeEditor.typeTextAt(3, 1, "public str foo;");
await ide.openModule(TestWorkspace.importerFile);
await ide.triggerTypeChecker(importerEditor, {waitForFinish : true});
await ide.hasErrorSquiggly(importerEditor);
});
it("errors in manifest detected", async() => {
const editor = await ide.openModule(TestWorkspace.manifest);
await editor.setTextAtLine(2, "Project-Name: foobar");
await editor.save();
const element = await ide.hasErrorSquiggly(editor);
await editor.setTextAtLine(2, "Project-Name: test-project");
await editor.save();
await driver.wait(until.stalenessOf(element), Delays.verySlow, "Error did not disapear");
});
});