Skip to content

Commit 3b02aac

Browse files
author
Vandita Patidar
committed
Changes to solve duplicate error
1 parent c424454 commit 3b02aac

File tree

1 file changed

+31
-64
lines changed
  • packages/core/src/test/awsService/appBuilder/serverlessLand

1 file changed

+31
-64
lines changed

packages/core/src/test/awsService/appBuilder/serverlessLand/main.test.ts

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
8498
describe('downloadPatternCode', () => {
8599
let sandbox: sinon.SinonSandbox
86100
let getPatternStub: sinon.SinonStub
@@ -104,78 +118,31 @@ 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)
119-
})
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-
}
122+
assertDownloadPatternCall(getPatternStub, mockConfig)
129123
})
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

168135
describe('openReadmeFile', () => {
169-
let sandbox: sinon.SinonSandbox
136+
let testsandbox: sinon.SinonSandbox
170137
let spyExecuteCommand: sinon.SinonSpy
171138

172139
beforeEach(function () {
173-
sandbox = sinon.createSandbox()
174-
spyExecuteCommand = sandbox.spy(vscode.commands, 'executeCommand')
140+
testsandbox = sinon.createSandbox()
141+
spyExecuteCommand = testsandbox.spy(vscode.commands, 'executeCommand')
175142
})
176143

177144
afterEach(function () {
178-
sandbox.restore()
145+
testsandbox.restore()
179146
})
180147
const mockConfig = {
181148
name: 'test-project',
@@ -187,43 +154,43 @@ describe('openReadmeFile', () => {
187154
}
188155
it('successfully opens README file', async () => {
189156
const mockReadmeUri = vscode.Uri.file('/test/README.md')
190-
sandbox.stub(main, 'getProjectUri').resolves(mockReadmeUri)
157+
testsandbox.stub(main, 'getProjectUri').resolves(mockReadmeUri)
191158

192-
sandbox.stub(fs, 'exists').resolves(true)
159+
testsandbox.stub(fs, 'exists').resolves(true)
193160

194161
// When
195162
await openReadmeFile(mockConfig)
196163
// Then
197-
sandbox.assert.calledWith(spyExecuteCommand, 'workbench.action.focusFirstEditorGroup')
198-
sandbox.assert.calledWith(spyExecuteCommand, 'markdown.showPreview')
164+
testsandbox.assert.calledWith(spyExecuteCommand, 'workbench.action.focusFirstEditorGroup')
165+
testsandbox.assert.calledWith(spyExecuteCommand, 'markdown.showPreview')
199166
})
200167

201168
it('handles missing README file', async () => {
202169
const mockReadmeUri = vscode.Uri.file('/test/file.md')
203-
sandbox.stub(main, 'getProjectUri').resolves(mockReadmeUri)
170+
testsandbox.stub(main, 'getProjectUri').resolves(mockReadmeUri)
204171

205-
sandbox.stub(fs, 'exists').resolves(false)
172+
testsandbox.stub(fs, 'exists').resolves(false)
206173

207174
// When
208175
await openReadmeFile(mockConfig)
209176
// Then
210-
sandbox.assert.neverCalledWith(spyExecuteCommand, 'markdown.showPreview')
177+
testsandbox.assert.neverCalledWith(spyExecuteCommand, 'markdown.showPreview')
211178
assert.ok(true, 'Function should return without throwing error when README is not found')
212179
})
213180

214181
it('handles error with opening README file', async () => {
215182
const mockReadmeUri = vscode.Uri.file('/test/README.md')
216-
sandbox.stub(main, 'getProjectUri').resolves(mockReadmeUri)
183+
testsandbox.stub(main, 'getProjectUri').resolves(mockReadmeUri)
217184

218-
sandbox.stub(fs, 'exists').rejects(new Error('File system error'))
185+
testsandbox.stub(fs, 'exists').rejects(new Error('File system error'))
219186

220187
// When
221188
await assert.rejects(() => openReadmeFile(mockConfig), {
222189
name: 'Error',
223190
message: 'Error processing README file',
224191
})
225192
// Then
226-
sandbox.assert.neverCalledWith(spyExecuteCommand, 'markdown.showPreview')
193+
testsandbox.assert.neverCalledWith(spyExecuteCommand, 'markdown.showPreview')
227194
})
228195
})
229196

0 commit comments

Comments
 (0)