Skip to content

Commit 5d65c3f

Browse files
committed
tests for sorting
1 parent de1af2a commit 5d65c3f

5 files changed

Lines changed: 117 additions & 9 deletions

File tree

src/client/sortImports.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export function activate(context: vscode.ExtensionContext, outChannel: vscode.Ou
1212
let activeEditor = vscode.window.activeTextEditor;
1313
if (!activeEditor || activeEditor.document.languageId !== 'python') {
1414
vscode.window.showErrorMessage('Please open a Python source file to sort the imports.');
15-
return;
15+
return Promise.resolve();
1616
}
1717
if (activeEditor.document.lineCount <= 1) {
18-
return;
18+
return Promise.resolve();
1919
}
2020

2121
let delays = new telemetryHelper.Delays();
@@ -31,7 +31,7 @@ export function activate(context: vscode.ExtensionContext, outChannel: vscode.Ou
3131
}).then(resolve, reject);
3232
});
3333
}
34-
emptyLineAdded.then(() => {
34+
return emptyLineAdded.then(() => {
3535
return new sortProvider.PythonImportSortProvider().sortImports(rootDir, activeEditor.document);
3636
}).then(changes => {
3737
if (changes.length === 0) {

src/test/extension.common.test.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import * as assert from 'assert';
1111
// You can import and use all API from the 'vscode' module
1212
// as well as import your extension to test it
1313
import { execPythonFile } from '../client/common/utils';
14-
import {EOL} from 'os';
14+
import { EOL } from 'os';
1515
import { createDeferred } from '../client/common/helpers';
16+
import * as vscode from 'vscode';
1617

1718
// Defines a Mocha test suite to group tests of similar kind together
1819
suite('ChildProc', () => {
@@ -34,4 +35,47 @@ suite('ChildProc', () => {
3435

3536
def.promise.then(done).catch(done);
3637
});
38+
39+
test('Stream Stdout', done => {
40+
const output: string[] = [];
41+
function handleOutput(data: string) {
42+
output.push(data);
43+
}
44+
execPythonFile('python', ['-c', 'print(1)'], __dirname, false, handleOutput).then(() => {
45+
assert.equal(output.length, 1, 'Ouput length incorrect');
46+
assert.equal(output[0], '1' + EOL, 'Ouput value incorrect');
47+
}).then(done, done);
48+
});
49+
50+
test('Stream Stdout with Threads', done => {
51+
const output: string[] = [];
52+
function handleOutput(data: string) {
53+
output.push(data);
54+
}
55+
execPythonFile('python', ['-c', 'import sys\nprint(1)\nsys.__stdout__.flush()\nimport time\ntime.sleep(5)\nprint(2)'], __dirname, false, handleOutput).then(() => {
56+
assert.equal(output.length, 2, 'Ouput length incorrect');
57+
assert.equal(output[0], '1' + EOL, 'First Ouput value incorrect');
58+
assert.equal(output[1], '2' + EOL, 'Second Ouput value incorrect');
59+
}).then(done, done);
60+
});
61+
62+
test('Kill', done => {
63+
const def = createDeferred<any>();
64+
const output: string[] = [];
65+
function handleOutput(data: string) {
66+
output.push(data);
67+
}
68+
const cancellation = new vscode.CancellationTokenSource();
69+
execPythonFile('python', ['-c', 'import sys\nprint(1)\nsys.__stdout__.flush()\nimport time\ntime.sleep(5)\nprint(2)'], __dirname, false, handleOutput, cancellation.token).then(() => {
70+
def.reject('Should not have completed');
71+
}).catch(()=>{
72+
def.resolve();
73+
});
74+
75+
setTimeout(() => {
76+
cancellation.cancel();
77+
}, 1000);
78+
79+
def.promise.then(done).catch(done);
80+
});
3781
});

src/test/extension.sort.test.ts

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ suite('Formatting', () => {
4444
fs.writeFileSync(fileToFormatWithoutConfig, fs.readFileSync(originalFileToFormatWithoutConfig));
4545
});
4646
teardown(() => {
47+
fs.writeFileSync(fileToFormatWithConfig, fs.readFileSync(originalFileToFormatWithConfig));
48+
fs.writeFileSync(fileToFormatWithoutConfig, fs.readFileSync(originalFileToFormatWithoutConfig));
4749
if (vscode.window.activeTextEditor) {
4850
return vscode.commands.executeCommand('workbench.action.closeActiveEditor');
4951
}
@@ -59,14 +61,30 @@ suite('Formatting', () => {
5961
textEditor = editor;
6062
const sorter = new PythonImportSortProvider();
6163
return sorter.sortImports(extensionDir, textDocument);
62-
}).then(edits => {
64+
}).then(edits => {
6365
assert.equal(edits.filter(value => value.newText === EOL && value.range.isEqual(new vscode.Range(2, 0, 2, 0))).length, 1, 'EOL not found');
6466
assert.equal(edits.filter(value => value.newText === '' && value.range.isEqual(new vscode.Range(3, 0, 4, 0))).length, 1, '"" not found');
6567
assert.equal(edits.filter(value => value.newText === `from rope.base import libutils${EOL}from rope.refactor.extract import ExtractMethod, ExtractVariable${EOL}from rope.refactor.rename import Rename${EOL}` && value.range.isEqual(new vscode.Range(6, 0, 6, 0))).length, 1, 'Text not found');
6668
assert.equal(edits.filter(value => value.newText === '' && value.range.isEqual(new vscode.Range(13, 0, 18, 0))).length, 1, '"" not found');
6769
}).then(done, done);
6870
});
6971

72+
test('Without Config (via Command)', done => {
73+
let textEditor: vscode.TextEditor;
74+
let textDocument: vscode.TextDocument;
75+
let originalContent = '';
76+
return vscode.workspace.openTextDocument(fileToFormatWithoutConfig).then(document => {
77+
textDocument = document;
78+
originalContent = textDocument.getText();
79+
return vscode.window.showTextDocument(textDocument);
80+
}).then(editor => {
81+
textEditor = editor;
82+
return vscode.commands.executeCommand('python.sortImports');
83+
}).then(() => {
84+
assert.notEqual(originalContent, textDocument.getText(), 'Contents have not changed');
85+
}).then(done, done);
86+
});
87+
7088
test('With Config', done => {
7189
let textEditor: vscode.TextEditor;
7290
let textDocument: vscode.TextDocument;
@@ -83,6 +101,22 @@ suite('Formatting', () => {
83101
}).then(done, done);
84102
});
85103

104+
test('With Config (via Command)', done => {
105+
let textEditor: vscode.TextEditor;
106+
let textDocument: vscode.TextDocument;
107+
let originalContent = '';
108+
return vscode.workspace.openTextDocument(fileToFormatWithConfig).then(document => {
109+
textDocument = document;
110+
originalContent = document.getText();
111+
return vscode.window.showTextDocument(textDocument);
112+
}).then(editor => {
113+
textEditor = editor;
114+
return vscode.commands.executeCommand('python.sortImports');
115+
}).then(() => {
116+
assert.notEqual(originalContent, textDocument.getText(), 'Contents have not changed');
117+
}).then(done, done);
118+
});
119+
86120
test('With Changes and Config in Args', done => {
87121
let textEditor: vscode.TextEditor;
88122
let textDocument: vscode.TextDocument;
@@ -99,8 +133,32 @@ suite('Formatting', () => {
99133
const sorter = new PythonImportSortProvider();
100134
return sorter.sortImports(extensionDir, textDocument);
101135
}).then(edits => {
102-
const newValue = `from third_party import lib2${EOL}from third_party import lib3${EOL}from third_party import lib4${EOL}from third_party import lib5${EOL}from third_party import lib6${EOL}from third_party import lib7${EOL}from third_party import lib8${EOL}from third_party import lib9${EOL}`;
103-
assert.equal(edits.filter(value => value.newText === newValue && value.range.isEqual(new vscode.Range(1, 0, 4, 0))).length, 1, 'New Text not found');
136+
const newValue = `from third_party import lib1${EOL}from third_party import lib2${EOL}from third_party import lib3${EOL}from third_party import lib4${EOL}from third_party import lib5${EOL}from third_party import lib6${EOL}from third_party import lib7${EOL}from third_party import lib8${EOL}from third_party import lib9${EOL}`;
137+
assert.equal(edits.length, 1, 'Incorrect number of edits');
138+
assert.equal(edits[0].newText, newValue, 'New Value is not the same');
139+
assert.equal(`${edits[0].range.start.line},${edits[0].range.start.character}`, '1,0', 'Start position is not the same');
140+
assert.equal(`${edits[0].range.end.line},${edits[0].range.end.character}`, '2,0', 'End position is not the same');
141+
}).then(done, done);
142+
});
143+
144+
test('With Changes and Config in Args (via Command)', done => {
145+
let textEditor: vscode.TextEditor;
146+
let textDocument: vscode.TextDocument;
147+
let originalContent = '';
148+
pythonSettings.sortImports.args = ['-sp', path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'sorting', 'withconfig')];
149+
return vscode.workspace.openTextDocument(fileToFormatWithConfig).then(document => {
150+
textDocument = document;
151+
return vscode.window.showTextDocument(textDocument);
152+
}).then(editor => {
153+
textEditor = editor;
154+
return editor.edit(editor => {
155+
editor.insert(new vscode.Position(0, 0), 'from third_party import lib0' + EOL);
156+
});
157+
}).then(() => {
158+
originalContent = textDocument.getText();
159+
return vscode.commands.executeCommand('python.sortImports');
160+
}).then(edits => {
161+
assert.notEqual(originalContent, textDocument.getText(), 'Contents have not changed');
104162
}).then(done, done);
105163
});
106164
});

src/test/extension.unittests.nosetest.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import * as vscode from 'vscode';
1515
import { TestsToRun } from '../client/unittests/common/contracts';
1616
import * as nose from '../client/unittests/nosetest/main';
1717
import { TestResultDisplay } from '../client/unittests/display/main';
18-
18+
import * as fs from 'fs';
1919

2020
import * as path from 'path';
2121
import * as configSettings from '../client/common/configSettings';
2222

2323
let pythonSettings = configSettings.PythonSettings.getInstance();
2424

2525
const UNITTEST_TEST_FILES_PATH = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'unitests');
26+
const UNITTEST_TEST_ID_FILE_PATH = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'unitests', '.noseids');
2627
class MockOutputChannel implements vscode.OutputChannel {
2728
constructor(name: string) {
2829
this.name = name;
@@ -44,12 +45,18 @@ class MockOutputChannel implements vscode.OutputChannel {
4445

4546
suite('Unit Tests (nosetest)', () => {
4647
suiteSetup(done => {
48+
if (fs.existsSync(UNITTEST_TEST_ID_FILE_PATH)){
49+
fs.unlinkSync(UNITTEST_TEST_ID_FILE_PATH);
50+
}
4751
initialize().then(() => {
4852
pythonSettings.pythonPath = PYTHON_PATH;
4953
done();
5054
});
5155
});
5256
suiteTeardown(done => {
57+
if (fs.existsSync(UNITTEST_TEST_ID_FILE_PATH)){
58+
fs.unlinkSync(UNITTEST_TEST_ID_FILE_PATH);
59+
}
5360
done();
5461
});
5562
setup(() => {
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from third_party import lib0
21
from third_party import (lib1, lib2, lib3,
32
lib4, lib5, lib6,
43
lib7, lib8, lib9)

0 commit comments

Comments
 (0)