@@ -23,6 +23,7 @@ import { fs } from '../../../../shared/fs/fs'
23
23
import * as downloadPatterns from '../../../../shared/utilities/downloadPatterns'
24
24
import { ExtContext } from '../../../../shared/extensions'
25
25
import { workspaceUtils } from '../../../../shared'
26
+ import * as messages from '../../../../shared/utilities/messages'
26
27
import * as downloadPattern from '../../../../shared/utilities/downloadPatterns'
27
28
import * as wizardModule from '../../../../awsService/appBuilder/serverlessLand/wizard'
28
29
@@ -80,63 +81,75 @@ describe('createNewServerlessLandProject', () => {
80
81
} )
81
82
} )
82
83
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
+
83
98
describe ( 'downloadPatternCode' , ( ) => {
84
99
let sandbox : sinon . SinonSandbox
85
100
let getPatternStub : sinon . SinonStub
101
+ let mockConfig : any
86
102
87
103
beforeEach ( function ( ) {
88
104
sandbox = sinon . createSandbox ( )
89
105
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
+ }
90
114
} )
91
115
afterEach ( function ( ) {
92
116
sandbox . restore ( )
117
+ getPatternStub . restore ( )
93
118
} )
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
- }
102
119
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
+ )
126
139
} )
127
140
} )
128
141
129
142
describe ( 'openReadmeFile' , ( ) => {
130
- let sandbox : sinon . SinonSandbox
143
+ let testsandbox : sinon . SinonSandbox
131
144
let spyExecuteCommand : sinon . SinonSpy
132
145
133
146
beforeEach ( function ( ) {
134
- sandbox = sinon . createSandbox ( )
135
- spyExecuteCommand = sandbox . spy ( vscode . commands , 'executeCommand' )
147
+ testsandbox = sinon . createSandbox ( )
148
+ spyExecuteCommand = testsandbox . spy ( vscode . commands , 'executeCommand' )
136
149
} )
137
150
138
151
afterEach ( function ( ) {
139
- sandbox . restore ( )
152
+ testsandbox . restore ( )
140
153
} )
141
154
const mockConfig = {
142
155
name : 'test-project' ,
@@ -148,43 +161,43 @@ describe('openReadmeFile', () => {
148
161
}
149
162
it ( 'successfully opens README file' , async ( ) => {
150
163
const mockReadmeUri = vscode . Uri . file ( '/test/README.md' )
151
- sandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
164
+ testsandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
152
165
153
- sandbox . stub ( fs , 'exists' ) . resolves ( true )
166
+ testsandbox . stub ( fs , 'exists' ) . resolves ( true )
154
167
155
168
// When
156
169
await openReadmeFile ( mockConfig )
157
170
// 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' )
160
173
} )
161
174
162
175
it ( 'handles missing README file' , async ( ) => {
163
176
const mockReadmeUri = vscode . Uri . file ( '/test/file.md' )
164
- sandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
177
+ testsandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
165
178
166
- sandbox . stub ( fs , 'exists' ) . resolves ( false )
179
+ testsandbox . stub ( fs , 'exists' ) . resolves ( false )
167
180
168
181
// When
169
182
await openReadmeFile ( mockConfig )
170
183
// Then
171
- sandbox . assert . neverCalledWith ( spyExecuteCommand , 'markdown.showPreview' )
184
+ testsandbox . assert . neverCalledWith ( spyExecuteCommand , 'markdown.showPreview' )
172
185
assert . ok ( true , 'Function should return without throwing error when README is not found' )
173
186
} )
174
187
175
188
it ( 'handles error with opening README file' , async ( ) => {
176
189
const mockReadmeUri = vscode . Uri . file ( '/test/README.md' )
177
- sandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
190
+ testsandbox . stub ( main , 'getProjectUri' ) . resolves ( mockReadmeUri )
178
191
179
- sandbox . stub ( fs , 'exists' ) . rejects ( new Error ( 'File system error' ) )
192
+ testsandbox . stub ( fs , 'exists' ) . rejects ( new Error ( 'File system error' ) )
180
193
181
194
// When
182
195
await assert . rejects ( ( ) => openReadmeFile ( mockConfig ) , {
183
196
name : 'Error' ,
184
197
message : 'Error processing README file' ,
185
198
} )
186
199
// Then
187
- sandbox . assert . neverCalledWith ( spyExecuteCommand , 'markdown.showPreview' )
200
+ testsandbox . assert . neverCalledWith ( spyExecuteCommand , 'markdown.showPreview' )
188
201
} )
189
202
} )
190
203
0 commit comments