Skip to content

Commit 3c25b67

Browse files
committed
faster tests
1 parent 91b3371 commit 3c25b67

9 files changed

Lines changed: 70 additions & 33 deletions

src/test/extension.autocomplete.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ suite('Autocomplete', () => {
4444
textDocument = document;
4545
return vscode.window.showTextDocument(textDocument);
4646
}).then(editor => {
47+
assert(vscode.window.activeTextEditor, 'No active editor');
4748
textEditor = editor;
4849
const position = new vscode.Position(3, 10);
4950
return vscode.commands.executeCommand('vscode.executeCompletionItemProvider', textDocument.uri, position);
@@ -61,6 +62,7 @@ suite('Autocomplete', () => {
6162
textDocument = document;
6263
return vscode.window.showTextDocument(textDocument);
6364
}).then(editor => {
65+
assert(vscode.window.activeTextEditor, 'No active editor');
6466
textEditor = editor;
6567
const position = new vscode.Position(30, 4);
6668
return vscode.commands.executeCommand('vscode.executeCompletionItemProvider', textDocument.uri, position);
@@ -77,6 +79,7 @@ suite('Autocomplete', () => {
7779
textDocument = document;
7880
return vscode.window.showTextDocument(textDocument);
7981
}).then(editor => {
82+
assert(vscode.window.activeTextEditor, 'No active editor');
8083
textEditor = editor;
8184
const position = new vscode.Position(25, 4);
8285
return vscode.commands.executeCommand('vscode.executeCompletionItemProvider', textDocument.uri, position);
@@ -94,6 +97,7 @@ suite('Autocomplete', () => {
9497
textDocument = document;
9598
return vscode.window.showTextDocument(textDocument);
9699
}).then(editor => {
100+
assert(vscode.window.activeTextEditor, 'No active editor');
97101
textEditor = editor;
98102
const position = new vscode.Position(1, 5);
99103
return vscode.commands.executeCommand('vscode.executeCompletionItemProvider', textDocument.uri, position);
@@ -130,6 +134,7 @@ suite('Code Definition', () => {
130134
textDocument = document;
131135
return vscode.window.showTextDocument(textDocument);
132136
}).then(editor => {
137+
assert(vscode.window.activeTextEditor, 'No active editor');
133138
textEditor = editor;
134139
const position = new vscode.Position(30, 5);
135140
return vscode.commands.executeCommand('vscode.executeDefinitionProvider', textDocument.uri, position);
@@ -147,6 +152,7 @@ suite('Code Definition', () => {
147152
textDocument = document;
148153
return vscode.window.showTextDocument(textDocument);
149154
}).then(editor => {
155+
assert(vscode.window.activeTextEditor, 'No active editor');
150156
textEditor = editor;
151157
const position = new vscode.Position(1, 5);
152158
return vscode.commands.executeCommand('vscode.executeDefinitionProvider', textDocument.uri, position);
@@ -165,6 +171,7 @@ suite('Code Definition', () => {
165171
textDocument = document;
166172
return vscode.window.showTextDocument(textDocument);
167173
}).then(editor => {
174+
assert(vscode.window.activeTextEditor, 'No active editor');
168175
textEditor = editor;
169176
const position = new vscode.Position(25, 6);
170177
return vscode.commands.executeCommand('vscode.executeDefinitionProvider', textDocument.uri, position);
@@ -183,6 +190,7 @@ suite('Code Definition', () => {
183190
textDocument = document;
184191
return vscode.window.showTextDocument(textDocument);
185192
}).then(editor => {
193+
assert(vscode.window.activeTextEditor, 'No active editor');
186194
textEditor = editor;
187195
const position = new vscode.Position(1, 11);
188196
return vscode.commands.executeCommand('vscode.executeDefinitionProvider', textDocument.uri, position);
@@ -217,6 +225,7 @@ suite('Hover Definition', () => {
217225
textDocument = document;
218226
return vscode.window.showTextDocument(textDocument);
219227
}).then(editor => {
228+
assert(vscode.window.activeTextEditor, 'No active editor');
220229
textEditor = editor;
221230
const position = new vscode.Position(30, 5);
222231
return vscode.commands.executeCommand('vscode.executeHoverProvider', textDocument.uri, position);
@@ -236,6 +245,7 @@ suite('Hover Definition', () => {
236245
textDocument = document;
237246
return vscode.window.showTextDocument(textDocument);
238247
}).then(editor => {
248+
assert(vscode.window.activeTextEditor, 'No active editor');
239249
textEditor = editor;
240250
const position = new vscode.Position(1, 12);
241251
return vscode.commands.executeCommand('vscode.executeHoverProvider', textDocument.uri, position);
@@ -255,6 +265,7 @@ suite('Hover Definition', () => {
255265
textDocument = document;
256266
return vscode.window.showTextDocument(textDocument);
257267
}).then(editor => {
268+
assert(vscode.window.activeTextEditor, 'No active editor');
258269
textEditor = editor;
259270
const position = new vscode.Position(25, 6);
260271
return vscode.commands.executeCommand('vscode.executeHoverProvider', textDocument.uri, position);
@@ -275,6 +286,7 @@ suite('Hover Definition', () => {
275286
textDocument = document;
276287
return vscode.window.showTextDocument(textDocument);
277288
}).then(editor => {
289+
assert(vscode.window.activeTextEditor, 'No active editor');
278290
textEditor = editor;
279291
const position = new vscode.Position(1, 11);
280292
return vscode.commands.executeCommand('vscode.executeHoverProvider', textDocument.uri, position);

src/test/extension.common.helpers.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ suite('Deferred', () => {
2020
def.promise.then(value => {
2121
assert.equal(value, valueToSent);
2222
assert.equal(def.resolved, true, 'resolved property value is not `true`');
23-
done();
24-
}).catch(done);
23+
}).then(done).catch(done);
2524

2625
assert.equal(def.resolved, false, 'Promise is resolved even when it should not be');
2726
assert.equal(def.rejected, false, 'Promise is rejected even when it should not be');

src/test/extension.common.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ suite('ChildProc', () => {
2323
test('Standard Response', done => {
2424
execPythonFile('python', ['-c', 'print(1)'], __dirname, false).then(data => {
2525
assert.ok(data === '1' + EOL);
26-
}).then(done, done);
26+
}).then(done).catch(done);
2727
});
2828
test('Error Response', done => {
2929
const def = createDeferred<any>();
@@ -44,7 +44,7 @@ suite('ChildProc', () => {
4444
execPythonFile('python', ['-c', 'print(1)'], __dirname, false, handleOutput).then(() => {
4545
assert.equal(output.length, 1, 'Ouput length incorrect');
4646
assert.equal(output[0], '1' + EOL, 'Ouput value incorrect');
47-
}).then(done, done);
47+
}).then(done).catch(done);
4848
});
4949

5050
test('Stream Stdout with Threads', done => {
@@ -56,7 +56,7 @@ suite('ChildProc', () => {
5656
assert.equal(output.length, 2, 'Ouput length incorrect');
5757
assert.equal(output[0], '1' + EOL, 'First Ouput value incorrect');
5858
assert.equal(output[1], '2' + EOL, 'Second Ouput value incorrect');
59-
}).then(done, done);
59+
}).then(done).catch(done);
6060
});
6161

6262
test('Kill', done => {

src/test/extension.format.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ suite('Formatting', () => {
4646
return Promise.all<string>([yapf, autoPep8]).then(formattedResults => {
4747
formattedYapf = formattedResults[0];
4848
formattedAutoPep8 = formattedResults[1];
49-
}).then(() => {
50-
done();
51-
}, done);
52-
}, done);
49+
}).then(() => { });
50+
}).then(done).catch(done);
5351
});
5452
suiteTeardown(done => {
5553
closeActiveWindows().then(done, done);
@@ -65,6 +63,7 @@ suite('Formatting', () => {
6563
textDocument = document;
6664
return vscode.window.showTextDocument(textDocument);
6765
}).then(editor => {
66+
assert(vscode.window.activeTextEditor, 'No active editor');
6867
textEditor = editor;
6968
return formatter.formatDocument(textDocument, null, null);
7069
}).then(edits => {
@@ -93,6 +92,7 @@ suite('Formatting', () => {
9392
textDocument = document;
9493
return vscode.window.showTextDocument(textDocument);
9594
}).then(editor => {
95+
assert(vscode.window.activeTextEditor, 'No active editor');
9696
return editor.edit(editBuilder => {
9797
editBuilder.insert(new vscode.Position(0, 0), '#\n');
9898
});

src/test/extension.refactor.extract.method.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ suite('Method Extraction', () => {
168168
textDocument = document;
169169
return vscode.window.showTextDocument(textDocument);
170170
}).then(editor => {
171+
assert(vscode.window.activeTextEditor, 'No active editor');
171172
editor.selections = [new vscode.Selection(rangeOfTextToExtract.start, rangeOfTextToExtract.end)];
172173
editor.selection = new vscode.Selection(rangeOfTextToExtract.start, rangeOfTextToExtract.end);
173174
textEditor = editor;

src/test/extension.refactor.extract.var.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ suite('Variable Extraction', () => {
100100
});
101101
});
102102
suiteTeardown(done => {
103-
closeActiveWindows().then(done);
103+
closeActiveWindows().then(done).catch(done);
104104
});
105105
setup(() => {
106106
if (fs.existsSync(refactorTargetFile)) {
@@ -109,7 +109,7 @@ suite('Variable Extraction', () => {
109109
fs.copySync(refactorSourceFile, refactorTargetFile, { clobber: true });
110110
});
111111
teardown(done => {
112-
closeActiveWindows().then(done);
112+
closeActiveWindows().then(done).catch(done);
113113
});
114114

115115
function testingVariableExtraction(shouldError: boolean, pythonSettings: settings.IPythonSettings, startPos: Position, endPos: Position) {
@@ -170,6 +170,7 @@ suite('Variable Extraction', () => {
170170
textDocument = document;
171171
return vscode.window.showTextDocument(textDocument);
172172
}).then(editor => {
173+
assert(vscode.window.activeTextEditor, 'No active editor');
173174
editor.selections = [new vscode.Selection(rangeOfTextToExtract.start, rangeOfTextToExtract.end)];
174175
editor.selection = new vscode.Selection(rangeOfTextToExtract.start, rangeOfTextToExtract.end);
175176
textEditor = editor;

src/test/extension.sort.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ suite('Sorting', () => {
3636
fs.writeFileSync(fileToFormatWithConfig, fs.readFileSync(originalFileToFormatWithConfig));
3737
fs.writeFileSync(fileToFormatWithConfig1, fs.readFileSync(originalFileToFormatWithConfig1));
3838
fs.writeFileSync(fileToFormatWithoutConfig, fs.readFileSync(originalFileToFormatWithoutConfig));
39-
closeActiveWindows().then(done, done);
39+
closeActiveWindows().then(done).catch(done);
4040
});
4141
setup(done => {
4242
pythonSettings.sortImports.args = [];
4343
fs.writeFileSync(fileToFormatWithConfig, fs.readFileSync(originalFileToFormatWithConfig));
4444
fs.writeFileSync(fileToFormatWithoutConfig, fs.readFileSync(originalFileToFormatWithoutConfig));
4545
fs.writeFileSync(fileToFormatWithConfig1, fs.readFileSync(originalFileToFormatWithConfig1));
46-
closeActiveWindows().then(done, done);
46+
closeActiveWindows().then(done).catch(done);
4747
});
4848

4949
test('Without Config', done => {
@@ -54,6 +54,7 @@ suite('Sorting', () => {
5454
return vscode.window.showTextDocument(textDocument);
5555
}).then(editor => {
5656
textEditor = editor;
57+
assert(vscode.window.activeTextEditor, 'No active editor');
5758
const sorter = new PythonImportSortProvider();
5859
return sorter.sortImports(extensionDir, textDocument);
5960
}).then(edits => {
@@ -73,6 +74,7 @@ suite('Sorting', () => {
7374
originalContent = textDocument.getText();
7475
return vscode.window.showTextDocument(textDocument);
7576
}).then(editor => {
77+
assert(vscode.window.activeTextEditor, 'No active editor');
7678
textEditor = editor;
7779
return vscode.commands.executeCommand('python.sortImports');
7880
}).then(() => {
@@ -87,6 +89,7 @@ suite('Sorting', () => {
8789
textDocument = document;
8890
return vscode.window.showTextDocument(textDocument);
8991
}).then(editor => {
92+
assert(vscode.window.activeTextEditor, 'No active editor');
9093
textEditor = editor;
9194
const sorter = new PythonImportSortProvider();
9295
return sorter.sortImports(extensionDir, textDocument);
@@ -105,6 +108,7 @@ suite('Sorting', () => {
105108
originalContent = document.getText();
106109
return vscode.window.showTextDocument(textDocument);
107110
}).then(editor => {
111+
assert(vscode.window.activeTextEditor, 'No active editor');
108112
textEditor = editor;
109113
return vscode.commands.executeCommand('python.sortImports');
110114
}).then(() => {
@@ -122,6 +126,7 @@ suite('Sorting', () => {
122126
textDocument = document;
123127
return vscode.window.showTextDocument(textDocument);
124128
}).then(editor => {
129+
assert(vscode.window.activeTextEditor, 'No active editor');
125130
textEditor = editor;
126131
return editor.edit(editor => {
127132
editor.insert(new vscode.Position(0, 0), 'from third_party import lib0' + EOL);
@@ -147,6 +152,7 @@ suite('Sorting', () => {
147152
textDocument = document;
148153
return vscode.window.showTextDocument(textDocument);
149154
}).then(editor => {
155+
assert(vscode.window.activeTextEditor, 'No active editor');
150156
textEditor = editor;
151157
return editor.edit(editor => {
152158
editor.insert(new vscode.Position(0, 0), 'from third_party import lib0' + EOL);

src/test/initialize.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,39 @@ import * as vscode from "vscode";
1616
import * as path from "path";
1717
let dummyPythonFile = path.join(__dirname, "..", "..", "src", "test", "pythonFiles", "dummy.py");
1818

19-
export function initialize(): Thenable<any> {
20-
return vscode.workspace.openTextDocument(dummyPythonFile);
19+
export function initialize(): Promise<any> {
20+
return new Promise<any>((resolve, reject) => {
21+
vscode.workspace.openTextDocument(dummyPythonFile).then(resolve, reject);
22+
});
2123
}
2224

23-
export function closeActiveWindows(counter: number = 0): Thenable<any> {
24-
if (counter >= 10 || !vscode.window.activeTextEditor) {
25-
return Promise.resolve();
26-
}
27-
return new Promise<any>(resolve => {
28-
setTimeout(function () {
29-
if (!vscode.window.activeTextEditor) {
30-
setTimeout(function () {
31-
resolve();
32-
}, 1000);
25+
export async function closeActiveWindows(): Promise<any> {
26+
// https://github.com/Microsoft/vscode/blob/master/extensions/vscode-api-tests/src/utils.ts
27+
return new Promise((c, e) => {
28+
if (vscode.window.visibleTextEditors.length === 0) {
29+
return c();
30+
}
31+
32+
// TODO: the visibleTextEditors variable doesn't seem to be
33+
// up to date after a onDidChangeActiveTextEditor event, not
34+
// even using a setTimeout 0... so we MUST poll :(
35+
let interval = setInterval(() => {
36+
if (vscode.window.visibleTextEditors.length > 0) {
37+
return;
3338
}
3439

35-
vscode.commands.executeCommand('workbench.action.closeActiveEditor').then(() => {
36-
closeActiveWindows(counter++).then(resolve, resolve);
37-
}, ()=>{
38-
closeActiveWindows(counter++).then(resolve, resolve);
40+
clearInterval(interval);
41+
c();
42+
}, 10);
43+
44+
vscode.commands.executeCommand('workbench.action.closeAllEditors')
45+
.then(() => null, (err: any) => {
46+
clearInterval(interval);
47+
e(err);
3948
});
40-
}, 500);
49+
}).then(() => {
50+
assert.equal(vscode.window.visibleTextEditors.length, 0);
51+
assert(!vscode.window.activeTextEditor);
4152
});
4253
}
4354

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
from third_party import (lib1, lib2, lib3,
2-
lib4, lib5, lib6,
3-
lib7, lib8, lib9)
1+
from third_party import lib0
2+
from third_party import lib1
3+
from third_party import lib2
4+
from third_party import lib3
5+
from third_party import lib4
6+
from third_party import lib5
7+
from third_party import lib6
8+
from third_party import lib7
9+
from third_party import lib8
10+
from third_party import lib9

0 commit comments

Comments
 (0)