@@ -19,6 +19,7 @@ import {
1919 getProjectUri ,
2020 downloadPatternCode ,
2121} from '../../../../awsService/appBuilder/serverlessLand/main'
22+ import { getTestWindow } from '../../../shared/vscode/window'
2223import { fs } from '../../../../shared/fs/fs'
2324import * as downloadPatterns from '../../../../shared/utilities/downloadPatterns'
2425import { ExtContext } from '../../../../shared/extensions'
@@ -83,29 +84,32 @@ describe('createNewServerlessLandProject', () => {
8384describe ( 'downloadPatternCode' , ( ) => {
8485 let sandbox : sinon . SinonSandbox
8586 let getPatternStub : sinon . SinonStub
87+ let mockConfig : any
8688
8789 beforeEach ( function ( ) {
8890 sandbox = sinon . createSandbox ( )
8991 getPatternStub = sandbox . stub ( downloadPatterns , 'getPattern' )
92+ mockConfig = {
93+ name : 'test-project' ,
94+ location : vscode . Uri . file ( '/test' ) ,
95+ pattern : 'test-project-sam-python' ,
96+ runtime : 'python' ,
97+ iac : 'sam' ,
98+ assetName : 'test-project-sam-python' ,
99+ }
90100 } )
91101 afterEach ( function ( ) {
92102 sandbox . restore ( )
103+ getPatternStub . restore ( )
93104 } )
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- }
105+
102106 it ( 'successfully downloads pattern code' , async ( ) => {
103107 const mockAssetName = 'test-project-sam-python.zip'
104108 const serverlessLandOwner = 'aws-samples'
105109 const serverlessLandRepo = 'serverless-patterns'
106110 const mockLocation = vscode . Uri . joinPath ( mockConfig . location , mockConfig . name )
107111
108- await downloadPatternCode ( mockConfig , 'test-project-sam-python' )
112+ await downloadPatternCode ( mockConfig , mockConfig . assetName )
109113 assert ( getPatternStub . calledOnce )
110114 assert ( getPatternStub . firstCall . args [ 0 ] === serverlessLandOwner )
111115 assert ( getPatternStub . firstCall . args [ 1 ] === serverlessLandRepo )
@@ -114,16 +118,51 @@ describe('downloadPatternCode', () => {
114118 assert ( getPatternStub . firstCall . args [ 4 ] === true )
115119 } )
116120 it ( 'handles download failure' , async ( ) => {
117- const mockAssetName = 'test-project-sam-python.zip'
118121 const error = new Error ( 'Download failed' )
119122 getPatternStub . rejects ( error )
120123 try {
121- await downloadPatternCode ( mockConfig , mockAssetName )
124+ await downloadPatternCode ( mockConfig , mockConfig . assetName )
122125 assert . fail ( 'Expected an error to be thrown' )
123126 } catch ( err : any ) {
124127 assert . strictEqual ( err . message , 'Failed to download pattern: Error: Download failed' )
125128 }
126129 } )
130+ it ( 'downloads pattern when directory exists and user confirms overwrite' , async function ( ) {
131+ const mockAssetName = 'test-project-sam-python.zip'
132+ const serverlessLandOwner = 'aws-samples'
133+ const serverlessLandRepo = 'serverless-patterns'
134+ const mockLocation = vscode . Uri . joinPath ( mockConfig . location , mockConfig . name )
135+
136+ getTestWindow ( ) . onDidShowMessage ( ( message ) => {
137+ message . selectItem ( 'Yes' )
138+ } )
139+
140+ await downloadPatternCode ( mockConfig , mockConfig . assetName )
141+ assert ( getPatternStub . calledOnce )
142+ assert ( getPatternStub . firstCall . args [ 0 ] === serverlessLandOwner )
143+ assert ( getPatternStub . firstCall . args [ 1 ] === serverlessLandRepo )
144+ assert ( getPatternStub . firstCall . args [ 2 ] === mockAssetName )
145+ assert ( getPatternStub . firstCall . args [ 3 ] . toString ( ) === mockLocation . toString ( ) )
146+ assert ( getPatternStub . firstCall . args [ 4 ] === true )
147+ } )
148+ it ( 'aborts download when directory exists and user declines overwrite' , async function ( ) {
149+ const existsStub = sinon . stub ( fs , 'exists' ) . resolves ( true )
150+
151+ const messagePromise = new Promise < void > ( ( resolve ) => {
152+ getTestWindow ( ) . onDidShowMessage ( ( message ) => {
153+ resolve ( )
154+ message . selectItem ( 'No' )
155+ } )
156+ } )
157+ try {
158+ await Promise . all ( [ messagePromise , downloadPatternCode ( mockConfig , mockConfig . assetName ) ] )
159+ assert . fail ( 'A folder named test-project already exists in this path.' )
160+ } catch ( e ) {
161+ assert . strictEqual ( ( e as Error ) . message , `A folder named ${ mockConfig . name } already exists in this path.` )
162+ }
163+ assert ( getPatternStub . notCalled )
164+ existsStub . restore ( )
165+ } )
127166} )
128167
129168describe ( 'openReadmeFile' , ( ) => {
0 commit comments