@@ -81,6 +81,20 @@ describe('createNewServerlessLandProject', () => {
8181 } )
8282} )
8383
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+
8498describe ( 'downloadPatternCode' , ( ) => {
8599 let sandbox : sinon . SinonSandbox
86100 let getPatternStub : sinon . SinonStub
@@ -104,64 +118,17 @@ describe('downloadPatternCode', () => {
104118 } )
105119
106120 it ( 'successfully downloads pattern code' , async ( ) => {
107- const mockAssetName = 'test-project-sam-python.zip'
108- const serverlessLandOwner = 'aws-samples'
109- const serverlessLandRepo = 'serverless-patterns'
110- const mockLocation = vscode . Uri . joinPath ( mockConfig . location , mockConfig . name )
111-
112121 await downloadPatternCode ( mockConfig , mockConfig . assetName )
113- assert ( getPatternStub . calledOnce )
114- assert ( getPatternStub . firstCall . args [ 0 ] === serverlessLandOwner )
115- assert ( getPatternStub . firstCall . args [ 1 ] === serverlessLandRepo )
116- assert ( getPatternStub . firstCall . args [ 2 ] === mockAssetName )
117- assert ( getPatternStub . firstCall . args [ 3 ] . toString ( ) === mockLocation . toString ( ) )
118- assert ( getPatternStub . firstCall . args [ 4 ] === true )
122+ assertDownloadPatternCall ( getPatternStub , mockConfig )
119123 } )
120- it ( 'handles download failure' , async ( ) => {
121- const error = new Error ( 'Download failed' )
122- getPatternStub . rejects ( error )
123- try {
124- await downloadPatternCode ( mockConfig , mockConfig . assetName )
125- assert . fail ( 'Expected an error to be thrown' )
126- } catch ( err : any ) {
127- assert . strictEqual ( err . message , 'Failed to download pattern: Error: Download failed' )
128- }
129- } )
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 )
135124
125+ it ( 'downloads pattern when directory exists and user confirms overwrite' , async function ( ) {
136126 getTestWindow ( ) . onDidShowMessage ( ( message ) => {
137127 message . selectItem ( 'Yes' )
138128 } )
139129
140130 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 ( 'Folder already exists: test-project' )
160- } catch ( e ) {
161- assert . strictEqual ( ( e as Error ) . message , `Folder already exists: ${ mockConfig . name } ` )
162- }
163- assert ( getPatternStub . notCalled )
164- existsStub . restore ( )
131+ assertDownloadPatternCall ( getPatternStub , mockConfig )
165132 } )
166133} )
167134
0 commit comments