diff options
author | nasa <htilcs1115@gmail.com> | 2023-06-08 23:47:12 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-08 08:47:12 -0600 |
commit | caad79ef7807bbd8f53b0338e23d281c5c45519b (patch) | |
tree | c26d874f1bd5f8ac52c129329ca26a50bf13a2fd /cli/tests/unit_node/_fs/_fs_handle_test.ts | |
parent | 262571e63e3086e0a4ea6125b3836c357a21af86 (diff) |
feat(node_compat): Add a write method to the FileHandle class (#19385)
## WHY
ref: https://github.com/denoland/deno/issues/19165
The FileHandle class has many missing methods compared to node.
## WHAT
Add write method
Diffstat (limited to 'cli/tests/unit_node/_fs/_fs_handle_test.ts')
-rw-r--r-- | cli/tests/unit_node/_fs/_fs_handle_test.ts | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/cli/tests/unit_node/_fs/_fs_handle_test.ts b/cli/tests/unit_node/_fs/_fs_handle_test.ts index 2865fc785..8cfbf6490 100644 --- a/cli/tests/unit_node/_fs/_fs_handle_test.ts +++ b/cli/tests/unit_node/_fs/_fs_handle_test.ts @@ -1,21 +1,22 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import * as path from "../../../../test_util/std/path/mod.ts"; +import { Buffer } from "node:buffer"; import * as fs from "node:fs/promises"; import { assert, assertEquals, } from "../../../../test_util/std/testing/asserts.ts"; -import { Buffer } from "node:buffer"; const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); const testData = path.resolve(moduleDir, "testdata", "hello.txt"); +const decoder = new TextDecoder(); Deno.test("readFileSuccess", async function () { const fileHandle = await fs.open(testData); const data = await fileHandle.readFile(); assert(data instanceof Uint8Array); - assertEquals(new TextDecoder().decode(data as Uint8Array), "hello world"); + assertEquals(decoder.decode(data as Uint8Array), "hello world"); await fileHandle.close(); }); @@ -27,7 +28,7 @@ Deno.test("read", async function () { const buf = new Buffer(byteLength); await fileHandle.read(buf, 0, byteLength, 0); - assertEquals(new TextDecoder().decode(buf as Uint8Array), "hello world"); + assertEquals(decoder.decode(buf as Uint8Array), "hello world"); await fileHandle.close(); }); @@ -54,7 +55,37 @@ Deno.test("read specify opt", async function () { res = await fileHandle.read(opt2); assertEquals(res.bytesRead, byteLength); - assertEquals(new TextDecoder().decode(res.buffer as Uint8Array), "hello"); + assertEquals(decoder.decode(res.buffer as Uint8Array), "hello"); + + await fileHandle.close(); +}); + +Deno.test("[node/fs filehandle.write] Write from Buffer", async function () { + const tempFile: string = await Deno.makeTempFile(); + const fileHandle = await fs.open(tempFile, "a+"); + + const buffer = Buffer.from("hello world"); + const res = await fileHandle.write(buffer, 0, 5, 0); + const data = Deno.readFileSync(tempFile); + await Deno.remove(tempFile); await fileHandle.close(); + + assertEquals(res.bytesWritten, 5); + assertEquals(decoder.decode(data), "hello"); +}); + +Deno.test("[node/fs filehandle.write] Write from string", async function () { + const tempFile: string = await Deno.makeTempFile(); + const fileHandle = await fs.open(tempFile, "a+"); + + const str = "hello world"; + const res = await fileHandle.write(str); + + const data = Deno.readFileSync(tempFile); + await Deno.remove(tempFile); + await fileHandle.close(); + + assertEquals(res.bytesWritten, 11); + assertEquals(decoder.decode(data), "hello world"); }); |