@@ -45,6 +45,9 @@ describe('Format Executor', () => {
4545 isVerbose : false ,
4646 } ;
4747 dotnetClient = new DotNetClient ( mockDotnetFactory ( ) ) ;
48+ ( dotnetClient as jest . Mocked < DotNetClient > ) . printSdkVersion . mockReturnValue (
49+ Buffer . from ( '5.0.402' ) ,
50+ ) ;
4851 } ) ;
4952
5053 afterEach ( async ( ) => {
@@ -91,4 +94,111 @@ describe('Format Executor', () => {
9194 ) . toHaveBeenCalled ( ) ;
9295 expect ( res . success ) . toBeTruthy ( ) ;
9396 } ) ;
97+
98+ it ( 'installs dotnet-format if not already installed' , async ( ) => {
99+ try {
100+ const directoryPath = `${ root } /apps/my-app` ;
101+ await fs . mkdir ( directoryPath , { recursive : true } ) ;
102+ await Promise . all ( [ fs . writeFile ( `${ directoryPath } /1.csproj` , '' ) ] ) ;
103+
104+ const manifestPath = `${ root } /.config` ;
105+ await fs . mkdir ( manifestPath , { recursive : true } ) ;
106+ await fs . writeFile ( `${ manifestPath } /dotnet-tools.json` , '{"tools": {}}' ) ;
107+ } catch ( e ) {
108+ if ( assertErrorMessage ( e ) ) console . warn ( e . message ) ;
109+ }
110+
111+ const res = await executor ( options , context , dotnetClient ) ;
112+ expect (
113+ ( dotnetClient as jest . Mocked < DotNetClient > ) . installTool ,
114+ ) . toHaveBeenCalled ( ) ;
115+ expect ( res . success ) . toBeTruthy ( ) ;
116+ } ) ;
117+
118+ it ( 'does not install dotnet-format if already installed' , async ( ) => {
119+ try {
120+ const directoryPath = `${ root } /apps/my-app` ;
121+ await fs . mkdir ( directoryPath , { recursive : true } ) ;
122+ await Promise . all ( [ fs . writeFile ( `${ directoryPath } /1.csproj` , '' ) ] ) ;
123+
124+ const manifestPath = `${ root } /.config` ;
125+ await fs . mkdir ( manifestPath , { recursive : true } ) ;
126+ await fs . writeFile (
127+ `${ manifestPath } /dotnet-tools.json` ,
128+ '{"tools": {"dotnet-format": {"version": "5.1.250801"}}}' ,
129+ ) ;
130+ } catch ( e ) {
131+ if ( assertErrorMessage ( e ) ) console . warn ( e . message ) ;
132+ }
133+
134+ const res = await executor ( options , context , dotnetClient ) ;
135+ expect (
136+ ( dotnetClient as jest . Mocked < DotNetClient > ) . installTool ,
137+ ) . not . toHaveBeenCalled ( ) ;
138+ expect ( res . success ) . toBeTruthy ( ) ;
139+ } ) ;
140+
141+ it ( 'does not install dotnet-format if SDK is 6+' , async ( ) => {
142+ ( dotnetClient as jest . Mocked < DotNetClient > ) . printSdkVersion . mockReturnValue (
143+ Buffer . from ( '6.0.101' ) ,
144+ ) ;
145+
146+ try {
147+ const directoryPath = `${ root } /apps/my-app` ;
148+ await fs . mkdir ( directoryPath , { recursive : true } ) ;
149+ await Promise . all ( [ fs . writeFile ( `${ directoryPath } /1.csproj` , '' ) ] ) ;
150+
151+ const manifestPath = `${ root } /.config` ;
152+ await fs . mkdir ( manifestPath , { recursive : true } ) ;
153+ await fs . writeFile ( `${ manifestPath } /dotnet-tools.json` , '{"tools": {}}' ) ;
154+ } catch ( e ) {
155+ if ( assertErrorMessage ( e ) ) console . warn ( e . message ) ;
156+ }
157+
158+ const res = await executor ( options , context , dotnetClient ) ;
159+ expect (
160+ ( dotnetClient as jest . Mocked < DotNetClient > ) . installTool ,
161+ ) . not . toHaveBeenCalled ( ) ;
162+ expect ( res . success ) . toBeTruthy ( ) ;
163+ } ) ;
164+
165+ it ( 'passes the --check option on .NET 5 and earlier' , async ( ) => {
166+ try {
167+ const directoryPath = `${ root } /apps/my-app` ;
168+ await fs . mkdir ( directoryPath , { recursive : true } ) ;
169+ await Promise . all ( [ fs . writeFile ( `${ directoryPath } /1.csproj` , '' ) ] ) ;
170+ } catch ( e ) {
171+ if ( assertErrorMessage ( e ) ) console . warn ( e . message ) ;
172+ }
173+
174+ const res = await executor ( options , context , dotnetClient ) ;
175+ expect ( res . success ) . toBeTruthy ( ) ;
176+
177+ const formatOptions = ( dotnetClient as jest . Mocked < DotNetClient > ) . format
178+ . mock . calls [ 0 ] [ 1 ] ;
179+ const checkFlag = formatOptions ?. find ( ( o ) => o . flag == 'check' ) ;
180+ expect ( checkFlag ?. value ) . toBeTruthy ( ) ;
181+ } ) ;
182+
183+ it ( 'passes the --verify-no-changes option on .NET 6 and later' , async ( ) => {
184+ ( dotnetClient as jest . Mocked < DotNetClient > ) . printSdkVersion . mockReturnValue (
185+ Buffer . from ( '6.0.101' ) ,
186+ ) ;
187+
188+ try {
189+ const directoryPath = `${ root } /apps/my-app` ;
190+ await fs . mkdir ( directoryPath , { recursive : true } ) ;
191+ await Promise . all ( [ fs . writeFile ( `${ directoryPath } /1.csproj` , '' ) ] ) ;
192+ } catch ( e ) {
193+ if ( assertErrorMessage ( e ) ) console . warn ( e . message ) ;
194+ }
195+
196+ const res = await executor ( options , context , dotnetClient ) ;
197+ expect ( res . success ) . toBeTruthy ( ) ;
198+
199+ const formatOptions = ( dotnetClient as jest . Mocked < DotNetClient > ) . format
200+ . mock . calls [ 0 ] [ 1 ] ;
201+ const checkFlag = formatOptions ?. find ( ( o ) => o . flag == 'verifyNoChanges' ) ;
202+ expect ( checkFlag ?. value ) . toBeTruthy ( ) ;
203+ } ) ;
94204} ) ;
0 commit comments