Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit a9f84ba

Browse files
committed
test: use workspace.fs instead of node.fs
1 parent 450fc5a commit a9f84ba

File tree

8 files changed

+113
-102
lines changed

8 files changed

+113
-102
lines changed

src/designer/create-ui.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import * as fs from 'node:fs'
21
import * as path from 'node:path'
32
import { firstValueFrom } from 'rxjs'
3+
import type { FileStat } from 'vscode'
4+
import { FileType, workspace } from 'vscode'
45
import type { URI } from 'vscode-uri'
56
import type { CommandDeps } from '../commands'
67
import { getTargetDocumentUri } from '../commands'
78
import type { ExecError, StdErrError } from '../run'
89
import { run } from '../run'
910
import { getToolCommand$ } from '../tool-utils'
1011
import type { ErrorResult, SuccessResult } from '../types'
11-
import { notNil } from '../utils'
1212

1313
export async function createUi(
1414
{ extensionUri }: CommandDeps,
@@ -52,15 +52,16 @@ type CreateUiResult =
5252
| ErrorResult<'IO'>
5353

5454
async function getDirectoryPath(uri: URI): Promise<GetDirectoryPathResult> {
55-
return new Promise<GetDirectoryPathResult>(resolve => {
56-
fs.lstat(uri.fsPath, (err, stats) => {
57-
if (notNil(err))
58-
resolve({ kind: 'IOError', message: `${JSON.stringify(err)}` })
59-
else if (stats.isDirectory())
60-
resolve({ kind: 'Success', value: uri.fsPath })
61-
else resolve({ kind: 'Success', value: path.dirname(uri.fsPath) })
62-
})
63-
})
55+
let stat: FileStat
56+
try {
57+
stat = await workspace.fs.stat(uri)
58+
} catch (err) {
59+
return { kind: 'IOError', message: `${JSON.stringify(err)}` }
60+
}
61+
62+
if (stat.type === FileType.Directory)
63+
return { kind: 'Success', value: uri.fsPath }
64+
return { kind: 'Success', value: path.dirname(uri.fsPath) }
6465
}
6566

6667
type GetDirectoryPathResult = SuccessResult<string> | ErrorResult<'IO'>

src/rcc/rcc-live-execution.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { X2jOptionsOptional } from 'fast-xml-parser'
22
import { XMLParser } from 'fast-xml-parser'
3-
import * as fs from 'node:fs/promises'
43
import * as path from 'node:path'
54
import {
65
concatMap,
@@ -82,7 +81,7 @@ type RegisterResourcesLiveExecutionArgs = {
8281
async function getResourceFiles({
8382
qrcUri,
8483
}: GetResourceFilesArgs): Promise<GetResourceFilesResult> {
85-
const content = await fs.readFile(qrcUri.fsPath, 'utf8')
84+
const content = (await workspace.fs.readFile(qrcUri)).toString()
8685

8786
const parserOptions: X2jOptionsOptional = {
8887
isArray: (name, _jpath, _isLeafNode, _isAttribute) => name === 'file',

src/test/suite/rcc/compile-resource.test.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as assert from 'node:assert'
2-
import * as fs from 'node:fs'
32
import * as path from 'node:path'
43
import { commands, window, workspace } from 'vscode'
54
import { URI } from 'vscode-uri'
65
import { EXTENSION_NAMESPACE } from '../../../constants'
76
import {
87
E2E_TIMEOUT,
8+
forceDeleteFile,
99
setupE2EEnvironment,
1010
TEST_ASSETS_PATH,
1111
waitFor,
@@ -31,7 +31,7 @@ suite('compile-resource/e2e', () => {
3131
setup(async function () {
3232
this.timeout(E2E_TIMEOUT)
3333

34-
removeGeneratedFile(sampleFilenameNoExt)
34+
await removeGeneratedFile(sampleFilenameNoExt)
3535

3636
const document = await workspace.openTextDocument(
3737
URI.file(
@@ -41,33 +41,34 @@ suite('compile-resource/e2e', () => {
4141
await window.showTextDocument(document)
4242
})
4343

44-
teardown(function () {
44+
teardown(async function () {
4545
this.timeout(E2E_TIMEOUT)
46-
removeGeneratedFile(sampleFilenameNoExt)
46+
await removeGeneratedFile(sampleFilenameNoExt)
4747
})
4848

4949
test('should run command', async () => {
5050
await commands.executeCommand(`${EXTENSION_NAMESPACE}.compileResource`)
5151

52-
return waitFor(() =>
53-
assert.ok(
54-
fs.existsSync(
52+
return waitFor(async () => {
53+
const readResult = await workspace.fs.readFile(
54+
URI.file(
5555
path.resolve(
5656
TEST_ASSETS_PATH,
5757
'qrc',
5858
`rc_${sampleFilenameNoExt}.py`,
5959
),
6060
),
61-
),
62-
)
61+
)
62+
63+
assert.ok(readResult.byteLength > 0)
64+
})
6365
}).timeout(E2E_TIMEOUT)
6466
})
6567
}).timeout(E2E_TIMEOUT)
6668
}).timeout(E2E_TIMEOUT)
6769

68-
function removeGeneratedFile(sampleFilenameNoExt: string) {
69-
return fs.rmSync(
70+
async function removeGeneratedFile(sampleFilenameNoExt: string) {
71+
await forceDeleteFile(
7072
path.resolve(TEST_ASSETS_PATH, 'qrc', `rc_${sampleFilenameNoExt}.py`),
71-
{ force: true, recursive: true },
7273
)
7374
}
Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as assert from 'node:assert'
2-
import * as fs from 'node:fs'
32
import * as path from 'node:path'
3+
import { workspace } from 'vscode'
4+
import { URI } from 'vscode-uri'
45
import {
56
E2E_TIMEOUT,
7+
forceDeleteFile,
68
setupE2EEnvironment,
7-
sleep,
89
TEST_ASSETS_PATH,
910
waitFor,
1011
} from '../test-utils'
@@ -38,31 +39,38 @@ suite('rcc-live-execution/e2e', () => {
3839

3940
setup(async function () {
4041
this.timeout(E2E_TIMEOUT)
41-
originalFullText = fs.readFileSync(qrcFilePath, { encoding: 'utf-8' })
42+
originalFullText = (
43+
await workspace.fs.readFile(URI.file(qrcFilePath))
44+
).toString()
4245
})
4346

4447
teardown(function () {
4548
this.timeout(E2E_TIMEOUT)
46-
fs.writeFileSync(qrcFilePath, originalFullText, { encoding: 'utf-8' })
49+
workspace.fs.writeFile(
50+
URI.file(qrcFilePath),
51+
Buffer.from(originalFullText),
52+
)
4753
})
4854

4955
test('should recompile', async () => {
50-
fs.writeFileSync(
51-
qrcFilePath,
52-
originalFullText.replace(/<file>rc0.txt<\/file>/gi, ''),
56+
workspace.fs.writeFile(
57+
URI.file(qrcFilePath),
58+
Buffer.from(originalFullText.replace(/<file>rc0.txt<\/file>/gi, '')),
5359
)
5460

55-
await waitFor(() =>
56-
assert.ok(
57-
fs.existsSync(
61+
await waitFor(async () => {
62+
const readResult = await workspace.fs.readFile(
63+
URI.file(
5864
path.resolve(
5965
TEST_ASSETS_PATH,
6066
'qrc',
6167
`rc_${sampleQrcFilenameNoExt}.py`,
6268
),
6369
),
64-
),
65-
)
70+
)
71+
72+
assert.ok(readResult.byteLength > 0)
73+
})
6674
}).timeout(E2E_TIMEOUT)
6775
})
6876

@@ -77,50 +85,43 @@ suite('rcc-live-execution/e2e', () => {
7785

7886
setup(async function () {
7987
this.timeout(E2E_TIMEOUT)
80-
originalFullText = fs.readFileSync(resourceFilePath, {
81-
encoding: 'utf-8',
82-
})
88+
originalFullText = (
89+
await workspace.fs.readFile(URI.file(resourceFilePath))
90+
).toString()
8391
})
8492

8593
teardown(function () {
8694
this.timeout(E2E_TIMEOUT)
87-
fs.writeFileSync(resourceFilePath, originalFullText, {
88-
encoding: 'utf-8',
89-
})
95+
workspace.fs.writeFile(
96+
URI.file(resourceFilePath),
97+
Buffer.from(originalFullText),
98+
)
9099
})
91100

92101
test('should recompile', async () => {
93-
fs.writeFileSync(
94-
resourceFilePath,
95-
originalFullText.replace(/hello/gi, 'world'),
102+
workspace.fs.writeFile(
103+
URI.file(resourceFilePath),
104+
Buffer.from(originalFullText.replace(/hello/gi, 'world')),
96105
)
97106

98-
await waitFor(() =>
99-
assert.ok(
100-
fs.existsSync(
107+
await waitFor(async () => {
108+
const readResult = await workspace.fs.readFile(
109+
URI.file(
101110
path.resolve(
102111
TEST_ASSETS_PATH,
103112
'qrc',
104113
`rc_${sampleQrcFilenameNoExt}.py`,
105114
),
106115
),
107-
),
108-
)
116+
)
117+
assert.ok(readResult.byteLength > 0)
118+
})
109119
})
110120
})
111121
}).timeout(E2E_TIMEOUT)
112122

113123
async function removeGeneratedFile(sampleFilenameNoExt: string) {
114-
return waitFor(async () => {
115-
await sleep() // Wait for the file to be created asynchronously by the extension
116-
fs.rmSync(
117-
path.resolve(TEST_ASSETS_PATH, 'qrc', `rc_${sampleFilenameNoExt}.py`),
118-
{ force: true, recursive: true },
119-
)
120-
assert.ok(
121-
!fs.existsSync(
122-
path.resolve(TEST_ASSETS_PATH, 'qrc', `rc_${sampleFilenameNoExt}.py`),
123-
),
124-
)
125-
})
124+
await forceDeleteFile(
125+
path.resolve(TEST_ASSETS_PATH, 'qrc', `rc_${sampleFilenameNoExt}.py`),
126+
)
126127
}

src/test/suite/test-utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as assert from 'node:assert'
22
import * as path from 'node:path'
3-
import { extensions } from 'vscode'
3+
import { extensions, workspace } from 'vscode'
4+
import { URI } from 'vscode-uri'
45
import { notNil } from '../../utils'
56

67
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -65,3 +66,11 @@ export async function waitFor<T>(
6566
`Timeout during waitFor: ${options?.timeout ?? defaultOptions.timeout}ms`,
6667
)
6768
}
69+
70+
export async function forceDeleteFile(filename: string) {
71+
try {
72+
await workspace.fs.delete(URI.file(filename))
73+
} catch {
74+
// ignore error
75+
}
76+
}

src/test/suite/uic/compile-ui.test.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as assert from 'node:assert'
2-
import * as fs from 'node:fs'
32
import * as path from 'node:path'
43
import { commands, window, workspace } from 'vscode'
54
import { URI } from 'vscode-uri'
65
import { EXTENSION_NAMESPACE } from '../../../constants'
76
import {
87
E2E_TIMEOUT,
8+
forceDeleteFile,
99
setupE2EEnvironment,
1010
TEST_ASSETS_PATH,
1111
waitFor,
@@ -31,7 +31,7 @@ suite('compile-ui/e2e', () => {
3131
setup(async function () {
3232
this.timeout(E2E_TIMEOUT)
3333

34-
removeGeneratedFile(sampleFilenameNoExt)
34+
await removeGeneratedFile(sampleFilenameNoExt)
3535

3636
const document = await workspace.openTextDocument(
3737
URI.file(
@@ -41,33 +41,34 @@ suite('compile-ui/e2e', () => {
4141
await window.showTextDocument(document)
4242
})
4343

44-
teardown(function () {
44+
teardown(async function () {
4545
this.timeout(E2E_TIMEOUT)
46-
removeGeneratedFile(sampleFilenameNoExt)
46+
await removeGeneratedFile(sampleFilenameNoExt)
4747
})
4848

4949
test('should run command', async () => {
5050
await commands.executeCommand(`${EXTENSION_NAMESPACE}.compileUi`)
5151

52-
return waitFor(() =>
53-
assert.ok(
54-
fs.existsSync(
52+
return waitFor(async () => {
53+
const readResult = await workspace.fs.readFile(
54+
URI.file(
5555
path.resolve(
5656
TEST_ASSETS_PATH,
5757
'ui',
5858
`ui_${sampleFilenameNoExt}.py`,
5959
),
6060
),
61-
),
62-
)
61+
)
62+
63+
assert.ok(readResult.byteLength > 0)
64+
})
6365
}).timeout(E2E_TIMEOUT)
6466
})
6567
}).timeout(E2E_TIMEOUT)
6668
}).timeout(E2E_TIMEOUT)
6769

68-
function removeGeneratedFile(sampleFilenameNoExt: string) {
69-
return fs.rmSync(
70+
async function removeGeneratedFile(sampleFilenameNoExt: string) {
71+
return forceDeleteFile(
7072
path.resolve(TEST_ASSETS_PATH, 'ui', `ui_${sampleFilenameNoExt}.py`),
71-
{ force: true, recursive: true },
7273
)
7374
}

0 commit comments

Comments
 (0)