@@ -44,6 +44,8 @@ suite('Formatting', () => {
4444 fs . writeFileSync ( fileToFormatWithoutConfig , fs . readFileSync ( originalFileToFormatWithoutConfig ) ) ;
4545 } ) ;
4646 teardown ( ( ) => {
47+ fs . writeFileSync ( fileToFormatWithConfig , fs . readFileSync ( originalFileToFormatWithConfig ) ) ;
48+ fs . writeFileSync ( fileToFormatWithoutConfig , fs . readFileSync ( originalFileToFormatWithoutConfig ) ) ;
4749 if ( vscode . window . activeTextEditor ) {
4850 return vscode . commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ;
4951 }
@@ -59,14 +61,30 @@ suite('Formatting', () => {
5961 textEditor = editor ;
6062 const sorter = new PythonImportSortProvider ( ) ;
6163 return sorter . sortImports ( extensionDir , textDocument ) ;
62- } ) . then ( edits => {
64+ } ) . then ( edits => {
6365 assert . equal ( edits . filter ( value => value . newText === EOL && value . range . isEqual ( new vscode . Range ( 2 , 0 , 2 , 0 ) ) ) . length , 1 , 'EOL not found' ) ;
6466 assert . equal ( edits . filter ( value => value . newText === '' && value . range . isEqual ( new vscode . Range ( 3 , 0 , 4 , 0 ) ) ) . length , 1 , '"" not found' ) ;
6567 assert . equal ( edits . filter ( value => value . newText === `from rope.base import libutils${ EOL } from rope.refactor.extract import ExtractMethod, ExtractVariable${ EOL } from rope.refactor.rename import Rename${ EOL } ` && value . range . isEqual ( new vscode . Range ( 6 , 0 , 6 , 0 ) ) ) . length , 1 , 'Text not found' ) ;
6668 assert . equal ( edits . filter ( value => value . newText === '' && value . range . isEqual ( new vscode . Range ( 13 , 0 , 18 , 0 ) ) ) . length , 1 , '"" not found' ) ;
6769 } ) . then ( done , done ) ;
6870 } ) ;
6971
72+ test ( 'Without Config (via Command)' , done => {
73+ let textEditor : vscode . TextEditor ;
74+ let textDocument : vscode . TextDocument ;
75+ let originalContent = '' ;
76+ return vscode . workspace . openTextDocument ( fileToFormatWithoutConfig ) . then ( document => {
77+ textDocument = document ;
78+ originalContent = textDocument . getText ( ) ;
79+ return vscode . window . showTextDocument ( textDocument ) ;
80+ } ) . then ( editor => {
81+ textEditor = editor ;
82+ return vscode . commands . executeCommand ( 'python.sortImports' ) ;
83+ } ) . then ( ( ) => {
84+ assert . notEqual ( originalContent , textDocument . getText ( ) , 'Contents have not changed' ) ;
85+ } ) . then ( done , done ) ;
86+ } ) ;
87+
7088 test ( 'With Config' , done => {
7189 let textEditor : vscode . TextEditor ;
7290 let textDocument : vscode . TextDocument ;
@@ -83,6 +101,22 @@ suite('Formatting', () => {
83101 } ) . then ( done , done ) ;
84102 } ) ;
85103
104+ test ( 'With Config (via Command)' , done => {
105+ let textEditor : vscode . TextEditor ;
106+ let textDocument : vscode . TextDocument ;
107+ let originalContent = '' ;
108+ return vscode . workspace . openTextDocument ( fileToFormatWithConfig ) . then ( document => {
109+ textDocument = document ;
110+ originalContent = document . getText ( ) ;
111+ return vscode . window . showTextDocument ( textDocument ) ;
112+ } ) . then ( editor => {
113+ textEditor = editor ;
114+ return vscode . commands . executeCommand ( 'python.sortImports' ) ;
115+ } ) . then ( ( ) => {
116+ assert . notEqual ( originalContent , textDocument . getText ( ) , 'Contents have not changed' ) ;
117+ } ) . then ( done , done ) ;
118+ } ) ;
119+
86120 test ( 'With Changes and Config in Args' , done => {
87121 let textEditor : vscode . TextEditor ;
88122 let textDocument : vscode . TextDocument ;
@@ -99,8 +133,32 @@ suite('Formatting', () => {
99133 const sorter = new PythonImportSortProvider ( ) ;
100134 return sorter . sortImports ( extensionDir , textDocument ) ;
101135 } ) . then ( edits => {
102- const newValue = `from third_party import lib2${ EOL } from third_party import lib3${ EOL } from third_party import lib4${ EOL } from third_party import lib5${ EOL } from third_party import lib6${ EOL } from third_party import lib7${ EOL } from third_party import lib8${ EOL } from third_party import lib9${ EOL } ` ;
103- assert . equal ( edits . filter ( value => value . newText === newValue && value . range . isEqual ( new vscode . Range ( 1 , 0 , 4 , 0 ) ) ) . length , 1 , 'New Text not found' ) ;
136+ const newValue = `from third_party import lib1${ EOL } from third_party import lib2${ EOL } from third_party import lib3${ EOL } from third_party import lib4${ EOL } from third_party import lib5${ EOL } from third_party import lib6${ EOL } from third_party import lib7${ EOL } from third_party import lib8${ EOL } from third_party import lib9${ EOL } ` ;
137+ assert . equal ( edits . length , 1 , 'Incorrect number of edits' ) ;
138+ assert . equal ( edits [ 0 ] . newText , newValue , 'New Value is not the same' ) ;
139+ assert . equal ( `${ edits [ 0 ] . range . start . line } ,${ edits [ 0 ] . range . start . character } ` , '1,0' , 'Start position is not the same' ) ;
140+ assert . equal ( `${ edits [ 0 ] . range . end . line } ,${ edits [ 0 ] . range . end . character } ` , '2,0' , 'End position is not the same' ) ;
141+ } ) . then ( done , done ) ;
142+ } ) ;
143+
144+ test ( 'With Changes and Config in Args (via Command)' , done => {
145+ let textEditor : vscode . TextEditor ;
146+ let textDocument : vscode . TextDocument ;
147+ let originalContent = '' ;
148+ pythonSettings . sortImports . args = [ '-sp' , path . join ( __dirname , '..' , '..' , 'src' , 'test' , 'pythonFiles' , 'sorting' , 'withconfig' ) ] ;
149+ return vscode . workspace . openTextDocument ( fileToFormatWithConfig ) . then ( document => {
150+ textDocument = document ;
151+ return vscode . window . showTextDocument ( textDocument ) ;
152+ } ) . then ( editor => {
153+ textEditor = editor ;
154+ return editor . edit ( editor => {
155+ editor . insert ( new vscode . Position ( 0 , 0 ) , 'from third_party import lib0' + EOL ) ;
156+ } ) ;
157+ } ) . then ( ( ) => {
158+ originalContent = textDocument . getText ( ) ;
159+ return vscode . commands . executeCommand ( 'python.sortImports' ) ;
160+ } ) . then ( edits => {
161+ assert . notEqual ( originalContent , textDocument . getText ( ) , 'Contents have not changed' ) ;
104162 } ) . then ( done , done ) ;
105163 } ) ;
106164} ) ;
0 commit comments