@@ -23,6 +23,7 @@ import { fs } from '../../../../shared/fs/fs'
2323import * as downloadPatterns from '../../../../shared/utilities/downloadPatterns'
2424import { ExtContext } from '../../../../shared/extensions'
2525import { workspaceUtils } from '../../../../shared'
26+ import * as messages from '../../../../shared/utilities/messages'
2627import * as downloadPattern from '../../../../shared/utilities/downloadPatterns'
2728import * as wizardModule from '../../../../awsService/appBuilder/serverlessLand/wizard'
2829
@@ -80,63 +81,75 @@ describe('createNewServerlessLandProject', () => {
8081 } )
8182} )
8283
84+ function assertDownloadPatternCall ( getPatternStub : sinon . SinonStub , mockConfig : any ) {
85+ const mockAssetName = 'test-project-sam-python.zip'
86+ const serverlessLandOwner = 'aws-samples'
87+ const serverlessLandRepo = 'serverless-patterns'
88+ const mockLocation = vscode . Uri . joinPath ( mockConfig . location , mockConfig . name )
89+
90+ assert ( getPatternStub . calledOnce )
91+ assert ( getPatternStub . firstCall . args [ 0 ] === serverlessLandOwner )
92+ assert ( getPatternStub . firstCall . args [ 1 ] === serverlessLandRepo )
93+ assert ( getPatternStub . firstCall . args [ 2 ] === mockAssetName )
94+ assert ( getPatternStub . firstCall . args [ 3 ] . toString ( ) === mockLocation . toString ( ) )
95+ assert ( getPatternStub . firstCall . args [ 4 ] === true )
96+ }
97+
8398describe ( 'downloadPatternCode' , ( ) => {
8499 let sandbox : sinon . SinonSandbox
85100 let getPatternStub : sinon . SinonStub
101+ let mockConfig : any
86102
87103 beforeEach ( function ( ) {
88104 sandbox = sinon . createSandbox ( )
89105 getPatternStub = sandbox . stub ( downloadPatterns , 'getPattern' )
106+ mockConfig = {
107+ name : 'test-project' ,
108+ location : vscode . Uri . file ( '/test' ) ,
109+ pattern : 'test-project-sam-python' ,
110+ runtime : 'python' ,
111+ iac : 'sam' ,
112+ assetName : 'test-project-sam-python' ,
113+ }
90114 } )
91115 afterEach ( function ( ) {
92116 sandbox . restore ( )
117+ getPatternStub . restore ( )
93118 } )
94- const mockConfig = {
95- name : 'test-project' ,
96- location : vscode . Uri . file ( '/test' ) ,
97- pattern : 'test-project-sam-python' ,
98- runtime : 'python' ,
99- iac : 'sam' ,
100- assetName : 'test-project-sam-python' ,
101- }
102119 it ( 'successfully downloads pattern code' , async ( ) => {
103- const mockAssetName = 'test-project-sam-python.zip'
104- const serverlessLandOwner = 'aws-samples'
105- const serverlessLandRepo = 'serverless-patterns'
106- const mockLocation = vscode . Uri . joinPath ( mockConfig . location , mockConfig . name )
107-
108- await downloadPatternCode ( mockConfig , 'test-project-sam-python' )
109- assert ( getPatternStub . calledOnce )
110- assert ( getPatternStub . firstCall . args [ 0 ] === serverlessLandOwner )
111- assert ( getPatternStub . firstCall . args [ 1 ] === serverlessLandRepo )
112- assert ( getPatternStub . firstCall . args [ 2 ] === mockAssetName )
113- assert ( getPatternStub . firstCall . args [ 3 ] . toString ( ) === mockLocation . toString ( ) )
114- assert ( getPatternStub . firstCall . args [ 4 ] === true )
115- } )
116- it ( 'handles download failure' , async ( ) => {
117- const mockAssetName = 'test-project-sam-python.zip'
118- const error = new Error ( 'Download failed' )
119- getPatternStub . rejects ( error )
120- try {
121- await downloadPatternCode ( mockConfig , mockAssetName )
122- assert . fail ( 'Expected an error to be thrown' )
123- } catch ( err : any ) {
124- assert . strictEqual ( err . message , 'Failed to download pattern: Error: Download failed' )
125- }
120+ sandbox . stub ( messages , 'handleOverwriteConflict' ) . resolves ( true )
121+
122+ await downloadPatternCode ( mockConfig , mockConfig . assetName )
123+ assertDownloadPatternCall ( getPatternStub , mockConfig )
124+ } )
125+ it ( 'downloads pattern when directory exists and user confirms overwrite' , async function ( ) {
126+ sandbox . stub ( messages , 'handleOverwriteConflict' ) . resolves ( true )
127+
128+ await downloadPatternCode ( mockConfig , mockConfig . assetName )
129+ assertDownloadPatternCall ( getPatternStub , mockConfig )
130+ } )
131+ it ( 'throws error when directory exists and user cancels overwrite' , async function ( ) {
132+ const handleOverwriteStub = sandbox . stub ( messages , 'handleOverwriteConflict' )
133+ handleOverwriteStub . rejects ( new Error ( 'Folder already exists: test-project' ) )
134+
135+ await assert . rejects (
136+ ( ) => downloadPatternCode ( mockConfig , mockConfig . assetName ) ,
137+ / F o l d e r a l r e a d y e x i s t s : t e s t - p r o j e c t /
138+ )
126139 } )
127140} )
128141
129142describe ( 'openReadmeFile' , ( ) => {
130- let sandbox : sinon . SinonSandbox
143+ let testsandbox : sinon . SinonSandbox
131144 let spyExecuteCommand : sinon . SinonSpy
132145
133146 beforeEach ( function ( ) {
134- sandbox = sinon . createSandbox ( )
135- spyExecuteCommand = sandbox . spy ( vscode . commands , 'executeCommand' )
147+ testsandbox = sinon . createSandbox ( )
148+ spyExecuteCommand = testsandbox . spy ( vscode . commands , 'executeCommand' )
136149 } )
137150
138151 afterEach ( function ( ) {
139- sandbox . restore ( )
152+ testsandbox . restore ( )
140153 } )
141154 const mockConfig = {
142155 name : 'test-project' ,
@@ -148,43 +161,43 @@ describe('openReadmeFile', () => {
148161 }
149162 it ( 'successfully opens README file' , async ( ) => {
150163 const mockReadmeUri = vscode . Uri . file ( '/test/README.md' )
151- sandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
164+ testsandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
152165
153- sandbox . stub ( fs , 'exists' ) . resolves ( true )
166+ testsandbox . stub ( fs , 'exists' ) . resolves ( true )
154167
155168 // When
156169 await openReadmeFile ( mockConfig )
157170 // Then
158- sandbox . assert . calledWith ( spyExecuteCommand , 'workbench.action.focusFirstEditorGroup' )
159- sandbox . assert . calledWith ( spyExecuteCommand , 'markdown.showPreview' )
171+ testsandbox . assert . calledWith ( spyExecuteCommand , 'workbench.action.focusFirstEditorGroup' )
172+ testsandbox . assert . calledWith ( spyExecuteCommand , 'markdown.showPreview' )
160173 } )
161174
162175 it ( 'handles missing README file' , async ( ) => {
163176 const mockReadmeUri = vscode . Uri . file ( '/test/file.md' )
164- sandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
177+ testsandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
165178
166- sandbox . stub ( fs , 'exists' ) . resolves ( false )
179+ testsandbox . stub ( fs , 'exists' ) . resolves ( false )
167180
168181 // When
169182 await openReadmeFile ( mockConfig )
170183 // Then
171- sandbox . assert . neverCalledWith ( spyExecuteCommand , 'markdown.showPreview' )
184+ testsandbox . assert . neverCalledWith ( spyExecuteCommand , 'markdown.showPreview' )
172185 assert . ok ( true , 'Function should return without throwing error when README is not found' )
173186 } )
174187
175188 it ( 'handles error with opening README file' , async ( ) => {
176189 const mockReadmeUri = vscode . Uri . file ( '/test/README.md' )
177- sandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
190+ testsandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
178191
179- sandbox . stub ( fs , 'exists' ) . rejects ( new Error ( 'File system error' ) )
192+ testsandbox . stub ( fs , 'exists' ) . rejects ( new Error ( 'File system error' ) )
180193
181194 // When
182195 await assert . rejects ( ( ) => openReadmeFile ( mockConfig ) , {
183196 name : 'Error' ,
184197 message : 'Error processing README file' ,
185198 } )
186199 // Then
187- sandbox . assert . neverCalledWith ( spyExecuteCommand , 'markdown.showPreview' )
200+ testsandbox . assert . neverCalledWith ( spyExecuteCommand , 'markdown.showPreview' )
188201 } )
189202} )
190203
0 commit comments