Skip to content

Commit 352dfdf

Browse files
fix test utilities that are using fs-extra
- There were many changes required since the toFile method now returns a promise and must be `await`ed Signed-off-by: nkomonen <[email protected]>
1 parent fe4687a commit 352dfdf

File tree

14 files changed

+136
-123
lines changed

14 files changed

+136
-123
lines changed

src/test/amazonqFeatureDev/controllers/chat/controller.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('Controller', () => {
6969
it('uses file location when file is found locally and /src is not available', async () => {
7070
sinon.stub(controllerSetup.sessionStorage, 'getSession').resolves(session)
7171
const newFileLocation = path.join(controllerSetup.workspaceFolder.uri.fsPath, 'mynewfile.js')
72-
toFile('', newFileLocation)
72+
await toFile('', newFileLocation)
7373
const executedDiff = await openDiff('mynewfile.js')
7474
assert.strictEqual(
7575
executedDiff.calledWith(
@@ -86,7 +86,7 @@ describe('Controller', () => {
8686
it('uses file location when file is found locally and /src is available', async () => {
8787
sinon.stub(controllerSetup.sessionStorage, 'getSession').resolves(session)
8888
const newFileLocation = path.join(controllerSetup.workspaceFolder.uri.fsPath, 'src', 'mynewfile.js')
89-
toFile('', newFileLocation)
89+
await toFile('', newFileLocation)
9090
const executedDiff = await openDiff(path.join('src', 'mynewfile.js'))
9191
assert.strictEqual(
9292
executedDiff.calledWith(
@@ -103,7 +103,7 @@ describe('Controller', () => {
103103
it('uses file location when file is found locally and source folder was picked', async () => {
104104
sinon.stub(controllerSetup.sessionStorage, 'getSession').resolves(session)
105105
const newFileLocation = path.join(controllerSetup.workspaceFolder.uri.fsPath, 'foo', 'fi', 'mynewfile.js')
106-
toFile('', newFileLocation)
106+
await toFile('', newFileLocation)
107107
sinon.stub(vscode.workspace, 'getWorkspaceFolder').returns(controllerSetup.workspaceFolder)
108108
session.config.sourceRoot = path.join(controllerSetup.workspaceFolder.uri.fsPath, 'foo', 'fi')
109109
const executedDiff = await openDiff(path.join('foo', 'fi', 'mynewfile.js'))

src/test/amazonqFeatureDev/util/files.test.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ describe('file utils', () => {
6060

6161
const workspace = await createTestWorkspace(fileAmount, { fileNamePrefix, fileContent })
6262

63-
const writeFile = (pathParts: string[], fileContent: string) => {
64-
toFile(fileContent, workspace.uri.fsPath, ...pathParts)
63+
const writeFile = async (pathParts: string[], fileContent: string) => {
64+
await toFile(fileContent, workspace.uri.fsPath, ...pathParts)
6565
}
6666

6767
sinon.stub(vscode.workspace, 'workspaceFolders').value([workspace])
@@ -74,26 +74,26 @@ describe('file utils', () => {
7474
7575
range_file[0-5]
7676
`
77-
writeFile(['.gitignore'], gitignoreContent)
77+
await writeFile(['.gitignore'], gitignoreContent)
7878

79-
writeFile(['build', `ignored1`], fileContent)
80-
writeFile(['build', `ignored2`], fileContent)
79+
await writeFile(['build', `ignored1`], fileContent)
80+
await writeFile(['build', `ignored2`], fileContent)
8181

82-
writeFile(['node_modules', `ignored1`], fileContent)
83-
writeFile(['node_modules', `ignored2`], fileContent)
82+
await writeFile(['node_modules', `ignored1`], fileContent)
83+
await writeFile(['node_modules', `ignored2`], fileContent)
8484

85-
writeFile([`range_file0`], fileContent)
86-
writeFile([`range_file9`], fileContent)
85+
await writeFile([`range_file0`], fileContent)
86+
await writeFile([`range_file9`], fileContent)
8787

8888
const gitignore2 = 'folder1\n'
89-
writeFile(['src', '.gitignore'], gitignore2)
90-
writeFile(['src', 'folder2', 'a.js'], fileContent)
89+
await writeFile(['src', '.gitignore'], gitignore2)
90+
await writeFile(['src', 'folder2', 'a.js'], fileContent)
9191

9292
const gitignore3 = `negate_test*
9393
!negate_test[0-5]`
94-
writeFile(['src', 'folder3', '.gitignore'], gitignore3)
95-
writeFile(['src', 'folder3', 'negate_test1'], fileContent)
96-
writeFile(['src', 'folder3', 'negate_test6'], fileContent)
94+
await writeFile(['src', 'folder3', '.gitignore'], gitignore3)
95+
await writeFile(['src', 'folder3', 'negate_test1'], fileContent)
96+
await writeFile(['src', 'folder3', 'negate_test6'], fileContent)
9797

9898
const result = await collectFiles(workspace.uri.fsPath, true)
9999
result.sort((l, r) => l.filePath.localeCompare(r.filePath))
@@ -146,14 +146,14 @@ describe('file utils', () => {
146146
const fileContent = ''
147147
for (const fmt of ['txt', 'md']) {
148148
// root license files
149-
toFile(fileContent, workspace.uri.fsPath, `license.${fmt}`)
150-
toFile(fileContent, workspace.uri.fsPath, `License.${fmt}`)
151-
toFile(fileContent, workspace.uri.fsPath, `LICENSE.${fmt}`)
149+
await toFile(fileContent, workspace.uri.fsPath, `license.${fmt}`)
150+
await toFile(fileContent, workspace.uri.fsPath, `License.${fmt}`)
151+
await toFile(fileContent, workspace.uri.fsPath, `LICENSE.${fmt}`)
152152

153153
// nested license files
154-
toFile(fileContent, workspace.uri.fsPath, 'src', `license.${fmt}`)
155-
toFile(fileContent, workspace.uri.fsPath, 'src', `License.${fmt}`)
156-
toFile(fileContent, workspace.uri.fsPath, 'src', `LICENSE.${fmt}`)
154+
await toFile(fileContent, workspace.uri.fsPath, 'src', `license.${fmt}`)
155+
await toFile(fileContent, workspace.uri.fsPath, 'src', `License.${fmt}`)
156+
await toFile(fileContent, workspace.uri.fsPath, 'src', `LICENSE.${fmt}`)
157157
}
158158

159159
const result = await collectFiles(workspace.uri.fsPath, true)

src/test/codewhisperer/commands/transformByQ.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('transformByQ', function () {
9090
it('WHEN validateProjectSelection called on project with pom.xml but no class files THEN throws error', async function () {
9191
const folder = await createTestWorkspaceFolder()
9292
const dummyPomPath = path.join(folder.uri.fsPath, 'pom.xml')
93-
toFile('', dummyPomPath)
93+
await toFile('', dummyPomPath)
9494
const findFilesStub = sinon.stub(vscode.workspace, 'findFiles')
9595
findFilesStub.onFirstCall().resolves([])
9696
const dummyQuickPickItem: vscode.QuickPickItem = {

src/test/codewhisperer/util/crossFileContextUtil.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ describe('crossFileContextUtil', function () {
243243
tempFolder = (await createTestWorkspaceFolder()).uri.fsPath
244244
})
245245

246-
it('should split file to a chunk of 2 lines', function () {
246+
it('should split file to a chunk of 2 lines', async function () {
247247
const filePath = path.join(tempFolder, 'file.txt')
248-
toFile('line_1\nline_2\nline_3\nline_4\nline_5\nline_6\nline_7', filePath)
248+
await toFile('line_1\nline_2\nline_3\nline_4\nline_5\nline_6\nline_7', filePath)
249249

250250
const chunks = crossFile.splitFileToChunks(filePath, 2)
251251

@@ -256,9 +256,9 @@ describe('crossFileContextUtil', function () {
256256
assert.strictEqual(chunks[3].content, 'line_7')
257257
})
258258

259-
it('should split file to a chunk of 5 lines', function () {
259+
it('should split file to a chunk of 5 lines', async function () {
260260
const filePath = path.join(tempFolder, 'file.txt')
261-
toFile('line_1\nline_2\nline_3\nline_4\nline_5\nline_6\nline_7', filePath)
261+
await toFile('line_1\nline_2\nline_3\nline_4\nline_5\nline_6\nline_7', filePath)
262262

263263
const chunks = crossFile.splitFileToChunks(filePath, 5)
264264

@@ -267,9 +267,9 @@ describe('crossFileContextUtil', function () {
267267
assert.strictEqual(chunks[1].content, 'line_6\nline_7')
268268
})
269269

270-
it('codewhisperer crossfile config should use 10 lines', function () {
270+
it('codewhisperer crossfile config should use 10 lines', async function () {
271271
const filePath = path.join(tempFolder, 'file.txt')
272-
toFile(sampleFileOf60Lines, filePath)
272+
await toFile(sampleFileOf60Lines, filePath)
273273

274274
const chunks = crossFile.splitFileToChunks(filePath, crossFileContextConfig.numberOfLinesEachChunk)
275275
assert.strictEqual(chunks.length, 6)

src/test/fakeExtensionContext.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { mkdirp } from 'fs-extra'
76
import * as vscode from 'vscode'
87
import * as path from 'path'
98
import { CredentialsStore } from '../auth/credentials/store'
@@ -26,6 +25,7 @@ import { FakeChildProcessResult, TestSamCliProcessInvoker } from './shared/sam/c
2625
import { createTestWorkspaceFolder } from './testUtil'
2726
import { FakeAwsContext } from './utilities/fakeAwsContext'
2827
import { createTestRegionProvider } from './shared/regions/testUtil'
28+
import { fsCommon } from '../srcShared/fs'
2929

3030
export interface FakeMementoStorage {
3131
[key: string]: any
@@ -107,8 +107,8 @@ export class FakeExtensionContext implements vscode.ExtensionContext {
107107
const folder = await createTestWorkspaceFolder('test')
108108
ctx.globalStorageUri = vscode.Uri.joinPath(folder.uri, 'globalStorage')
109109
ctx.logUri = vscode.Uri.joinPath(folder.uri, 'logs')
110-
await mkdirp(ctx.globalStorageUri.fsPath)
111-
await mkdirp(ctx.logUri.fsPath)
110+
await fsCommon.mkdir(ctx.globalStorageUri.fsPath)
111+
await fsCommon.mkdir(ctx.logUri.fsPath)
112112
return ctx
113113
}
114114

src/test/lambda/commands/createNewSamApp.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('createNewSamApp', function () {
5353
tempFolder = await makeTemporaryToolkitFolder()
5454
tempTemplate = vscode.Uri.file(path.join(tempFolder, 'test.yaml'))
5555
fakeTarget = path.join(tempFolder, templateYaml)
56-
testutil.toFile('target file', fakeTarget)
56+
await testutil.toFile('target file', fakeTarget)
5757

5858
fakeWorkspaceFolder = {
5959
uri: vscode.Uri.file(path.dirname(tempFolder)),
@@ -96,7 +96,7 @@ describe('createNewSamApp', function () {
9696
fs.unlinkSync(fakeTarget)
9797
tempTemplate = vscode.Uri.file(path.join(tempFolder, 'test.yml'))
9898
fakeTarget = path.join(tempFolder, 'template.yml')
99-
testutil.toFile('target file', fakeTarget)
99+
await testutil.toFile('target file', fakeTarget)
100100
assert.strictEqual(
101101
normalize((await getProjectUri(fakeResponse, samInitTemplateFiles))?.fsPath ?? ''),
102102
normalize(fakeTarget)
@@ -112,7 +112,7 @@ describe('createNewSamApp', function () {
112112

113113
describe('addInitialLaunchConfiguration', function () {
114114
it('produces and returns initial launch configurations', async function () {
115-
testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
115+
await testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
116116

117117
// without runtime
118118
await (await globals.templateRegistry).addItem(tempTemplate)
@@ -147,7 +147,7 @@ describe('createNewSamApp', function () {
147147
})
148148

149149
it('produces and returns initial launch configurations with runtime', async function () {
150-
testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
150+
await testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
151151

152152
// without runtime
153153
await (await globals.templateRegistry).addItem(tempTemplate)
@@ -184,7 +184,7 @@ describe('createNewSamApp', function () {
184184
})
185185

186186
it('returns a blank array if it does not match any launch configs', async function () {
187-
testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
187+
await testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
188188

189189
await (await globals.templateRegistry).addItem(tempTemplate)
190190
const launchConfigs = await addInitialLaunchConfiguration(
@@ -200,7 +200,7 @@ describe('createNewSamApp', function () {
200200
it('produces a launch config when config has a relative path', async function () {
201201
;(fakeConfig as any).invokeTarget.templatePath = 'test.yaml'
202202

203-
testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
203+
await testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
204204

205205
await (await globals.templateRegistry).addItem(tempTemplate)
206206
const launchConfigs = await addInitialLaunchConfiguration(
@@ -231,10 +231,10 @@ describe('createNewSamApp', function () {
231231
const otherTemplate1: vscode.Uri = vscode.Uri.file(path.join(otherFolder1, 'test.yaml'))
232232
const otherTemplate2: vscode.Uri = vscode.Uri.file(path.join(otherFolder2, 'test.yaml'))
233233

234-
testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
235-
testutil.toFile(makeSampleSamTemplateYaml(true), otherTemplate1.fsPath)
236-
testutil.toFile(makeSampleSamTemplateYaml(true), otherTemplate2.fsPath)
237-
testutil.toFile('target file', path.join(otherFolder1, templateYaml))
234+
await testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
235+
await testutil.toFile(makeSampleSamTemplateYaml(true), otherTemplate1.fsPath)
236+
await testutil.toFile(makeSampleSamTemplateYaml(true), otherTemplate2.fsPath)
237+
await testutil.toFile('target file', path.join(otherFolder1, templateYaml))
238238

239239
await (await globals.templateRegistry).addItem(tempTemplate)
240240
await (await globals.templateRegistry).addItem(otherTemplate1)

src/test/lambda/commands/uploadLambda.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('uploadLambda', async function () {
3838
})
3939

4040
it('finds application.json file from dir path - flat', async function () {
41-
toFile('top secret data', path.join(tempFolder, '.application.json'))
41+
await toFile('top secret data', path.join(tempFolder, '.application.json'))
4242
assertEqualPaths(
4343
(await findApplicationJsonFile(folderUri))?.fsPath ?? '',
4444
path.join(tempFolder, '.application.json')
@@ -53,7 +53,7 @@ describe('uploadLambda', async function () {
5353
it('finds application.json file from dir path - nested', async function () {
5454
const subfolder = path.join(tempFolder, 'one', 'two')
5555
const appjsonPath = path.join(subfolder, '.application.json')
56-
toFile('top secret data', appjsonPath)
56+
await toFile('top secret data', appjsonPath)
5757

5858
assertEqualPaths((await findApplicationJsonFile(folderUri))?.fsPath ?? '', appjsonPath)
5959
// Also test Cloud9 temporary workaround.
@@ -63,8 +63,8 @@ describe('uploadLambda', async function () {
6363
it('finds application.json file from template file path', async function () {
6464
const templateUri = vscode.Uri.file(path.join(tempFolder, 'template.yaml'))
6565
const appjsonPath = path.join(tempFolder, '.application.json')
66-
toFile('SAM stuff...', templateUri.fsPath)
67-
toFile('top secret data', appjsonPath)
66+
await toFile('SAM stuff...', templateUri.fsPath)
67+
await toFile('top secret data', appjsonPath)
6868

6969
assertEqualPaths((await findApplicationJsonFile(templateUri))?.fsPath ?? '', appjsonPath)
7070
// Also test Cloud9 temporary workaround.
@@ -73,7 +73,7 @@ describe('uploadLambda', async function () {
7373

7474
it('lists functions from .application.json', async function () {
7575
const filePath = path.join(tempFolder, '.application.json')
76-
toFile(dotApplicationJsonData, filePath)
76+
await toFile(dotApplicationJsonData, filePath)
7777
const foundFunctions1 = getFunctionNames(vscode.Uri.file(filePath), 'us-west-2')
7878
const foundFunctions2 = getFunctionNames(vscode.Uri.file(filePath), 'us-east-1')
7979
assert.deepStrictEqual(foundFunctions1, ['sampleFunction-w2'])
@@ -83,7 +83,7 @@ describe('uploadLambda', async function () {
8383
it('invalid .application.json', async function () {
8484
const filePath = path.join(tempFolder, '.application.json')
8585
const invalidJson = '{ "DeploymentMethod": "lambda", "Functions": { ?? } }'
86-
toFile(invalidJson, filePath)
86+
await toFile(invalidJson, filePath)
8787
assert.deepStrictEqual(getFunctionNames(vscode.Uri.file(filePath), 'us-west-2'), undefined)
8888
assert.deepStrictEqual(getFunctionNames(vscode.Uri.file(filePath), 'us-east-1'), undefined)
8989
})

src/test/shared/awsFiletypes.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('awsFiletypes', function () {
2525
// so this will trigger "file_editAwsFile" telemetry.
2626
const awsConfigFile = path.join(sysutil.SystemUtilities.getHomeDirectory(), '.aws/test_awstoolkit')
2727
awsConfigUri = vscode.Uri.file(awsConfigFile)
28-
testUtil.toFile('Test file from the aws-toolkit-vscode test suite.', awsConfigFile)
28+
await testUtil.toFile('Test file from the aws-toolkit-vscode test suite.', awsConfigFile)
2929

3030
const cfnFile = workspaceUtils.tryGetAbsolutePath(
3131
vscode.workspace.workspaceFolders?.[0],

src/test/shared/debug/launchConfiguration.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ describe('LaunchConfiguration', function () {
9999
const workspace = vscode.workspace.workspaceFolders![0]
100100
const testLaunchJson = vscode.Uri.file(path.join(workspace.uri.fsPath, '.vscode/launch.json'))
101101
/** Object representation of the launch.json test file. */
102-
const testLaunchJsonData = JSON.parse(testutil.fromFile(testLaunchJson.fsPath))
102+
let testLaunchJsonData: any
103103
const templateUriJsPlainApp = vscode.Uri.file(path.join(workspace.uri.fsPath, 'js-plain-sam-app/template.yaml'))
104104
const templateUriPython37 = vscode.Uri.file(
105105
path.join(workspace.uri.fsPath, 'python3.7-plain-sam-app/template.yaml')
106106
)
107107
const templateUriCsharp = vscode.Uri.file(path.join(workspace.uri.fsPath, 'csharp6-zip/template.yaml'))
108108

109109
beforeEach(async function () {
110+
testLaunchJsonData = JSON.parse(await testutil.fromFile(testLaunchJson.fsPath))
110111
const registry = await globals.templateRegistry
111112
registry.addWatchPatterns([CloudFormation.templateFileGlobPattern])
112113
await registry.rebuild()

src/test/shared/fs/templateRegistry.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ describe('CloudFormation Template Registry', async function () {
372372
}
373373

374374
it('checks for an exact handler match to a relative path in a Dockerfile for image functions', async function () {
375-
toFile('CMD: ["index.handler"]', path.join(helloPath, 'Dockerfile'))
376-
toFile('CMD: ["index.handler"]', path.join(nestedPath, 'Dockerfile'))
375+
await toFile('CMD: ["index.handler"]', path.join(helloPath, 'Dockerfile'))
376+
await toFile('CMD: ["index.handler"]', path.join(nestedPath, 'Dockerfile'))
377377

378378
const helloWorldResource = {
379379
...resource,
@@ -442,8 +442,8 @@ describe('CloudFormation Template Registry', async function () {
442442
})
443443

444444
it('checks for an exact handler match for C# files in a Dockerfile for image functions', async function () {
445-
toFile('CMD: ["HelloWorld::HelloWorld.Function::FunctionHandler"]', path.join(helloPath, 'Dockerfile'))
446-
toFile('CMD: ["HelloWorld::HelloWorld.Function::FunctionHandler"]', path.join(nestedPath, 'Dockerfile'))
445+
await toFile('CMD: ["HelloWorld::HelloWorld.Function::FunctionHandler"]', path.join(helloPath, 'Dockerfile'))
446+
await toFile('CMD: ["HelloWorld::HelloWorld.Function::FunctionHandler"]', path.join(nestedPath, 'Dockerfile'))
447447

448448
const helloWorldResource = {
449449
...resource,

0 commit comments

Comments
 (0)