forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.sort.test.ts
More file actions
168 lines (156 loc) · 9.58 KB
/
extension.sort.test.ts
File metadata and controls
168 lines (156 loc) · 9.58 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
// Note: This example test is leveraging the Mocha test framework.
// Please refer to their documentation on https://mochajs.org/ for help.
// Place this right on top
import { initialize, IS_TRAVIS, closeActiveWindows, setPythonExecutable } from './initialize';
// The module 'assert' provides assertion methods from node
import * as assert from 'assert';
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';
import { PythonImportSortProvider } from '../client/providers/importSortProvider';
import * as path from 'path';
import * as settings from '../client/common/configSettings';
import * as fs from 'fs';
import { EOL } from 'os';
const pythonSettings = settings.PythonSettings.getInstance();
const disposable = setPythonExecutable(pythonSettings);
const fileToFormatWithoutConfig = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'sorting', 'noconfig', 'before.py');
const originalFileToFormatWithoutConfig = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'sorting', 'noconfig', 'original.py');
const fileToFormatWithConfig = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'sorting', 'withconfig', 'before.py');
const originalFileToFormatWithConfig = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'sorting', 'withconfig', 'original.py');
const fileToFormatWithConfig1 = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'sorting', 'withconfig', 'before.1.py');
const originalFileToFormatWithConfig1 = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'sorting', 'withconfig', 'original.1.py');
const extensionDir = path.join(__dirname, '..', '..');
suite('Sorting', () => {
suiteSetup(done => {
initialize().then(() => done(), () => done());
});
suiteTeardown(done => {
disposable.dispose();
fs.writeFileSync(fileToFormatWithConfig, fs.readFileSync(originalFileToFormatWithConfig));
fs.writeFileSync(fileToFormatWithConfig1, fs.readFileSync(originalFileToFormatWithConfig1));
fs.writeFileSync(fileToFormatWithoutConfig, fs.readFileSync(originalFileToFormatWithoutConfig));
closeActiveWindows().then(() => done(), () => done());
});
setup(done => {
pythonSettings.sortImports.args = [];
fs.writeFileSync(fileToFormatWithConfig, fs.readFileSync(originalFileToFormatWithConfig));
fs.writeFileSync(fileToFormatWithoutConfig, fs.readFileSync(originalFileToFormatWithoutConfig));
fs.writeFileSync(fileToFormatWithConfig1, fs.readFileSync(originalFileToFormatWithConfig1));
closeActiveWindows().then(() => done(), () => done());
});
test('Without Config', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileToFormatWithoutConfig).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
textEditor = editor;
assert(vscode.window.activeTextEditor, 'No active editor');
const sorter = new PythonImportSortProvider();
return sorter.sortImports(extensionDir, textDocument);
}).then(edits => {
assert.equal(edits.filter(value => value.newText === EOL && value.range.isEqual(new vscode.Range(2, 0, 2, 0))).length, 1, 'EOL not found');
assert.equal(edits.filter(value => value.newText === '' && value.range.isEqual(new vscode.Range(3, 0, 4, 0))).length, 1, '"" not found');
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');
assert.equal(edits.filter(value => value.newText === '' && value.range.isEqual(new vscode.Range(13, 0, 18, 0))).length, 1, '"" not found');
}).then(done, done);
});
test('Without Config (via Command)', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
let originalContent = '';
vscode.workspace.openTextDocument(fileToFormatWithoutConfig).then(document => {
textDocument = document;
originalContent = textDocument.getText();
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
return vscode.commands.executeCommand('python.sortImports');
}).then(() => {
assert.notEqual(originalContent, textDocument.getText(), 'Contents have not changed');
}).then(done, done);
});
test('With Config', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileToFormatWithConfig).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const sorter = new PythonImportSortProvider();
return sorter.sortImports(extensionDir, textDocument);
}).then(edits => {
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}`;
assert.equal(edits.filter(value => value.newText === newValue && value.range.isEqual(new vscode.Range(0, 0, 3, 0))).length, 1, 'New Text not found');
}).then(done, done);
});
test('With Config (via Command)', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
let originalContent = '';
vscode.workspace.openTextDocument(fileToFormatWithConfig).then(document => {
textDocument = document;
originalContent = document.getText();
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
return vscode.commands.executeCommand('python.sortImports');
}).then(() => {
assert.notEqual(originalContent, textDocument.getText(), 'Contents have not changed');
}).then(done, done);
});
// Doesn't always work on Travis !?!
if (!IS_TRAVIS) {
test('With Changes and Config in Args', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
pythonSettings.sortImports.args = ['-sp', path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'sorting', 'withconfig')];
vscode.workspace.openTextDocument(fileToFormatWithConfig).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
return editor.edit(editor => {
editor.insert(new vscode.Position(0, 0), 'from third_party import lib0' + EOL);
});
}).then(() => {
const sorter = new PythonImportSortProvider();
return sorter.sortImports(extensionDir, textDocument);
}).then(edits => {
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}`;
assert.equal(edits.length, 1, 'Incorrect number of edits');
assert.equal(edits[0].newText, newValue, 'New Value is not the same');
assert.equal(`${edits[0].range.start.line},${edits[0].range.start.character}`, '1,0', 'Start position is not the same');
assert.equal(`${edits[0].range.end.line},${edits[0].range.end.character}`, '2,0', 'End position is not the same');
}).then(done, done);
});
}
test('With Changes and Config in Args (via Command)', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
let originalContent = '';
pythonSettings.sortImports.args = ['-sp', path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'sorting', 'withconfig')];
vscode.workspace.openTextDocument(fileToFormatWithConfig).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
return editor.edit(editor => {
editor.insert(new vscode.Position(0, 0), 'from third_party import lib0' + EOL);
});
}).then(() => {
originalContent = textDocument.getText();
return vscode.commands.executeCommand('python.sortImports');
}).then(edits => {
assert.notEqual(originalContent, textDocument.getText(), 'Contents have not changed');
}).then(done, done);
});
});