@@ -19,102 +19,102 @@ let pythonSettings = settings.PythonSettings.getInstance();
1919let ch = vscode . window . createOutputChannel ( "Tests" ) ;
2020let pythoFilesPath = path . join ( __dirname , ".." , ".." , "src" , "test" , "pythonFiles" , "formatting" ) ;
2121
22- function closeActiveEditor ( ) {
23- if ( vscode . window . activeTextEditor ) {
24- vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
25- }
26- }
2722suite ( "Formatting" , ( ) => {
23+ setup ( ( ) => {
24+ return new Promise < any > ( resolve => {
25+ setTimeout ( function ( ) {
26+ resolve ( ) ;
27+ } , 1000 ) ;
28+ } ) ;
29+ } ) ;
30+ teardown ( ( ) => {
31+ if ( vscode . window . activeTextEditor ) {
32+ return vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
33+ }
34+ return Promise . resolve ( ) ;
35+ } ) ;
2836 test ( "AutoPep8" , done => {
2937 let fileToFormat = path . join ( pythoFilesPath , "beforeAutoPep8.py" ) ;
30- vscode . workspace . openTextDocument ( fileToFormat ) . then ( textDocument => {
31- vscode . window . showTextDocument ( textDocument ) . then ( textEditor => {
32- let formatter = new AutoPep8Formatter ( ch , pythonSettings , pythoFilesPath ) ;
33- return formatter . formatDocument ( textDocument , null , null ) . then ( edits => {
34- textEditor . edit ( editBuilder => {
35- edits . forEach ( edit => editBuilder . replace ( edit . range , edit . newText ) ) ;
36- } ) . then ( edited => {
37- let formattedFile = path . join ( pythoFilesPath , "afterAutoPep8.py" ) ;
38- let formattedContents = fs . readFile ( formattedFile , "utf-8" , ( error , data ) => {
39- if ( error ) {
40- return assert . fail ( error , "" , "Failed to read formatted file" ) ;
41- }
42- assert . equal ( textEditor . document . getText ( ) , data , "Formatted text is not the same" ) ;
43- } ) ;
44- } ) ;
45- } , error => {
46- assert . fail ( error , "" , "Error in Formatting, " + error ) ;
47- } ) ;
48- } , error => {
49- assert . fail ( error , "" , "Error in Formatting, " + error ) ;
38+ let textEditor : vscode . TextEditor ;
39+ let textDocument : vscode . TextDocument ;
40+ vscode . workspace . openTextDocument ( fileToFormat ) . then ( document => {
41+ textDocument = document ;
42+ return vscode . window . showTextDocument ( textDocument ) ;
43+ } ) . then ( editor => {
44+ textEditor = editor ;
45+ let formatter = new AutoPep8Formatter ( ch , pythonSettings , pythoFilesPath ) ;
46+ return formatter . formatDocument ( textDocument , null , null ) ;
47+ } ) . then ( edits => {
48+ return textEditor . edit ( editBuilder => {
49+ edits . forEach ( edit => editBuilder . replace ( edit . range , edit . newText ) ) ;
5050 } ) ;
51- } , error => {
52- assert . fail ( error , "" , "Error in Opening Document, " + error ) ;
53- } ) . then ( ( ) => done ( ) , done ) . then ( ( ) => closeActiveEditor ( ) , closeActiveEditor ) ;
51+ } ) . then ( edited => {
52+ let formattedFile = path . join ( pythoFilesPath , "afterAutoPep8.py" ) ;
53+ let formattedContents = fs . readFile ( formattedFile , "utf-8" , ( error , data ) => {
54+ if ( error ) {
55+ return assert . fail ( error , "" , "Failed to read formatted file" ) ;
56+ }
57+ assert . equal ( textEditor . document . getText ( ) , data , "Formatted text is not the same" ) ;
58+ } ) ;
59+ } ) . then ( done , done ) ;
5460 } ) ;
5561
5662 test ( "Yapf" , done => {
5763 let fileToFormat = path . join ( pythoFilesPath , "beforeYapf.py" ) ;
58- vscode . workspace . openTextDocument ( fileToFormat ) . then ( textDocument => {
59- vscode . window . showTextDocument ( textDocument ) . then ( textEditor => {
60- let formatter = new YapfFormatter ( ch , pythonSettings , pythoFilesPath ) ;
61- return formatter . formatDocument ( textDocument , null , null ) . then ( edits => {
62- textEditor . edit ( editBuilder => {
63- edits . forEach ( edit => editBuilder . replace ( edit . range , edit . newText ) ) ;
64- } ) . then ( edited => {
65- let formattedFile = path . join ( pythoFilesPath , "afterYapf.py" ) ;
66- let formattedContents = fs . readFile ( formattedFile , "utf-8" , ( error , data ) => {
67- if ( error ) {
68- return assert . fail ( error , "" , "Failed to read formatted file" ) ;
69- }
70- assert . equal ( textEditor . document . getText ( ) , data , "Formatted text is not the same" ) ;
71- } ) ;
72- } ) ;
73- } , error => {
74- assert . fail ( error , "" , "Error in Formatting, " + error ) ;
75- } ) ;
76- } , error => {
77- assert . fail ( error , "" , "Error in Formatting, " + error ) ;
64+ let textEditor : vscode . TextEditor ;
65+ let textDocument : vscode . TextDocument ;
66+ vscode . workspace . openTextDocument ( fileToFormat ) . then ( document => {
67+ textDocument = document ;
68+ return vscode . window . showTextDocument ( textDocument ) ;
69+ } ) . then ( editor => {
70+ textEditor = editor ;
71+ let formatter = new YapfFormatter ( ch , pythonSettings , pythoFilesPath ) ;
72+ return formatter . formatDocument ( textDocument , null , null ) ;
73+ } ) . then ( edits => {
74+ return textEditor . edit ( editBuilder => {
75+ edits . forEach ( edit => editBuilder . replace ( edit . range , edit . newText ) ) ;
76+ } ) ;
77+ } ) . then ( edited => {
78+ let formattedFile = path . join ( pythoFilesPath , "afterYapf.py" ) ;
79+ let formattedContents = fs . readFile ( formattedFile , "utf-8" , ( error , data ) => {
80+ if ( error ) {
81+ return assert . fail ( error , "" , "Failed to read formatted file" ) ;
82+ }
83+ var x = textEditor . document . getText ( ) ;
84+ assert . equal ( textEditor . document . getText ( ) , data , "Formatted text is not the same" ) ;
7885 } ) ;
79- } , error => {
80- assert . fail ( error , "" , "Error in Opening Document, " + error ) ;
81- } ) . then ( ( ) => done ( ) , done ) . then ( ( ) => closeActiveEditor ( ) , closeActiveEditor ) ;
86+ } ) . then ( done , done ) ;
8287 } ) ;
8388
8489 test ( "Yapf autoformat on save" , done => {
8590 let formattedFile = path . join ( pythoFilesPath , "afterYapfFormatOnSave.py" ) ;
86- let fileToFormat = path . join ( pythoFilesPath , "beforeYapfFormatOnSave.py" ) ;
8791 let fileToCopyFrom = path . join ( pythoFilesPath , "beforeYapfFormatOnSaveOriginal.py" ) ;
8892 let formattedFileContents = fs . readFileSync ( formattedFile , "utf-8" ) ;
93+
94+ let fileToFormat = path . join ( pythoFilesPath , "beforeYapf.py" ) ;
95+ let textDocument : vscode . TextDocument ;
96+
8997 if ( fs . existsSync ( fileToFormat ) ) { fs . unlinkSync ( fileToFormat ) ; }
9098 fs . copySync ( fileToCopyFrom , fileToFormat ) ;
9199 const FORMAT_ON_SAVE = pythonSettings . formatting . formatOnSave ;
92100 pythonSettings . formatting . formatOnSave = true ;
93101 pythonSettings . formatting . provider = "yapf" ;
94102
95- vscode . workspace . openTextDocument ( fileToFormat ) . then ( textDocument => {
96- return vscode . window . showTextDocument ( textDocument ) . then ( textEditor => {
97- return textEditor . edit ( editBuilder => {
98- editBuilder . insert ( new vscode . Position ( 0 , 0 ) , "#" ) ;
99- } ) . then ( edited => {
100- return textDocument . save ( ) . then ( saved => {
101- return new Promise < any > ( ( resolve , reject ) => {
102- setTimeout ( ( ) => {
103- assert . equal ( textDocument . getText ( ) , formattedFileContents , "Formatted contents are not the same" ) ;
104- resolve ( ) ;
105- } , 1000 ) ;
106- } ) ;
107- } , error => {
108- assert . fail ( error , "" , "Error in Saving Document, " + error ) ;
109- } ) ;
110- } , error => {
111- assert . fail ( error , "" , "Error in Editing Document, " + error ) ;
112- } ) ;
113- } , error => {
114- assert . fail ( error , "" , "Error in Showing Document, " + error ) ;
103+ vscode . workspace . openTextDocument ( fileToFormat ) . then ( document => {
104+ textDocument = document ;
105+ return vscode . window . showTextDocument ( textDocument ) ;
106+ } ) . then ( editor => {
107+ return editor . edit ( editBuilder => {
108+ editBuilder . insert ( new vscode . Position ( 0 , 0 ) , "#\n" ) ;
109+ } ) ;
110+ } ) . then ( saved => {
111+ return new Promise < any > ( ( resolve , reject ) => {
112+ setTimeout ( ( ) => {
113+ resolve ( ) ;
114+ } , 1000 ) ;
115115 } ) ;
116- } , error => {
117- assert . fail ( error , "" , "Error in Opening Document, " + error ) ;
118- } ) . then ( ( ) => done ( ) , done ) . then ( ( ) => closeActiveEditor ( ) , closeActiveEditor ) ;
116+ } ) . then ( ( ) => {
117+ assert . equal ( textDocument . getText ( ) , formattedFileContents , "Formatted contents are not the same" ) ;
118+ } ) . then ( done , done ) ;
119119 } ) ;
120120} ) ;
0 commit comments