@@ -6,17 +6,18 @@ import * as vscode from 'vscode';
66import * as path from 'path' ;
77import * as settings from '../client/common/configSettings' ;
88import * as fs from 'fs-extra' ;
9- import { initialize } from './initialize' ;
9+ import { initialize , closeActiveWindows } from './initialize' ;
1010import { execPythonFile } from '../client/common/utils' ;
1111import { extractVariable , extractMethod } from '../client/providers/simpleRefactorProvider' ;
12+ import { RefactorProxy } from '../client/refactor/proxy' ;
1213
1314let EXTENSION_DIR = path . join ( __dirname , '..' , '..' ) ;
1415let pythonSettings = settings . PythonSettings . getInstance ( ) ;
1516
1617const refactorSourceFile = path . join ( __dirname , '..' , '..' , 'src' , 'test' , 'pythonFiles' , 'refactoring' , 'standAlone' , 'refactor.py' ) ;
1718const refactorTargetFile = path . join ( __dirname , '..' , '..' , 'out' , 'test' , 'pythonFiles' , 'refactoring' , 'standAlone' , 'refactor.py' ) ;
1819let isPython3 = true ;
19-
20+ let isTRAVIS = ( process . env [ 'TRAVIS' ] + '' ) === 'true' ;
2021class 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} ) ;
0 commit comments