44
55
66// The module 'assert' provides assertion methods from node 
7- import  *  as  assert  from  " assert" ; 
7+ import  *  as  assert  from  ' assert' ; 
88
99// You can import and use all API from the 'vscode' module 
1010// as well as import your extension to test it 
11- import  *  as  vscode  from  "vscode" ; 
12- import  { AutoPep8Formatter }  from  "../client/formatters/autoPep8Formatter" ; 
13- import  { YapfFormatter }  from  "../client/formatters/yapfFormatter" ; 
14- import  *  as  path  from  "path" ; 
15- import  *  as  settings  from  "../client/common/configSettings" ; 
16- import  *  as  fs  from  "fs-extra" ; 
17- import  { initialize }  from  "./initialize" ; 
11+ import  *  as  vscode  from  'vscode' ; 
12+ import  { AutoPep8Formatter }  from  '../client/formatters/autoPep8Formatter' ; 
13+ import  { YapfFormatter }  from  '../client/formatters/yapfFormatter' ; 
14+ import  *  as  path  from  'path' ; 
15+ import  *  as  settings  from  '../client/common/configSettings' ; 
16+ import  *  as  fs  from  'fs-extra' ; 
17+ import  { initialize }  from  './initialize' ; 
18+ import  { execPythonFile }  from  '../client/common/utils' ; 
1819
1920let  pythonSettings  =  settings . PythonSettings . getInstance ( ) ; 
20- let  ch  =  vscode . window . createOutputChannel ( "Tests" ) ; 
21- let  pythoFilesPath  =  path . join ( __dirname ,  ".." ,  ".." ,  "src" ,  "test" ,  "pythonFiles" ,  "formatting" ) ; 
21+ let  ch  =  vscode . window . createOutputChannel ( 'Tests' ) ; 
22+ let  pythoFilesPath  =  path . join ( __dirname ,  '..' ,  '..' ,  'src' ,  'test' ,  'pythonFiles' ,  'formatting' ) ; 
23+ const  originalUnformattedFile  =  path . join ( pythoFilesPath ,  'fileToFormat.py' ) ; 
2224
23- suite ( "Formatting" ,  ( )  =>  { 
24-  setup ( done  =>  { 
25-  initialize ( ) . then ( ( )  =>  done ( ) ,  done ) ; 
26-  } ) ; 
25+ const  autoPep8FileToFormat  =  path . join ( __dirname ,  'pythonFiles' ,  'formatting' ,  'autoPep8FileToFormat.py' ) ; 
26+ const  autoPep8FileToAutoFormat  =  path . join ( __dirname ,  'pythonFiles' ,  'formatting' ,  'autoPep8FileToAutoFormat.py' ) ; 
27+ const  yapfFileToFormat  =  path . join ( __dirname ,  'pythonFiles' ,  'formatting' ,  'yapfFileToFormat.py' ) ; 
28+ const  yapfFileToAutoFormat  =  path . join ( __dirname ,  'pythonFiles' ,  'formatting' ,  'yapfFileToAutoFormat.py' ) ; 
29+ 
30+ let  formattedYapf  =  '' ; 
31+ let  formattedAutoPep8  =  '' ; 
32+ 
33+ suiteSetup ( done  =>  { 
34+  initialize ( ) . then ( ( )  =>  { 
35+  [ autoPep8FileToFormat ,  autoPep8FileToAutoFormat ,  yapfFileToFormat ,  yapfFileToAutoFormat ] . forEach ( file  =>  { 
36+  if  ( fs . existsSync ( file ) )  {  fs . unlinkSync ( file ) ;  } 
37+  fs . copySync ( originalUnformattedFile ,  file ) ; 
38+  } ) ; 
39+ 
40+  fs . ensureDirSync ( path . dirname ( autoPep8FileToFormat ) ) ; 
41+  let  yapf  =  execPythonFile ( 'yapf' ,  [ originalUnformattedFile ] ,  pythoFilesPath ,  false ) ; 
42+  let  autoPep8  =  execPythonFile ( 'autopep8' ,  [ originalUnformattedFile ] ,  pythoFilesPath ,  false ) ; 
43+  return  Promise . all < string > ( [ yapf ,  autoPep8 ] ) . then ( formattedResults  =>  { 
44+  formattedYapf  =  formattedResults [ 0 ] ; 
45+  formattedAutoPep8  =  formattedResults [ 1 ] ; 
46+  } ) . then ( ( )  =>  { 
47+  done ( ) ; 
48+  } ) ; 
49+  } ,  done ) ; 
50+ } ) ; 
51+ 
52+ suiteTeardown ( ( )  =>  { 
53+  if  ( vscode . window . activeTextEditor )  { 
54+  return  vscode . commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ; 
55+  } 
56+ } ) ; 
57+ 
58+ suite ( 'Formatting' ,  ( )  =>  { 
2759 teardown ( ( )  =>  { 
2860 if  ( vscode . window . activeTextEditor )  { 
29-  return  vscode . commands . executeCommand ( " workbench.action.closeActiveEditor" ) ; 
61+  return  vscode . commands . executeCommand ( ' workbench.action.closeActiveEditor' ) ; 
3062 } 
31-  return  Promise . resolve ( ) ; 
3263 } ) ; 
33-  test ( "AutoPep8" ,  done  =>  { 
34-  let  fileToFormat  =  path . join ( pythoFilesPath ,  "beforeAutoPep8.py" ) ; 
64+  function  testFormatting ( formatter : AutoPep8Formatter  |  YapfFormatter ,  formattedContents : string ,  fileToFormat : string ) : PromiseLike < void >  { 
3565 let  textEditor : vscode . TextEditor ; 
3666 let  textDocument : vscode . TextDocument ; 
37-  vscode . workspace . openTextDocument ( fileToFormat ) . then ( document  =>  { 
67+  return   vscode . workspace . openTextDocument ( fileToFormat ) . then ( document  =>  { 
3868 textDocument  =  document ; 
3969 return  vscode . window . showTextDocument ( textDocument ) ; 
4070 } ) . then ( editor  =>  { 
4171 textEditor  =  editor ; 
42-  let  formatter  =  new  AutoPep8Formatter ( ch ,  pythonSettings ,  pythoFilesPath ) ; 
4372 return  formatter . formatDocument ( textDocument ,  null ,  null ) ; 
4473 } ) . then ( edits  =>  { 
4574 return  textEditor . edit ( editBuilder  =>  { 
4675 edits . forEach ( edit  =>  editBuilder . replace ( edit . range ,  edit . newText ) ) ; 
4776 } ) ; 
4877 } ) . then ( edited  =>  { 
49-  let  formattedFile  =  path . join ( pythoFilesPath ,  "afterAutoPep8.py" ) ; 
50-  let  formattedContents  =  fs . readFile ( formattedFile ,  "utf-8" ,  ( error ,  data )  =>  { 
51-  if  ( error )  { 
52-  return  assert . fail ( error ,  "" ,  "Failed to read formatted file" ) ; 
53-  } 
54-  assert . equal ( textEditor . document . getText ( ) ,  data ,  "Formatted text is not the same" ) ; 
55-  } ) ; 
56-  } ) . then ( done ,  done ) ; 
78+  assert . equal ( textEditor . document . getText ( ) ,  formattedContents ,  'Formatted text is not the same' ) ; 
79+  } ) ; 
80+  } 
81+  test ( 'AutoPep8' ,  done  =>  { 
82+  testFormatting ( new  AutoPep8Formatter ( ch ,  pythonSettings ,  pythoFilesPath ) ,  formattedAutoPep8 ,  autoPep8FileToFormat ) . then ( done ,  done ) ; 
5783 } ) ; 
5884
59-  test ( "Yapf" ,  done  =>  { 
60-  let  fileToFormat  =  path . join ( pythoFilesPath ,  "beforeYapf.py" ) ; 
61-  let  textEditor : vscode . TextEditor ; 
62-  let  textDocument : vscode . TextDocument ; 
63-  vscode . workspace . openTextDocument ( fileToFormat ) . then ( document  =>  { 
64-  textDocument  =  document ; 
65-  return  vscode . window . showTextDocument ( textDocument ) ; 
66-  } ) . then ( editor  =>  { 
67-  textEditor  =  editor ; 
68-  let  formatter  =  new  YapfFormatter ( ch ,  pythonSettings ,  pythoFilesPath ) ; 
69-  return  formatter . formatDocument ( textDocument ,  null ,  null ) ; 
70-  } ) . then ( edits  =>  { 
71-  return  textEditor . edit ( editBuilder  =>  { 
72-  edits . forEach ( edit  =>  editBuilder . replace ( edit . range ,  edit . newText ) ) ; 
73-  } ) ; 
74-  } ) . then ( edited  =>  { 
75-  let  formattedFile  =  path . join ( pythoFilesPath ,  "afterYapf.py" ) ; 
76-  let  formattedContents  =  fs . readFile ( formattedFile ,  "utf-8" ,  ( error ,  data )  =>  { 
77-  if  ( error )  { 
78-  return  assert . fail ( error ,  "" ,  "Failed to read formatted file" ) ; 
79-  } 
80-  assert . equal ( textEditor . document . getText ( ) ,  data ,  "Formatted text is not the same" ) ; 
81-  } ) ; 
82-  } ) . then ( done ,  done ) ; 
85+  test ( 'Yapf' ,  done  =>  { 
86+  testFormatting ( new  YapfFormatter ( ch ,  pythonSettings ,  pythoFilesPath ) ,  formattedYapf ,  yapfFileToFormat ) . then ( done ,  done ) ; 
8387 } ) ; 
8488
85-  test ( "Yapf autoformat on save" ,  done  =>  { 
86-  let  formattedFile  =  path . join ( pythoFilesPath ,  "afterYapfFormatOnSave.py" ) ; 
87-  let  fileToCopyFrom  =  path . join ( pythoFilesPath ,  "beforeYapfFormatOnSaveOriginal.py" ) ; 
88-  let  formattedFileContents  =  fs . readFileSync ( formattedFile ,  "utf-8" ) ; 
89- 
90-  let  fileToFormat  =  path . join ( pythoFilesPath ,  "beforeYapf.py" ) ; 
89+  function  testAutoFormatting ( formatter : string ,  formattedContents : string ,  fileToFormat : string ) : PromiseLike < void >  { 
9190 let  textDocument : vscode . TextDocument ; 
92- 
93-  if  ( fs . existsSync ( fileToFormat ) )  {  fs . unlinkSync ( fileToFormat ) ;  } 
94-  fs . copySync ( fileToCopyFrom ,  fileToFormat ) ; 
95-  const  FORMAT_ON_SAVE  =  pythonSettings . formatting . formatOnSave ; 
9691 pythonSettings . formatting . formatOnSave  =  true ; 
97-  pythonSettings . formatting . provider  =  "yapf" ; 
98- 
99-  vscode . workspace . openTextDocument ( fileToFormat ) . then ( document  =>  { 
92+  pythonSettings . formatting . provider  =  formatter ; 
93+  return  vscode . workspace . openTextDocument ( fileToFormat ) . then ( document  =>  { 
10094 textDocument  =  document ; 
10195 return  vscode . window . showTextDocument ( textDocument ) ; 
10296 } ) . then ( editor  =>  { 
10397 return  editor . edit ( editBuilder  =>  { 
104-  editBuilder . insert ( new  vscode . Position ( 0 ,  0 ) ,  " #\n" ) ; 
98+  editBuilder . insert ( new  vscode . Position ( 0 ,  0 ) ,  ' #\n' ) ; 
10599 } ) ; 
100+  } ) . then ( edited  =>  { 
101+  return  textDocument . save ( ) ; 
106102 } ) . then ( saved  =>  { 
107103 return  new  Promise < any > ( ( resolve ,  reject )  =>  { 
108104 setTimeout ( ( )  =>  { 
109105 resolve ( ) ; 
110106 } ,  1000 ) ; 
111107 } ) ; 
112108 } ) . then ( ( )  =>  { 
113-  assert . equal ( textDocument . getText ( ) ,  formattedFileContents ,  "Formatted contents are not the same" ) ; 
114-  } ) . then ( done ,  done ) ; 
109+  assert . equal ( textDocument . getText ( ) ,  formattedContents ,  'Formatted contents are not the same' ) ; 
110+  } ) ; 
111+  } 
112+  test ( 'AutoPep8 autoformat on save' ,  done  =>  { 
113+  testAutoFormatting ( 'autopep8' ,  '#\n'  +  formattedAutoPep8 ,  autoPep8FileToAutoFormat ) . then ( done ,  done ) ; 
114+  } ) ; 
115+ 
116+  test ( 'Yapf autoformat on save' ,  done  =>  { 
117+  testAutoFormatting ( 'yapf' ,  '#\n'  +  formattedYapf ,  yapfFileToAutoFormat ) . then ( done ,  done ) ; 
115118 } ) ; 
116119} ) ; 
0 commit comments