Skip to content

Commit 1bfd53b

Browse files
authored
refactor(fs): prefer our fs and fs to fs-extra (pt2: test files) (aws#5712)
## Problem Follow up from aws#5689, focusing on the test files. ## Solution Mostly direct translation, one implementation change from `writeJson` to `Json.stringify` then `writeFile` in `packages/core/src/test/awsService/cdk/detectCdkProjects.test.ts` line 120 --- <!--- REMINDER: Ensure that your PR meets the guidelines in CONTRIBUTING.md --> License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent af68124 commit 1bfd53b

File tree

50 files changed

+256
-244
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+256
-244
lines changed

docs/vscode_behaviors.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ for each extension.
1212
- The Extension Host process has at most 5 seconds to shut down, after which it will exit. [1]
1313
- The vscode API will be unreliable at deactivation time. So certain VSC APIs like the filesystem may not work. [1]
1414
- The VSC Filesystem API has been confirmed to not work
15-
- In `Run & Debug` mode, closing the Debug IDE instance behaves differently depending on how it is closed
16-
- The regular close button in the Debug IDE instance results in a graceful shutdown
17-
- The red square in the root IDE instance to stop the debugging session results on a non-graceful shutdown, meaning `deactivate()` is not run.
18-
- `Reload Window` triggers `deactivate()`
15+
- In `Run & Debug` mode, closing the Debug IDE instance behaves differently depending on how it is closed
16+
- The regular close button in the Debug IDE instance results in a graceful shutdown
17+
- The red square in the root IDE instance to stop the debugging session results on a non-graceful shutdown, meaning `deactivate()` is not run.
18+
- `Reload Window` triggers `deactivate()`
1919

2020
Sources:
2121

@@ -25,8 +25,7 @@ Sources:
2525
## State (`globalState`, `Memento`)
2626

2727
TODO:
28-
- How it behaves between remote (ssh) and local
29-
- How it is not completely reliable. Reads/writes have no guarantee to work (though we are fine in most cases)
30-
- How it can break as observed with crash monitoring work. At a certain point writes were seemingly succeeding, but did not actually propagate to all IDE instances.
31-
3228

29+
- How it behaves between remote (ssh) and local
30+
- How it is not completely reliable. Reads/writes have no guarantee to work (though we are fine in most cases)
31+
- How it can break as observed with crash monitoring work. At a certain point writes were seemingly succeeding, but did not actually propagate to all IDE instances.

packages/amazonq/test/unit/amazonqGumby/transformationResultsHandler.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
*/
55
import assert from 'assert'
66
import sinon from 'sinon'
7-
import fs from 'fs-extra'
87
import os from 'os'
98
import { DiffModel, AddedChangeNode, ModifiedChangeNode } from 'aws-core-vscode/codewhisperer/node'
109
import path from 'path'
1110
import { getTestResourceFilePath } from './amazonQGumbyUtil'
11+
import { fs } from 'aws-core-vscode/shared'
1212

1313
describe('DiffModel', function () {
1414
afterEach(() => {
@@ -20,7 +20,7 @@ describe('DiffModel', function () {
2020

2121
const workspacePath = 'workspace'
2222

23-
sinon.replace(fs, 'existsSync', (path) => {
23+
sinon.replace(fs, 'exists', async (path) => {
2424
const pathStr = path.toString()
2525
if (pathStr.includes(workspacePath)) {
2626
return false
@@ -42,9 +42,9 @@ describe('DiffModel', function () {
4242

4343
const workspacePath = os.tmpdir()
4444

45-
sinon.replace(fs, 'existsSync', (path) => true)
45+
sinon.replace(fs, 'exists', async (path) => true)
4646

47-
fs.writeFileSync(
47+
await fs.writeFile(
4848
path.join(workspacePath, 'README.md'),
4949
'This guide walks you through using Gradle to build a simple Java project.'
5050
)
@@ -56,6 +56,6 @@ describe('DiffModel', function () {
5656

5757
assert.strictEqual(change instanceof ModifiedChangeNode, true)
5858

59-
fs.rmSync(path.join(workspacePath, 'README.md'))
59+
await fs.delete(path.join(workspacePath, 'README.md'), { recursive: true })
6060
})
6161
})

packages/core/src/test/awsService/cdk/detectCdkProjects.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
import assert from 'assert'
77
import * as path from 'path'
88
import * as vscode from 'vscode'
9-
import * as fs from 'fs-extra'
109
import { detectCdkProjects } from '../../../awsService/cdk/explorer/detectCdkProjects'
1110
import { makeTemporaryToolkitFolder } from '../../../shared/filesystemUtilities'
1211
import { saveCdkJson } from './treeTestUtils'
1312
import { createTestWorkspaceFolder } from '../../testUtil'
1413
import { FakeExtensionContext } from '../../fakeExtensionContext'
15-
import { mkdirp, writeJSON } from 'fs-extra'
1614
import { waitUntil } from '../../../shared/utilities/timeoutUtils'
15+
import { fs } from '../../../shared'
1716

1817
describe('detectCdkProjects', function () {
1918
const workspacePaths: string[] = []
@@ -45,7 +44,7 @@ describe('detectCdkProjects', function () {
4544

4645
afterEach(async function () {
4746
for (const path of workspacePaths) {
48-
await fs.remove(path)
47+
await fs.delete(path, { recursive: true })
4948
}
5049

5150
workspacePaths.length = 0
@@ -88,15 +87,15 @@ describe('detectCdkProjects', function () {
8887

8988
it('detects deep projects', async function () {
9089
const cdkJsonUri = vscode.Uri.joinPath(workspaceFolders[0].uri, 'directory1', 'directory2', 'cdk.json')
91-
await mkdirp(path.dirname(cdkJsonUri.fsPath))
90+
await fs.mkdir(path.dirname(cdkJsonUri.fsPath))
9291
await saveCdkJson(cdkJsonUri.fsPath)
9392
const actual = await detectCdkProjects_wait(workspaceFolders)
9493
assert.strictEqual(actual[0]?.cdkJsonUri.fsPath, cdkJsonUri.fsPath)
9594
})
9695

9796
it('ignores projects in `node_modules`', async function () {
9897
const cdkJsonPath = path.join(workspaceFolders[0].uri.fsPath, 'node_modules', 'lib', 'cdk.json')
99-
await mkdirp(path.dirname(cdkJsonPath))
98+
await fs.mkdir(path.dirname(cdkJsonPath))
10099
await saveCdkJson(cdkJsonPath)
101100
const actual = await detectCdkProjects_wait(workspaceFolders)
102101
assert.strictEqual(actual.length, 0)
@@ -118,7 +117,8 @@ describe('detectCdkProjects', function () {
118117

119118
it('takes into account `output` from cdk.json to build tree.json path', async function () {
120119
const cdkJsonUri = vscode.Uri.joinPath(workspaceFolders[0].uri, 'cdk.json')
121-
await writeJSON(cdkJsonUri.fsPath, { app: 'npx ts-node bin/demo-nov7.ts', output: 'build/cdk.out' })
120+
const cdkJsonStr = JSON.stringify({ app: 'npx ts-node bin/demo-nov7.ts', output: 'build/cdk.out' })
121+
await fs.writeFile(cdkJsonUri.fsPath, cdkJsonStr)
122122
const actual = await detectCdkProjects_wait(workspaceFolders)
123123

124124
assert.ok(actual)

packages/core/src/test/awsService/cdk/treeTestUtils.ts

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

6-
import { writeFile } from 'fs-extra'
76
import { ConstructTree, ConstructTreeEntity } from '../../../awsService/cdk/explorer/tree/types'
7+
import { fs } from '../../../shared'
88

99
export async function saveCdkJson(cdkJsonPath: string) {
1010
const cdkJsonContent = '{ "app": "npx ts-node bin/demo-nov7.ts"}'
1111

12-
await writeFile(cdkJsonPath, cdkJsonContent, 'utf8')
12+
await fs.writeFile(cdkJsonPath, cdkJsonContent, 'utf8')
1313
}
1414

1515
export function generateConstructTreeEntity(label: string, treePath: string, children?: boolean): ConstructTreeEntity {

packages/core/src/test/awsService/cloudWatchLogs/commands/saveCurrentLogDataContent.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import assert from 'assert'
77
import * as path from 'path'
88
import * as vscode from 'vscode'
9-
import * as fs from 'fs-extra'
109

1110
import { createURIFromArgs } from '../../../../awsService/cloudWatchLogs/cloudWatchLogsUtils'
1211
import { saveCurrentLogDataContent } from '../../../../awsService/cloudWatchLogs/commands/saveCurrentLogDataContent'
@@ -19,6 +18,7 @@ import {
1918
LogDataRegistry,
2019
} from '../../../../awsService/cloudWatchLogs/registry/logDataRegistry'
2120
import { assertTextEditorContains } from '../../../testUtil'
21+
import { fs } from '../../../../shared'
2222

2323
async function testFilterLogEvents(
2424
logGroupInfo: CloudWatchLogsGroupInfo,
@@ -46,7 +46,7 @@ describe('saveCurrentLogDataContent', async function () {
4646
})
4747

4848
afterEach(async function () {
49-
await fs.remove(tempDir)
49+
await fs.delete(tempDir, { recursive: true })
5050
})
5151

5252
it('saves log content to a file', async function () {

packages/core/src/test/awsService/ec2/sshKeyPair.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('SshKeyUtility', async function () {
3535
})
3636

3737
after(async function () {
38-
await fs.delete(temporaryDirectory, { recursive: true, force: true })
38+
await fs.delete(temporaryDirectory, { recursive: true })
3939
clock.uninstall()
4040
sinon.restore()
4141
})

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import assert, { fail } from 'assert'
77
import * as vscode from 'vscode'
8-
import * as fs from 'fs-extra'
98
import * as sinon from 'sinon'
109
import { makeTemporaryToolkitFolder } from '../../../shared/filesystemUtilities'
1110
import { transformByQState, TransformByQStoppedError } from '../../../codewhisperer/models/model'
@@ -37,6 +36,7 @@ import {
3736
} from '../../../codewhisperer/service/transformByQ/transformProjectValidationHandler'
3837
import { TransformationCandidateProject, ZipManifest } from '../../../codewhisperer/models/model'
3938
import globals from '../../../shared/extensionGlobals'
39+
import { fs } from '../../../shared'
4040

4141
describe('transformByQ', function () {
4242
let tempDir: string
@@ -48,7 +48,7 @@ describe('transformByQ', function () {
4848

4949
afterEach(async function () {
5050
sinon.restore()
51-
await fs.remove(tempDir)
51+
await fs.delete(tempDir, { recursive: true })
5252
})
5353

5454
it('WHEN converting short duration in milliseconds THEN converts correctly', async function () {
@@ -254,13 +254,13 @@ describe('transformByQ', function () {
254254
'resolver-status.properties',
255255
]
256256

257-
m2Folders.forEach((folder) => {
257+
for (const folder of m2Folders) {
258258
const folderPath = path.join(tempDir, folder)
259-
fs.mkdirSync(folderPath, { recursive: true })
260-
filesToAdd.forEach((file) => {
261-
fs.writeFileSync(path.join(folderPath, file), 'sample content for the test file')
262-
})
263-
})
259+
await fs.mkdir(folderPath)
260+
for (const file of filesToAdd) {
261+
await fs.writeFile(path.join(folderPath, file), 'sample content for the test file')
262+
}
263+
}
264264

265265
const tempFileName = `testfile-${globals.clock.Date.now()}.zip`
266266
transformByQState.setProjectPath(tempDir)

packages/core/src/test/credentials/sharedCredentials.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
import assert from 'assert'
77
import * as path from 'path'
8-
import * as fs from 'fs-extra'
98
import { EnvironmentVariables } from '../../shared/environmentVariables'
109
import { makeTemporaryToolkitFolder } from '../../shared/filesystemUtilities'
1110
import { getCredentialsFilename, getConfigFilename } from '../../auth/credentials/sharedCredentialsFile'
11+
import { fs } from '../../shared'
1212

1313
describe('sharedCredentials', function () {
1414
let tempFolder: string
@@ -26,7 +26,7 @@ describe('sharedCredentials', function () {
2626
})
2727

2828
after(async function () {
29-
await fs.remove(tempFolder)
29+
await fs.delete(tempFolder, { recursive: true })
3030
})
3131

3232
describe('getCredentialsFilename', function () {

packages/core/src/test/credentials/sso/cache.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import assert from 'assert'
77
import * as path from 'path'
8-
import * as fs from 'fs-extra'
98
import { makeTemporaryToolkitFolder, tryRemoveFolder } from '../../../shared/filesystemUtilities'
109
import { getRegistrationCache, getTokenCache } from '../../../auth/sso/cache'
10+
import { fs } from '../../../shared'
1111

1212
describe('SSO Cache', function () {
1313
const region = 'dummyRegion'
@@ -47,7 +47,7 @@ describe('SSO Cache', function () {
4747
await cache.save({ startUrl, region }, validRegistration)
4848

4949
const cachedPath = path.join(testDir, `aws-toolkit-vscode-client-id-${region}.json`)
50-
const contents = await fs.readFile(cachedPath, 'utf-8')
50+
const contents = await fs.readFileAsString(cachedPath)
5151

5252
assert.deepStrictEqual(JSON.parse(contents), {
5353
...validRegistration,
@@ -70,7 +70,7 @@ describe('SSO Cache', function () {
7070

7171
// SHA-1 hash of the encoded start URL `https://123456.awsapps.com/start`
7272
const cachedPath = path.join(testDir, 'c1ac99f782ad92755c6de8647b510ec247330ad1.json')
73-
const contents = await fs.readFile(cachedPath, 'utf-8')
73+
const contents = await fs.readFileAsString(cachedPath)
7474

7575
assert.deepStrictEqual(JSON.parse(contents), {
7676
region,

packages/core/src/test/dynamicResources/awsResourceManager.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import { CloudControlClient, DefaultCloudControlClient } from '../../shared/clie
1616
import { CloudFormationClient, DefaultCloudFormationClient } from '../../shared/clients/cloudFormationClient'
1717
import { makeTemporaryToolkitFolder, readFileAsString } from '../../shared/filesystemUtilities'
1818
import { FakeExtensionContext } from '../fakeExtensionContext'
19-
import { remove } from 'fs-extra'
2019
import { existsSync } from 'fs'
2120
import { ResourceTypeMetadata } from '../../dynamicResources/model/resources'
2221
import globals from '../../shared/extensionGlobals'
2322
import { Stub, stub } from '../utilities/stubber'
2423
import { CloudControl, CloudFormation } from 'aws-sdk'
24+
import { fs } from '../../shared'
2525

2626
describe('ResourceManager', function () {
2727
let sandbox: sinon.SinonSandbox
@@ -71,7 +71,7 @@ describe('ResourceManager', function () {
7171
registerMappingSpy.restore()
7272
sandbox.restore()
7373
await resourceManager.dispose()
74-
await remove(tempFolder)
74+
await fs.delete(tempFolder, { recursive: true })
7575
})
7676

7777
it('opens resources in preview mode', async function () {

0 commit comments

Comments
 (0)