1+ //
2+ // Note: This example test is leveraging the Mocha test framework.
3+ // Please refer to their documentation on https://mochajs.org/ for help.
4+ //
5+
6+ // The module 'assert' provides assertion methods from node
7+ import * as assert from "assert" ;
8+
9+ // You can import and use all API from the 'vscode' module
10+ // 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+
18+ let pythonSettings = settings . PythonSettings . getInstance ( ) ;
19+ let ch = vscode . window . createOutputChannel ( "Tests" ) ;
20+ let pythoFilesPath = path . join ( __dirname , ".." , ".." , "src" , "test" , "pythonFiles" , "formatting" ) ;
21+
22+ function closeActiveEditor ( ) {
23+ if ( vscode . window . activeTextEditor ) {
24+ vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
25+ }
26+ }
27+ suite ( "Formatting" , ( ) => {
28+ test ( "AutoPep8" , done => {
29+ let fileToFormat = path . join ( pythoFilesPath , "beforeAutoPep8.py" ) ;
30+ vscode . workspace . openTextDocument ( fileToFormat ) . then ( textDocument => {
31+ vscode . window . showTextDocument ( textDocument ) ;
32+ let formatter = new AutoPep8Formatter ( ch , pythonSettings , pythoFilesPath ) ;
33+ return formatter . formatDocument ( textDocument , null , null ) . then ( edits => {
34+ let activeEditor = vscode . window . activeTextEditor ;
35+ activeEditor . edit ( editBuilder => {
36+ edits . forEach ( edit => editBuilder . replace ( edit . range , edit . newText ) ) ;
37+ } ) . then ( edited => {
38+ let formattedFile = path . join ( pythoFilesPath , "afterAutoPep8.py" ) ;
39+ let formattedContents = fs . readFile ( formattedFile , "utf-8" , ( error , data ) => {
40+ if ( error ) {
41+ return assert . fail ( error , "" , "Failed to read formatted file" ) ;
42+ }
43+ assert . equal ( activeEditor . document . getText ( ) , data , "Formatted text is not the same" ) ;
44+ } ) ;
45+ } ) ;
46+ } , error => {
47+ assert . fail ( error , "" , "Error in Formatting, " + error ) ;
48+ } ) ;
49+ } , error => {
50+ assert . fail ( error , "" , "Error in Opening Document, " + error ) ;
51+ } ) . then ( ( ) => done ( ) , done ) . then ( ( ) => closeActiveEditor ( ) , closeActiveEditor ) ;
52+ } ) ;
53+
54+ test ( "Yapf" , done => {
55+ let fileToFormat = path . join ( pythoFilesPath , "beforeYapf.py" ) ;
56+ vscode . workspace . openTextDocument ( fileToFormat ) . then ( textDocument => {
57+ vscode . window . showTextDocument ( textDocument ) ;
58+ let formatter = new YapfFormatter ( ch , pythonSettings , pythoFilesPath ) ;
59+ return formatter . formatDocument ( textDocument , null , null ) . then ( edits => {
60+ let activeEditor = vscode . window . activeTextEditor ;
61+ activeEditor . edit ( editBuilder => {
62+ edits . forEach ( edit => editBuilder . replace ( edit . range , edit . newText ) ) ;
63+ } ) . then ( edited => {
64+ let formattedFile = path . join ( pythoFilesPath , "afterYapf.py" ) ;
65+ let formattedContents = fs . readFile ( formattedFile , "utf-8" , ( error , data ) => {
66+ if ( error ) {
67+ return assert . fail ( error , "" , "Failed to read formatted file" ) ;
68+ }
69+ assert . equal ( activeEditor . document . getText ( ) , data , "Formatted text is not the same" ) ;
70+ } ) ;
71+ } ) ;
72+ } , error => {
73+ assert . fail ( error , "" , "Error in Formatting, " + error ) ;
74+ } ) ;
75+ } , error => {
76+ assert . fail ( error , "" , "Error in Opening Document, " + error ) ;
77+ } ) . then ( ( ) => done ( ) , done ) . then ( ( ) => closeActiveEditor ( ) , closeActiveEditor ) ;
78+ } ) ;
79+
80+ test ( "Yapf autoformat on save" , done => {
81+ let formattedFile = path . join ( pythoFilesPath , "afterYapfFormatOnSave.py" ) ;
82+ let fileToFormat = path . join ( pythoFilesPath , "beforeYapfFormatOnSave.py" ) ;
83+ let fileToCopyFrom = path . join ( pythoFilesPath , "beforeYapfFormatOnSaveOriginal.py" ) ;
84+ let formattedFileContents = fs . readFileSync ( formattedFile , "utf-8" ) ;
85+ if ( fs . existsSync ( fileToFormat ) ) { fs . unlinkSync ( fileToFormat ) ; }
86+ fs . copySync ( fileToCopyFrom , fileToFormat ) ;
87+ const FORMAT_ON_SAVE = pythonSettings . formatting . formatOnSave ;
88+ pythonSettings . formatting . formatOnSave = true ;
89+ pythonSettings . formatting . provider = "yapf" ;
90+
91+ vscode . workspace . openTextDocument ( fileToFormat ) . then ( textDocument => {
92+ return vscode . window . showTextDocument ( textDocument ) . then ( textEditor => {
93+ return textEditor . edit ( editBuilder => {
94+ editBuilder . insert ( new vscode . Position ( 0 , 0 ) , "#" ) ;
95+ } ) . then ( edited => {
96+ return textDocument . save ( ) . then ( saved => {
97+ return new Promise < any > ( ( resolve , reject ) => {
98+ setTimeout ( ( ) => {
99+ assert . equal ( textDocument . getText ( ) , formattedFileContents , "Formatted contents are not the same" ) ;
100+ resolve ( ) ;
101+ } , 1000 ) ;
102+ } ) ;
103+ } , error => {
104+ assert . fail ( error , "" , "Error in Saving Document, " + error ) ;
105+ } ) ;
106+ } , error => {
107+ assert . fail ( error , "" , "Error in Editing Document, " + error ) ;
108+ } ) ;
109+ } , error => {
110+ assert . fail ( error , "" , "Error in Showing Document, " + error ) ;
111+ } ) ;
112+ } , error => {
113+ assert . fail ( error , "" , "Error in Opening Document, " + error ) ;
114+ } ) . then ( ( ) => done ( ) , done ) . then ( ( ) => closeActiveEditor ( ) , closeActiveEditor ) ;
115+ } ) ;
116+ } ) ;
0 commit comments