Skip to content

Commit db20bee

Browse files
author
Vandita Patidar
committed
Checking if folder exists
1 parent 5768cf5 commit db20bee

File tree

2 files changed

+66
-11
lines changed
  • packages/core/src
    • awsService/appBuilder/serverlessLand
    • test/awsService/appBuilder/serverlessLand

2 files changed

+66
-11
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ export async function launchProjectCreationWizard(
8989
export async function downloadPatternCode(config: CreateServerlessLandWizardForm, assetName: string): Promise<void> {
9090
const fullAssetName = assetName + '.zip'
9191
const location = vscode.Uri.joinPath(config.location, config.name)
92+
93+
if (await fs.exists(location)) {
94+
const choice = await vscode.window.showInformationMessage(
95+
localize(
96+
'AWS.toolkit.serverlessLand.fileExistsPrompt',
97+
'{0} already exists in the selected directory, overwrite?',
98+
config.name
99+
),
100+
'Yes',
101+
'No'
102+
)
103+
if (choice !== 'Yes') {
104+
return Promise.reject(new ToolkitError(`A folder named ${config.name} already exists in this path.`))
105+
}
106+
await vscode.workspace.fs.delete(location)
107+
}
92108
try {
93109
await getPattern(serverlessLandOwner, serverlessLandRepo, fullAssetName, location, true)
94110
} catch (error) {

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

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
getProjectUri,
2020
downloadPatternCode,
2121
} from '../../../../awsService/appBuilder/serverlessLand/main'
22+
import { getTestWindow } from '../../../shared/vscode/window'
2223
import { fs } from '../../../../shared/fs/fs'
2324
import * as downloadPatterns from '../../../../shared/utilities/downloadPatterns'
2425
import { ExtContext } from '../../../../shared/extensions'
@@ -83,29 +84,32 @@ describe('createNewServerlessLandProject', () => {
8384
describe('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

129168
describe('openReadmeFile', () => {

0 commit comments

Comments
 (0)