Skip to content

Commit 7fca46a

Browse files
committed
disabled Travis tests for refactor #220
1 parent 1b97870 commit 7fca46a

4 files changed

Lines changed: 114 additions & 84 deletions

File tree

src/client/providers/simpleRefactorProvider.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as path from 'path';
55
import * as fs from 'fs';
66
import {RefactorProxy} from '../refactor/proxy';
77
import {getTextEditsFromPatch} from '../common/editor';
8+
import {PythonSettings, IPythonSettings} from '../common/configSettings';
89

910
interface RenameResponse {
1011
results: [{ diff: string }];
@@ -30,9 +31,10 @@ export function activateSimplePythonRefactorProvider(context: vscode.ExtensionCo
3031

3132
// Exported for unit testing
3233
export function extractVariable(extensionDir: string, textEditor: vscode.TextEditor, range: vscode.Range,
33-
outputChannel: vscode.OutputChannel, workspaceRoot: string = vscode.workspace.rootPath, renameAfterExtration: boolean = true): Promise<any> {
34+
outputChannel: vscode.OutputChannel, workspaceRoot: string = vscode.workspace.rootPath,
35+
renameAfterExtration: boolean = true, pythonSettings: IPythonSettings = PythonSettings.getInstance()): Promise<any> {
3436
let newName = 'newvariable' + new Date().getMilliseconds().toString();
35-
let proxy = new RefactorProxy(extensionDir, workspaceRoot);
37+
let proxy = new RefactorProxy(extensionDir, pythonSettings, workspaceRoot);
3638
let rename = proxy.extractVariable<RenameResponse>(textEditor.document, newName, textEditor.document.uri.fsPath, range).then(response => {
3739
return response.results[0].diff;
3840
});
@@ -42,9 +44,10 @@ export function extractVariable(extensionDir: string, textEditor: vscode.TextEdi
4244

4345
// Exported for unit testing
4446
export function extractMethod(extensionDir: string, textEditor: vscode.TextEditor, range: vscode.Range,
45-
outputChannel: vscode.OutputChannel, workspaceRoot: string = vscode.workspace.rootPath, renameAfterExtration: boolean = true): Promise<any> {
47+
outputChannel: vscode.OutputChannel, workspaceRoot: string = vscode.workspace.rootPath,
48+
renameAfterExtration: boolean = true, pythonSettings: IPythonSettings = PythonSettings.getInstance()): Promise<any> {
4649
let newName = 'newmethod' + new Date().getMilliseconds().toString();
47-
let proxy = new RefactorProxy(extensionDir, workspaceRoot);
50+
let proxy = new RefactorProxy(extensionDir, pythonSettings, workspaceRoot);
4851
let rename = proxy.extractMethod<RenameResponse>(textEditor.document, newName, textEditor.document.uri.fsPath, range).then(response => {
4952
return response.results[0].diff;
5053
});
@@ -101,5 +104,6 @@ function extractName(extensionDir: string, textEditor: vscode.TextEditor, range:
101104
outputChannel.appendLine('#'.repeat(10) + 'Refactor Output' + '#'.repeat(10));
102105
outputChannel.appendLine('Error in refactoring:\n' + errorMessage);
103106
vscode.window.showErrorMessage(errorMessage);
107+
throw errorMessage;
104108
});
105109
}

src/client/refactor/proxy.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as fs from 'fs';
66
import * as child_process from 'child_process';
77
import {ExtractResult} from './contracts';
88
import {execPythonFile} from '../common/utils';
9-
import {PythonSettings} from '../common/configSettings';
9+
import {IPythonSettings} from '../common/configSettings';
1010

1111
const ROPE_PYTHON_VERSION = 'Refactor requires Python 2.x. Set \'python.pythonRopePath\' in Settings.json';
1212
const ERROR_PREFIX = '$ERROR';
@@ -21,11 +21,9 @@ export class RefactorProxy extends vscode.Disposable {
2121
private _commandReject: (reason?: any) => void;
2222
private _initializeReject: (reason?: any) => void;
2323
static pythonPath: string;
24-
private _settings: PythonSettings;
25-
constructor(extensionDir: string, private workspaceRoot: string = vscode.workspace.rootPath) {
24+
constructor(extensionDir: string, private pythonSettings: IPythonSettings, private workspaceRoot: string = vscode.workspace.rootPath) {
2625
super(() => { });
2726
this._extensionDir = extensionDir;
28-
this._settings = PythonSettings.getInstance();
2927
vscode.workspace.onDidChangeConfiguration(() => {
3028
RefactorProxy.pythonPath = '';
3129
});
@@ -49,7 +47,6 @@ export class RefactorProxy extends vscode.Disposable {
4947
}
5048
private sendCommand<T>(command: string): Promise<T> {
5149
return this.pickValidPythonPath().then(pythonPath => {
52-
console.log(`Resolved path - ${pythonPath}`);
5350
return this.initialize(pythonPath);
5451
}).then(() => {
5552
return new Promise<T>((resolve, reject) => {
@@ -65,28 +62,27 @@ export class RefactorProxy extends vscode.Disposable {
6562
return Promise.resolve(RefactorProxy.pythonPath);
6663
}
6764

68-
if (this._settings.pythonPath === this._settings.python2Path) {
65+
if (this.pythonSettings.pythonPath === this.pythonSettings.python2Path) {
6966
// First try what ever path we have in pythonRopePath
70-
return this.checkIfPythonVersionIs3(this._settings.python2Path).then(() => {
71-
return this._settings.python2Path;
67+
return this.checkIfPythonVersionIs3(this.pythonSettings.python2Path).then(() => {
68+
return this.pythonSettings.python2Path;
7269
});
7370
}
7471

7572
// First try what ever path we have in pythonRopePath
76-
return this.checkIfPythonVersionIs3(this._settings.python2Path).then(() => {
77-
return this._settings.python2Path;
73+
return this.checkIfPythonVersionIs3(this.pythonSettings.python2Path).then(() => {
74+
return this.pythonSettings.python2Path;
7875
}).catch(() => {
7976
// Now the path in pythonPath
80-
return this.checkIfPythonVersionIs3(this._settings.pythonPath).then(() => {
81-
return this._settings.pythonPath;
77+
return this.checkIfPythonVersionIs3(this.pythonSettings.pythonPath).then(() => {
78+
return this.pythonSettings.pythonPath;
8279
});
8380
});
8481
}
8582

8683
private checkIfPythonVersionIs3(pythonPath: string): Promise<boolean> {
8784
return new Promise<boolean>((resolve, reject) => {
8885
child_process.execFile(pythonPath, ['-c', 'import sys;print(sys.version)'], null, (error, stdout, stderr) => {
89-
console.log(`Testing version - ${pythonPath}, ${stdout}, ${stderr}, ${error + ''}`);
9086
if (stdout.indexOf('3.') === 0) {
9187
reject(new Error(ROPE_PYTHON_VERSION));
9288
}
@@ -126,7 +122,7 @@ export class RefactorProxy extends vscode.Disposable {
126122
this._previousStdErrData = '';
127123
return;
128124
}
129-
console.log(`Refactor StdErr Out - ${dataStr}`);
125+
130126
let lengthOfHeader = dataStr.indexOf(':') + 1;
131127
let lengthOfMessage = parseInt(dataStr.substring(ERROR_PREFIX.length, lengthOfHeader - 1));
132128
if (dataStr.length === lengthOfMessage + lengthOfHeader) {
@@ -148,21 +144,18 @@ export class RefactorProxy extends vscode.Disposable {
148144
}
149145
}
150146
else {
151-
console.log(`Initialize Failed - ${dataStr}`);
152147
this._initializeReject(`Refactor failed. ${dataStr}`);
153148
}
154149
}
155150
private handleError(error: Error) {
156151
if (this._startedSuccessfully) {
157-
console.log(`handleError after starting - ${error + ''}`);
158152
return this._commandReject(error);
159153
}
160-
console.log(`handleError before starting - ${error + ''}`);
161154
this._initializeReject(error);
162155
}
163156
private onData(data: string) {
164157
if (!this._commandResolve) { return; }
165-
console.log('onData - ' + data);
158+
166159
// Possible there was an exception in parsing the data returned
167160
// So append the data then parse it
168161
let dataStr = this._previousOutData = this._previousOutData + data + '';

src/test/extension.simpleRefactor.test.ts

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import * as vscode from 'vscode';
66
import * as path from 'path';
77
import * as settings from '../client/common/configSettings';
88
import * as fs from 'fs-extra';
9-
import {initialize} from './initialize';
9+
import {initialize, closeActiveWindows} from './initialize';
1010
import {execPythonFile} from '../client/common/utils';
1111
import {extractVariable, extractMethod} from '../client/providers/simpleRefactorProvider';
12+
import {RefactorProxy} from '../client/refactor/proxy';
1213

1314
let EXTENSION_DIR = path.join(__dirname, '..', '..');
1415
let pythonSettings = settings.PythonSettings.getInstance();
1516

1617
const refactorSourceFile = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'refactoring', 'standAlone', 'refactor.py');
1718
const refactorTargetFile = path.join(__dirname, '..', '..', 'out', 'test', 'pythonFiles', 'refactoring', 'standAlone', 'refactor.py');
1819
let isPython3 = true;
19-
20+
let isTRAVIS = (process.env['TRAVIS'] + '') === 'true';
2021
class MockOutputChannel implements vscode.OutputChannel {
2122
constructor(name: string) {
2223
this.name = name;
@@ -63,72 +64,87 @@ suite('Simple Refactor', () => {
6364
fs.unlinkSync(refactorTargetFile);
6465
}
6566
fs.copySync(refactorSourceFile, refactorTargetFile, { clobber: true });
66-
// pythonSettings.python2Path = '/Users/donjayamanne/Desktop/Development/Python/Temp/MyEnvs/p3/bin/python'
67-
// pythonSettings.pythonPath = '/Users/donjayamanne/Desktop/Development/Python/Temp/MyEnvs/p3/bin/python'
6867
});
69-
teardown(() => {
70-
if (vscode.window.activeTextEditor) {
71-
return vscode.commands.executeCommand('workbench.action.closeActiveEditor');
72-
}
68+
teardown(done => {
69+
closeActiveWindows().then(() => {
70+
setTimeout(function () {
71+
RefactorProxy.pythonPath = null;
72+
done();
73+
}, 1000);
74+
});
7375
});
7476

75-
test('Extract Variable', () => {
76-
let ch = new MockOutputChannel('Python');
77-
let textDocument: vscode.TextDocument;
78-
let textEditor: vscode.TextEditor;
79-
let rangeOfTextToExtract = new vscode.Range(new vscode.Position(234, 29), new vscode.Position(234, 38));
77+
if (!isTRAVIS) {
78+
function testingVariableExtraction(shouldError: boolean, pythonSettings: settings.IPythonSettings) {
79+
let ch = new MockOutputChannel('Python');
80+
let textDocument: vscode.TextDocument;
81+
let textEditor: vscode.TextEditor;
82+
let rangeOfTextToExtract = new vscode.Range(new vscode.Position(234, 29), new vscode.Position(234, 38));
8083

81-
return vscode.workspace.openTextDocument(refactorTargetFile).then(document => {
82-
textDocument = document;
83-
return vscode.window.showTextDocument(textDocument);
84-
}).then(editor => {
85-
editor.selections = [new vscode.Selection(rangeOfTextToExtract.start, rangeOfTextToExtract.end)];
86-
editor.selection = new vscode.Selection(rangeOfTextToExtract.start, rangeOfTextToExtract.end);
87-
textEditor = editor;
88-
return;
89-
}).then(() => {
90-
return extractVariable(EXTENSION_DIR, textEditor, rangeOfTextToExtract, ch, path.dirname(refactorTargetFile), false).then(() => {
91-
assert.equal(ch.output.length, 0, 'Output channel is not empty');
92-
assert.equal(textDocument.lineAt(234).text.trim().indexOf('newvariable'), 0, 'New Variable not created');
93-
assert.equal(textDocument.lineAt(234).text.trim().endsWith('= "STARTED"'), true, 'Started Text Assigned to variable');
94-
assert.equal(textDocument.lineAt(235).text.indexOf('(newvariable') >= 0, true, 'New Variable not being used');
95-
}).catch(error => {
96-
assert.fail(error + '', null, 'Variable extraction failed\n' + ch.output);
84+
return vscode.workspace.openTextDocument(refactorTargetFile).then(document => {
85+
textDocument = document;
86+
return vscode.window.showTextDocument(textDocument);
87+
}).then(editor => {
88+
editor.selections = [new vscode.Selection(rangeOfTextToExtract.start, rangeOfTextToExtract.end)];
89+
editor.selection = new vscode.Selection(rangeOfTextToExtract.start, rangeOfTextToExtract.end);
90+
textEditor = editor;
91+
return;
92+
}).then(() => {
93+
return extractVariable(EXTENSION_DIR, textEditor, rangeOfTextToExtract, ch, path.dirname(refactorTargetFile), false, pythonSettings).then(() => {
94+
if (shouldError) {
95+
// Wait a minute this shouldn't work, what's going on
96+
throw new Error('This should fail, but seems to have worked');
97+
}
98+
assert.equal(ch.output.length, 0, 'Output channel is not empty');
99+
assert.equal(textDocument.lineAt(234).text.trim().indexOf('newvariable'), 0, 'New Variable not created');
100+
assert.equal(textDocument.lineAt(234).text.trim().endsWith('= "STARTED"'), true, 'Started Text Assigned to variable');
101+
assert.equal(textDocument.lineAt(235).text.indexOf('(newvariable') >= 0, true, 'New Variable not being used');
102+
}).catch(error => {
103+
if (shouldError) {
104+
// Wait a minute this shouldn't work, what's going on
105+
assert.equal(true, true, 'Error raised as expected');
106+
return;
107+
}
108+
109+
if (typeof error === 'object' && error.message) {
110+
throw error;
111+
}
112+
else {
113+
throw new Error(error);
114+
}
115+
});
116+
}, error => {
117+
if (shouldError) {
118+
// Wait a minute this shouldn't work, what's going on
119+
assert.equal(true, true, 'Error raised as expected');
120+
}
121+
else {
122+
assert.fail(error + '', null, 'Variable extraction failed\n' + ch.output);
123+
if (typeof error === 'object' && error.message) {
124+
throw error;
125+
}
126+
else {
127+
throw new Error(error);
128+
}
129+
}
97130
});
98-
}, error => {
99-
assert.fail(error + '', null, 'Variable extraction failed\n' + ch.output);
131+
}
132+
133+
test('Extract Variable', done => {
134+
testingVariableExtraction(false, pythonSettings).then(() => done(), done);
100135
});
101-
});
102136

103-
test('Extract Variable', () => {
104-
let ch = new MockOutputChannel('Python');
105-
let textDocument: vscode.TextDocument;
106-
let textEditor: vscode.TextEditor;
107-
let rangeOfTextToExtract = new vscode.Range(new vscode.Position(234, 29), new vscode.Position(234, 38));
137+
test('Extract Variable will try to find Python 2.x', done => {
138+
let clonedSettings = JSON.parse(JSON.stringify(pythonSettings));
139+
clonedSettings.python2Path = 'python3';
140+
testingVariableExtraction(false, clonedSettings).then(() => done(), done);
141+
});
108142

109-
return vscode.workspace.openTextDocument(refactorTargetFile).then(document => {
110-
textDocument = document;
111-
return vscode.window.showTextDocument(textDocument);
112-
}).then(editor => {
113-
editor.selections = [new vscode.Selection(rangeOfTextToExtract.start, rangeOfTextToExtract.end)];
114-
editor.selection = new vscode.Selection(rangeOfTextToExtract.start, rangeOfTextToExtract.end);
115-
textEditor = editor;
116-
return;
117-
}).then(() => {
118-
return extractVariable(EXTENSION_DIR, textEditor, rangeOfTextToExtract, ch, path.dirname(refactorTargetFile), false).then(() => {
119-
assert.equal(ch.output.length, 0, 'Output channel is not empty');
120-
assert.equal(textDocument.lineAt(234).text.trim().indexOf('newvariable'), 0, 'New Variable not created');
121-
assert.equal(textDocument.lineAt(234).text.trim().endsWith('= "STARTED"'), true, 'Started Text Assigned to variable');
122-
assert.equal(textDocument.lineAt(235).text.indexOf('(newvariable') >= 0, true, 'New Variable not being used');
123-
}).catch(error => {
124-
console.log('Catch Error:' + error);
125-
console.log('Output:' + ch.output);
126-
assert.fail(error + '', null, 'Variable extraction failed\n' + ch.output);
127-
});
128-
}, error => {
129-
console.log('Error:' + error);
130-
console.log('Output:' + ch.output);
131-
assert.fail(error + '', null, 'Variable extraction failed\n' + ch.output);
143+
test('Extract Variable will not work in Python 3.x', done => {
144+
let clonedSettings = JSON.parse(JSON.stringify(pythonSettings));
145+
clonedSettings.pythonPath = 'python3';
146+
clonedSettings.python2Path = 'python3';
147+
testingVariableExtraction(true, clonedSettings).then(() => done(), done);
132148
});
133-
});
149+
}
134150
});

src/test/initialize.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,21 @@ let dummyPythonFile = path.join(__dirname, "..", "..", "src", "test", "pythonFil
1414

1515
export function initialize(): Thenable<any> {
1616
return vscode.workspace.openTextDocument(dummyPythonFile);
17-
}
17+
}
18+
19+
export function closeActiveWindows(counter: number = 0): Thenable<any> {
20+
if (counter >= 10 || !vscode.window.activeTextEditor) {
21+
return Promise.resolve();
22+
}
23+
return new Promise<any>(resolve => {
24+
setTimeout(function () {
25+
if (!vscode.window.activeTextEditor) {
26+
return resolve();
27+
}
28+
29+
vscode.commands.executeCommand('workbench.action.closeActiveEditor').then(() => {
30+
closeActiveWindows(counter++).then(resolve, resolve);
31+
});
32+
}, 500);
33+
});
34+
}

0 commit comments

Comments
 (0)