@@ -11,12 +11,12 @@ import * as assert from 'assert';
1111// You can import and use all API from the 'vscode' module
1212// as well as import your extension to test it
1313import * as vscode from 'vscode' ;
14- import { AutoPep8Formatter } from '../client/formatters/autoPep8Formatter' ;
15- import { YapfFormatter } from '../client/formatters/yapfFormatter' ;
14+ import { AutoPep8Formatter } from '../client/formatters/autoPep8Formatter' ;
15+ import { YapfFormatter } from '../client/formatters/yapfFormatter' ;
1616import * as path from 'path' ;
1717import * as settings from '../client/common/configSettings' ;
1818import * as fs from 'fs-extra' ;
19- import { execPythonFile } from '../client/common/utils' ;
19+ import { execPythonFile } from '../client/common/utils' ;
2020
2121let pythonSettings = settings . PythonSettings . getInstance ( ) ;
2222let ch = vscode . window . createOutputChannel ( 'Tests' ) ;
@@ -46,7 +46,7 @@ suiteSetup(done => {
4646 formattedAutoPep8 = formattedResults [ 1 ] ;
4747 } ) . then ( ( ) => {
4848 done ( ) ;
49- } , reason => {
49+ } , reason => {
5050 console . error ( reason ) ;
5151 console . error ( 'Failed to initialize format tests' ) ;
5252 done ( ) ;
@@ -81,7 +81,7 @@ suite('Formatting', () => {
8181 } ) ;
8282 } ) . then ( edited => {
8383 assert . equal ( textEditor . document . getText ( ) , formattedContents , 'Formatted text is not the same' ) ;
84- } , reason => {
84+ } , reason => {
8585 assert . fail ( reason , undefined , 'Formatting failed' , '' ) ;
8686 } ) ;
8787 }
@@ -92,4 +92,35 @@ suite('Formatting', () => {
9292 test ( 'Yapf' , done => {
9393 testFormatting ( new YapfFormatter ( ch , pythonSettings , pythoFilesPath ) , formattedYapf , yapfFileToFormat ) . then ( done , done ) ;
9494 } ) ;
95+
96+ function testAutoFormatting ( formatter : string , formattedContents : string , fileToFormat : string ) : PromiseLike < void > {
97+ let textDocument : vscode . TextDocument ;
98+ pythonSettings . formatting . formatOnSave = true ;
99+ pythonSettings . formatting . provider = formatter ;
100+ return vscode . workspace . openTextDocument ( fileToFormat ) . then ( document => {
101+ textDocument = document ;
102+ return vscode . window . showTextDocument ( textDocument ) ;
103+ } ) . then ( editor => {
104+ return editor . edit ( editBuilder => {
105+ editBuilder . insert ( new vscode . Position ( 0 , 0 ) , '#\n' ) ;
106+ } ) ;
107+ } ) . then ( edited => {
108+ return textDocument . save ( ) ;
109+ } ) . then ( saved => {
110+ return new Promise < any > ( ( resolve , reject ) => {
111+ setTimeout ( ( ) => {
112+ resolve ( ) ;
113+ } , 4000 ) ;
114+ } ) ;
115+ } ) . then ( ( ) => {
116+ assert . equal ( textDocument . getText ( ) , formattedContents , 'Formatted contents are not the same' ) ;
117+ } ) ;
118+ }
119+ test ( 'AutoPep8 autoformat on save' , done => {
120+ testAutoFormatting ( 'autopep8' , '#\n' + formattedAutoPep8 , autoPep8FileToAutoFormat ) . then ( done , done ) ;
121+ } ) ;
122+
123+ test ( 'Yapf autoformat on save' , done => {
124+ testAutoFormatting ( 'yapf' , '#\n' + formattedYapf , yapfFileToAutoFormat ) . then ( done , done ) ;
125+ } ) ;
95126} ) ;
0 commit comments