diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2020-06-12 20:23:38 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-12 15:23:38 -0400 |
commit | 1fff6f55c3ba98a10018c6d374795e612061e9b6 (patch) | |
tree | 12074b6d44736b11513d857e437f9e30a6bf65a4 /std/node/_fs | |
parent | 26bf56afdaf16634ffbaa23684faf3a44cc10f62 (diff) |
refactor: Don't destructure the Deno namespace (#6268)
Diffstat (limited to 'std/node/_fs')
-rw-r--r-- | std/node/_fs/_fs_appendFile_test.ts | 19 | ||||
-rw-r--r-- | std/node/_fs/_fs_chmod_test.ts | 5 | ||||
-rw-r--r-- | std/node/_fs/_fs_chown_test.ts | 5 | ||||
-rw-r--r-- | std/node/_fs/_fs_close_test.ts | 11 | ||||
-rw-r--r-- | std/node/_fs/_fs_copy_test.ts | 8 | ||||
-rw-r--r-- | std/node/_fs/_fs_dir_test.ts | 17 | ||||
-rw-r--r-- | std/node/_fs/_fs_dirent_test.ts | 11 | ||||
-rw-r--r-- | std/node/_fs/_fs_exists_test.ts | 7 | ||||
-rw-r--r-- | std/node/_fs/_fs_link_test.ts | 8 | ||||
-rw-r--r-- | std/node/_fs/_fs_mkdir_test.ts | 7 | ||||
-rw-r--r-- | std/node/_fs/_fs_readFile.ts | 8 | ||||
-rw-r--r-- | std/node/_fs/_fs_readFile_test.ts | 13 | ||||
-rw-r--r-- | std/node/_fs/_fs_readlink.ts | 6 | ||||
-rw-r--r-- | std/node/_fs/_fs_readlink_test.ts | 9 | ||||
-rw-r--r-- | std/node/_fs/_fs_writeFile_test.ts | 271 | ||||
-rw-r--r-- | std/node/_fs/promises/_fs_readFile_test.ts | 9 | ||||
-rw-r--r-- | std/node/_fs/promises/_fs_writeFile_test.ts | 146 |
17 files changed, 283 insertions, 277 deletions
diff --git a/std/node/_fs/_fs_appendFile_test.ts b/std/node/_fs/_fs_appendFile_test.ts index 402ac1c10..1286ff900 100644 --- a/std/node/_fs/_fs_appendFile_test.ts +++ b/std/node/_fs/_fs_appendFile_test.ts @@ -1,12 +1,11 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; import { assertEquals, assertThrows, fail } from "../../testing/asserts.ts"; import { appendFile, appendFileSync } from "./_fs_appendFile.ts"; import { fromFileUrl } from "../path.ts"; const decoder = new TextDecoder("utf-8"); -test({ +Deno.test({ name: "No callback Fn results in Error", fn() { assertThrows( @@ -19,7 +18,7 @@ test({ }, }); -test({ +Deno.test({ name: "Unsupported encoding results in error()", fn() { assertThrows( @@ -57,7 +56,7 @@ test({ }, }); -test({ +Deno.test({ name: "Async: Data is written to passed in rid", async fn() { const tempFile: string = await Deno.makeTempFile(); @@ -86,7 +85,7 @@ test({ }, }); -test({ +Deno.test({ name: "Async: Data is written to passed in file path", async fn() { const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources(); @@ -110,7 +109,7 @@ test({ }, }); -test({ +Deno.test({ name: "Async: Data is written to passed in URL", async fn() { const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources(); @@ -135,7 +134,7 @@ test({ }, }); -test({ +Deno.test({ name: "Async: Callback is made with error if attempting to append data to an existing file with 'ax' flag", async fn() { @@ -159,7 +158,7 @@ test({ }, }); -test({ +Deno.test({ name: "Sync: Data is written to passed in rid", fn() { const tempFile: string = Deno.makeTempFileSync(); @@ -176,7 +175,7 @@ test({ }, }); -test({ +Deno.test({ name: "Sync: Data is written to passed in file path", fn() { const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources(); @@ -188,7 +187,7 @@ test({ }, }); -test({ +Deno.test({ name: "Sync: error thrown if attempting to append data to an existing file with 'ax' flag", fn() { diff --git a/std/node/_fs/_fs_chmod_test.ts b/std/node/_fs/_fs_chmod_test.ts index e43f09788..de4981a8c 100644 --- a/std/node/_fs/_fs_chmod_test.ts +++ b/std/node/_fs/_fs_chmod_test.ts @@ -1,9 +1,8 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; import { fail, assert } from "../../testing/asserts.ts"; import { chmod, chmodSync } from "./_fs_chmod.ts"; -test({ +Deno.test({ name: "ASYNC: Permissions are changed (non-Windows)", ignore: Deno.build.os === "windows", async fn() { @@ -29,7 +28,7 @@ test({ }, }); -test({ +Deno.test({ name: "SYNC: Permissions are changed (non-Windows)", ignore: Deno.build.os === "windows", fn() { diff --git a/std/node/_fs/_fs_chown_test.ts b/std/node/_fs/_fs_chown_test.ts index 1c1393ac4..de7dd992f 100644 --- a/std/node/_fs/_fs_chown_test.ts +++ b/std/node/_fs/_fs_chown_test.ts @@ -1,5 +1,4 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; import { fail, assertEquals } from "../../testing/asserts.ts"; import { chown, chownSync } from "./_fs_chown.ts"; @@ -7,7 +6,7 @@ import { chown, chownSync } from "./_fs_chown.ts"; // id again const ignore = Deno.build.os == "windows"; -test({ +Deno.test({ ignore, name: "ASYNC: setting existing uid/gid works as expected (non-Windows)", async fn() { @@ -35,7 +34,7 @@ test({ }, }); -test({ +Deno.test({ ignore, name: "SYNC: setting existing uid/gid works as expected (non-Windows)", fn() { diff --git a/std/node/_fs/_fs_close_test.ts b/std/node/_fs/_fs_close_test.ts index 1ea324cb4..feaf92ab8 100644 --- a/std/node/_fs/_fs_close_test.ts +++ b/std/node/_fs/_fs_close_test.ts @@ -1,9 +1,8 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; import { fail, assert, assertThrows } from "../../testing/asserts.ts"; import { close, closeSync } from "./_fs_close.ts"; -test({ +Deno.test({ name: "ASYNC: File is closed", async fn() { const tempFile: string = await Deno.makeTempFile(); @@ -28,7 +27,7 @@ test({ }, }); -test({ +Deno.test({ name: "ASYNC: Invalid fd", async fn() { await new Promise((resolve, reject) => { @@ -40,7 +39,7 @@ test({ }, }); -test({ +Deno.test({ name: "close callback should be asynchronous", async fn() { const tempFile: string = Deno.makeTempFileSync(); @@ -60,7 +59,7 @@ test({ }, }); -test({ +Deno.test({ name: "SYNC: File is closed", fn() { const tempFile: string = Deno.makeTempFileSync(); @@ -73,7 +72,7 @@ test({ }, }); -test({ +Deno.test({ name: "SYNC: Invalid fd", fn() { assertThrows(() => closeSync(-1)); diff --git a/std/node/_fs/_fs_copy_test.ts b/std/node/_fs/_fs_copy_test.ts index f7ce0e279..891e80784 100644 --- a/std/node/_fs/_fs_copy_test.ts +++ b/std/node/_fs/_fs_copy_test.ts @@ -1,13 +1,11 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { assert } from "../../testing/asserts.ts"; import { copyFile, copyFileSync } from "./_fs_copy.ts"; import { existsSync } from "./_fs_exists.ts"; -import { assert } from "../../testing/asserts.ts"; -const { test } = Deno; - const destFile = "./destination.txt"; -test({ +Deno.test({ name: "[std/node/fs] copy file", fn: async () => { const sourceFile = Deno.makeTempFileSync(); @@ -21,7 +19,7 @@ test({ }, }); -test({ +Deno.test({ name: "[std/node/fs] copy file sync", fn: () => { const sourceFile = Deno.makeTempFileSync(); diff --git a/std/node/_fs/_fs_dir_test.ts b/std/node/_fs/_fs_dir_test.ts index 5b3336201..e89912772 100644 --- a/std/node/_fs/_fs_dir_test.ts +++ b/std/node/_fs/_fs_dir_test.ts @@ -1,9 +1,8 @@ -const { test } = Deno; import { assert, assertEquals, fail } from "../../testing/asserts.ts"; import Dir from "./_fs_dir.ts"; import Dirent from "./_fs_dirent.ts"; -test({ +Deno.test({ name: "Closing current directory with callback is successful", fn() { let calledBack = false; @@ -16,21 +15,21 @@ test({ }, }); -test({ +Deno.test({ name: "Closing current directory without callback returns void Promise", async fn() { await new Dir(".").close(); }, }); -test({ +Deno.test({ name: "Closing current directory synchronously works", fn() { new Dir(".").closeSync(); }, }); -test({ +Deno.test({ name: "Path is correctly returned", fn() { assertEquals(new Dir("std/node").path, "std/node"); @@ -40,7 +39,7 @@ test({ }, }); -test({ +Deno.test({ name: "read returns null for empty directory", async fn() { const testDir: string = Deno.makeTempDirSync(); @@ -67,7 +66,7 @@ test({ }, }); -test({ +Deno.test({ name: "Async read returns one file at a time", async fn() { const testDir: string = Deno.makeTempDirSync(); @@ -108,7 +107,7 @@ test({ }, }); -test({ +Deno.test({ name: "Sync read returns one file at a time", fn() { const testDir: string = Deno.makeTempDirSync(); @@ -139,7 +138,7 @@ test({ }, }); -test({ +Deno.test({ name: "Async iteration over existing directory", async fn() { const testDir: string = Deno.makeTempDirSync(); diff --git a/std/node/_fs/_fs_dirent_test.ts b/std/node/_fs/_fs_dirent_test.ts index 43becedd1..8c4b98214 100644 --- a/std/node/_fs/_fs_dirent_test.ts +++ b/std/node/_fs/_fs_dirent_test.ts @@ -1,4 +1,3 @@ -const { test } = Deno; import { assert, assertEquals, assertThrows } from "../../testing/asserts.ts"; import Dirent from "./_fs_dirent.ts"; @@ -9,7 +8,7 @@ class DirEntryMock implements Deno.DirEntry { isSymlink = false; } -test({ +Deno.test({ name: "Directories are correctly identified", fn() { const entry: DirEntryMock = new DirEntryMock(); @@ -22,7 +21,7 @@ test({ }, }); -test({ +Deno.test({ name: "Files are correctly identified", fn() { const entry: DirEntryMock = new DirEntryMock(); @@ -35,7 +34,7 @@ test({ }, }); -test({ +Deno.test({ name: "Symlinks are correctly identified", fn() { const entry: DirEntryMock = new DirEntryMock(); @@ -48,7 +47,7 @@ test({ }, }); -test({ +Deno.test({ name: "File name is correct", fn() { const entry: DirEntryMock = new DirEntryMock(); @@ -57,7 +56,7 @@ test({ }, }); -test({ +Deno.test({ name: "Socket and FIFO pipes aren't yet available", fn() { const entry: DirEntryMock = new DirEntryMock(); diff --git a/std/node/_fs/_fs_exists_test.ts b/std/node/_fs/_fs_exists_test.ts index b4885c87f..d7d2f7f29 100644 --- a/std/node/_fs/_fs_exists_test.ts +++ b/std/node/_fs/_fs_exists_test.ts @@ -1,11 +1,8 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - import { assertEquals } from "../../testing/asserts.ts"; import { exists, existsSync } from "./_fs_exists.ts"; -const { test } = Deno; - -test("existsFile", async function () { +Deno.test("existsFile", async function () { const availableFile = await new Promise((resolve) => { const tmpFilePath = Deno.makeTempFileSync(); exists(tmpFilePath, (exists: boolean) => { @@ -20,7 +17,7 @@ test("existsFile", async function () { assertEquals(notAvailableFile, false); }); -test("existsSyncFile", function () { +Deno.test("existsSyncFile", function () { const tmpFilePath = Deno.makeTempFileSync(); assertEquals(existsSync(tmpFilePath), true); Deno.removeSync(tmpFilePath); diff --git a/std/node/_fs/_fs_link_test.ts b/std/node/_fs/_fs_link_test.ts index e59984c8c..0251e55fb 100644 --- a/std/node/_fs/_fs_link_test.ts +++ b/std/node/_fs/_fs_link_test.ts @@ -1,11 +1,11 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; import { fail, assertEquals } from "../../testing/asserts.ts"; import { link, linkSync } from "./_fs_link.ts"; import { assert } from "https://deno.land/std@v0.50.0/testing/asserts.ts"; + const isWindows = Deno.build.os === "windows"; -test({ +Deno.test({ ignore: isWindows, name: "ASYNC: hard linking files works as expected", async fn() { @@ -30,7 +30,7 @@ test({ }, }); -test({ +Deno.test({ ignore: isWindows, name: "ASYNC: hard linking files passes error to callback", async fn() { @@ -52,7 +52,7 @@ test({ }, }); -test({ +Deno.test({ ignore: isWindows, name: "SYNC: hard linking files works as expected", fn() { diff --git a/std/node/_fs/_fs_mkdir_test.ts b/std/node/_fs/_fs_mkdir_test.ts index 7e9d4859f..8909d85de 100644 --- a/std/node/_fs/_fs_mkdir_test.ts +++ b/std/node/_fs/_fs_mkdir_test.ts @@ -1,14 +1,11 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - import { assert } from "../../testing/asserts.ts"; import { mkdir, mkdirSync } from "./_fs_mkdir.ts"; import { existsSync } from "./_fs_exists.ts"; -const { test } = Deno; - const tmpDir = "./tmpdir"; -test({ +Deno.test({ name: "[node/fs] mkdir", fn: async () => { const result = await new Promise((resolve) => { @@ -22,7 +19,7 @@ test({ }, }); -test({ +Deno.test({ name: "[node/fs] mkdirSync", fn: () => { mkdirSync(tmpDir); diff --git a/std/node/_fs/_fs_readFile.ts b/std/node/_fs/_fs_readFile.ts index 448045fd2..d4093ff7f 100644 --- a/std/node/_fs/_fs_readFile.ts +++ b/std/node/_fs/_fs_readFile.ts @@ -1,13 +1,9 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - import { intoCallbackAPIWithIntercept, MaybeEmpty } from "../_utils.ts"; - import { getEncoding, FileOptions } from "./_fs_common.ts"; import { Buffer } from "../buffer.ts"; import { fromFileUrl } from "../path.ts"; -const { readFile: denoReadFile, readFileSync: denoReadFileSync } = Deno; - type ReadFileCallback = ( err: MaybeEmpty<Error>, data: MaybeEmpty<string | Buffer> @@ -38,7 +34,7 @@ export function readFile( const encoding = getEncoding(optOrCallback); intoCallbackAPIWithIntercept<Uint8Array, string | Buffer>( - denoReadFile, + Deno.readFile, (data: Uint8Array): string | Buffer => maybeDecode(data, encoding), cb, path @@ -50,5 +46,5 @@ export function readFileSync( opt?: FileOptions | string ): string | Buffer { path = path instanceof URL ? fromFileUrl(path) : path; - return maybeDecode(denoReadFileSync(path), getEncoding(opt)); + return maybeDecode(Deno.readFileSync(path), getEncoding(opt)); } diff --git a/std/node/_fs/_fs_readFile_test.ts b/std/node/_fs/_fs_readFile_test.ts index 1a850c91a..02e4c3745 100644 --- a/std/node/_fs/_fs_readFile_test.ts +++ b/std/node/_fs/_fs_readFile_test.ts @@ -1,4 +1,3 @@ -const { test } = Deno; import { readFile, readFileSync } from "./_fs_readFile.ts"; import * as path from "../../path/mod.ts"; import { assertEquals, assert } from "../../testing/asserts.ts"; @@ -7,7 +6,7 @@ const testData = path.resolve( path.join("node", "_fs", "testdata", "hello.txt") ); -test("readFileSuccess", async function () { +Deno.test("readFileSuccess", async function () { const data = await new Promise((res, rej) => { readFile(testData, (err, data) => { if (err) { @@ -21,7 +20,7 @@ test("readFileSuccess", async function () { assertEquals(new TextDecoder().decode(data as Uint8Array), "hello world"); }); -test("readFileEncodeUtf8Success", async function () { +Deno.test("readFileEncodeUtf8Success", async function () { const data = await new Promise((res, rej) => { readFile(testData, { encoding: "utf8" }, (err, data) => { if (err) { @@ -35,7 +34,7 @@ test("readFileEncodeUtf8Success", async function () { assertEquals(data as string, "hello world"); }); -test("readFileEncodingAsString", async function () { +Deno.test("readFileEncodingAsString", async function () { const data = await new Promise((res, rej) => { readFile(testData, "utf8", (err, data) => { if (err) { @@ -49,19 +48,19 @@ test("readFileEncodingAsString", async function () { assertEquals(data as string, "hello world"); }); -test("readFileSyncSuccess", function () { +Deno.test("readFileSyncSuccess", function () { const data = readFileSync(testData); assert(data instanceof Uint8Array); assertEquals(new TextDecoder().decode(data as Uint8Array), "hello world"); }); -test("readFileEncodeUtf8Success", function () { +Deno.test("readFileEncodeUtf8Success", function () { const data = readFileSync(testData, { encoding: "utf8" }); assertEquals(typeof data, "string"); assertEquals(data as string, "hello world"); }); -test("readFileEncodeAsString", function () { +Deno.test("readFileEncodeAsString", function () { const data = readFileSync(testData, "utf8"); assertEquals(typeof data, "string"); assertEquals(data as string, "hello world"); diff --git a/std/node/_fs/_fs_readlink.ts b/std/node/_fs/_fs_readlink.ts index d461cf390..11ce43f55 100644 --- a/std/node/_fs/_fs_readlink.ts +++ b/std/node/_fs/_fs_readlink.ts @@ -6,8 +6,6 @@ import { } from "../_utils.ts"; import { fromFileUrl } from "../path.ts"; -const { readLink: denoReadlink, readLinkSync: denoReadlinkSync } = Deno; - type ReadlinkCallback = ( err: MaybeEmpty<Error>, linkString: MaybeEmpty<string | Uint8Array> @@ -66,7 +64,7 @@ export function readlink( const encoding = getEncoding(optOrCallback); intoCallbackAPIWithIntercept<string, Uint8Array | string>( - denoReadlink, + Deno.readLink, (data: string): string | Uint8Array => maybeEncode(data, encoding), cb, path @@ -79,5 +77,5 @@ export function readlinkSync( ): string | Uint8Array { path = path instanceof URL ? fromFileUrl(path) : path; - return maybeEncode(denoReadlinkSync(path), getEncoding(opt)); + return maybeEncode(Deno.readLinkSync(path), getEncoding(opt)); } diff --git a/std/node/_fs/_fs_readlink_test.ts b/std/node/_fs/_fs_readlink_test.ts index 77ce60a3f..437873494 100644 --- a/std/node/_fs/_fs_readlink_test.ts +++ b/std/node/_fs/_fs_readlink_test.ts @@ -1,4 +1,3 @@ -const { test } = Deno; import { readlink, readlinkSync } from "./_fs_readlink.ts"; import { assertEquals, assert } from "../../testing/asserts.ts"; import * as path from "../path.ts"; @@ -13,7 +12,7 @@ if (Deno.build.os === "windows") { Deno.symlinkSync(oldname, newname); } -test({ +Deno.test({ name: "readlinkSuccess", async fn() { const data = await new Promise((res, rej) => { @@ -30,7 +29,7 @@ test({ }, }); -test({ +Deno.test({ name: "readlinkEncodeBufferSuccess", async fn() { const data = await new Promise((res, rej) => { @@ -47,7 +46,7 @@ test({ }, }); -test({ +Deno.test({ name: "readlinkSyncSuccess", fn() { const data = readlinkSync(newname); @@ -56,7 +55,7 @@ test({ }, }); -test({ +Deno.test({ name: "readlinkEncodeBufferSuccess", fn() { const data = readlinkSync(newname, { encoding: "buffer" }); diff --git a/std/node/_fs/_fs_writeFile_test.ts b/std/node/_fs/_fs_writeFile_test.ts index 486c55fa1..81913d0b0 100644 --- a/std/node/_fs/_fs_writeFile_test.ts +++ b/std/node/_fs/_fs_writeFile_test.ts @@ -1,6 +1,4 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; - import { assert, assertEquals, @@ -13,7 +11,7 @@ import * as path from "../../path/mod.ts"; const testDataDir = path.resolve(path.join("node", "_fs", "testdata")); const decoder = new TextDecoder("utf-8"); -test("Callback must be a function error", function fn() { +Deno.test("Callback must be a function error", function fn() { assertThrows( () => { writeFile("some/path", "some data", "utf8"); @@ -23,7 +21,7 @@ test("Callback must be a function error", function fn() { ); }); -test("Invalid encoding results in error()", function testEncodingErrors() { +Deno.test("Invalid encoding results in error()", function testEncodingErrors() { assertThrows( () => { writeFile("some/path", "some data", "made-up-encoding", () => {}); @@ -66,82 +64,91 @@ test("Invalid encoding results in error()", function testEncodingErrors() { ); }); -test("Unsupported encoding results in error()", function testUnsupportedEncoding() { - assertThrows( - () => { - writeFile("some/path", "some data", "hex", () => {}); - }, - Error, - `Not implemented: "hex" encoding` - ); - - assertThrows( - () => { - writeFileSync("some/path", "some data", "hex"); - }, - Error, - `Not implemented: "hex" encoding` - ); - - assertThrows( - () => { - writeFile( - "some/path", - "some data", - { +Deno.test( + "Unsupported encoding results in error()", + function testUnsupportedEncoding() { + assertThrows( + () => { + writeFile("some/path", "some data", "hex", () => {}); + }, + Error, + `Not implemented: "hex" encoding` + ); + + assertThrows( + () => { + writeFileSync("some/path", "some data", "hex"); + }, + Error, + `Not implemented: "hex" encoding` + ); + + assertThrows( + () => { + writeFile( + "some/path", + "some data", + { + encoding: "base64", + }, + () => {} + ); + }, + Error, + `Not implemented: "base64" encoding` + ); + + assertThrows( + () => { + writeFileSync("some/path", "some data", { encoding: "base64", - }, - () => {} - ); - }, - Error, - `Not implemented: "base64" encoding` - ); + }); + }, + Error, + `Not implemented: "base64" encoding` + ); + } +); + +Deno.test( + "Data is written to correct rid", + async function testCorrectWriteUsingRid() { + const tempFile: string = await Deno.makeTempFile(); + const file: Deno.File = await Deno.open(tempFile, { + create: true, + write: true, + read: true, + }); - assertThrows( - () => { - writeFileSync("some/path", "some data", { - encoding: "base64", + await new Promise((resolve, reject) => { + writeFile(file.rid, "hello world", (err) => { + if (err) return reject(err); + resolve(); }); - }, - Error, - `Not implemented: "base64" encoding` - ); -}); - -test("Data is written to correct rid", async function testCorrectWriteUsingRid() { - const tempFile: string = await Deno.makeTempFile(); - const file: Deno.File = await Deno.open(tempFile, { - create: true, - write: true, - read: true, - }); - - await new Promise((resolve, reject) => { - writeFile(file.rid, "hello world", (err) => { - if (err) return reject(err); - resolve(); }); - }); - Deno.close(file.rid); - - const data = await Deno.readFile(tempFile); - await Deno.remove(tempFile); - assertEquals(decoder.decode(data), "hello world"); -}); - -test("Data is written to correct file", async function testCorrectWriteUsingPath() { - const res = await new Promise((resolve) => { - writeFile("_fs_writeFile_test_file.txt", "hello world", resolve); - }); + Deno.close(file.rid); + + const data = await Deno.readFile(tempFile); + await Deno.remove(tempFile); + assertEquals(decoder.decode(data), "hello world"); + } +); + +Deno.test( + "Data is written to correct file", + async function testCorrectWriteUsingPath() { + const res = await new Promise((resolve) => { + writeFile("_fs_writeFile_test_file.txt", "hello world", resolve); + }); - const data = await Deno.readFile("_fs_writeFile_test_file.txt"); - await Deno.remove("_fs_writeFile_test_file.txt"); - assertEquals(res, null); - assertEquals(decoder.decode(data), "hello world"); -}); + const data = await Deno.readFile("_fs_writeFile_test_file.txt"); + await Deno.remove("_fs_writeFile_test_file.txt"); + assertEquals(res, null); + assertEquals(decoder.decode(data), "hello world"); + } +); -test("Path can be an URL", async function testCorrectWriteUsingURL() { +Deno.test("Path can be an URL", async function testCorrectWriteUsingURL() { const url = new URL( Deno.build.os === "windows" ? "file:///" + @@ -162,7 +169,7 @@ test("Path can be an URL", async function testCorrectWriteUsingURL() { assertEquals(decoder.decode(data), "hello world"); }); -test("Mode is correctly set", async function testCorrectFileMode() { +Deno.test("Mode is correctly set", async function testCorrectFileMode() { if (Deno.build.os === "windows") return; const filename = "_fs_writeFile_test_file.txt"; @@ -177,57 +184,66 @@ test("Mode is correctly set", async function testCorrectFileMode() { assertEquals(fileInfo.mode & 0o777, 0o777); }); -test("Mode is not set when rid is passed", async function testCorrectFileModeRid() { - if (Deno.build.os === "windows") return; - - const filename: string = await Deno.makeTempFile(); - const file: Deno.File = await Deno.open(filename, { - create: true, - write: true, - read: true, - }); +Deno.test( + "Mode is not set when rid is passed", + async function testCorrectFileModeRid() { + if (Deno.build.os === "windows") return; - await new Promise((resolve, reject) => { - writeFile(file.rid, "hello world", { mode: 0o777 }, (err) => { - if (err) return reject(err); - resolve(); + const filename: string = await Deno.makeTempFile(); + const file: Deno.File = await Deno.open(filename, { + create: true, + write: true, + read: true, }); - }); - Deno.close(file.rid); - const fileInfo = await Deno.stat(filename); - await Deno.remove(filename); - assert(fileInfo.mode); - assertNotEquals(fileInfo.mode & 0o777, 0o777); -}); - -test("Data is written synchronously to correct rid", function testCorrectWriteSyncUsingRid() { - const tempFile: string = Deno.makeTempFileSync(); - const file: Deno.File = Deno.openSync(tempFile, { - create: true, - write: true, - read: true, - }); + await new Promise((resolve, reject) => { + writeFile(file.rid, "hello world", { mode: 0o777 }, (err) => { + if (err) return reject(err); + resolve(); + }); + }); + Deno.close(file.rid); + + const fileInfo = await Deno.stat(filename); + await Deno.remove(filename); + assert(fileInfo.mode); + assertNotEquals(fileInfo.mode & 0o777, 0o777); + } +); + +Deno.test( + "Data is written synchronously to correct rid", + function testCorrectWriteSyncUsingRid() { + const tempFile: string = Deno.makeTempFileSync(); + const file: Deno.File = Deno.openSync(tempFile, { + create: true, + write: true, + read: true, + }); - writeFileSync(file.rid, "hello world"); - Deno.close(file.rid); + writeFileSync(file.rid, "hello world"); + Deno.close(file.rid); - const data = Deno.readFileSync(tempFile); - Deno.removeSync(tempFile); - assertEquals(decoder.decode(data), "hello world"); -}); + const data = Deno.readFileSync(tempFile); + Deno.removeSync(tempFile); + assertEquals(decoder.decode(data), "hello world"); + } +); -test("Data is written synchronously to correct file", function testCorrectWriteSyncUsingPath() { - const file = "_fs_writeFileSync_test_file"; +Deno.test( + "Data is written synchronously to correct file", + function testCorrectWriteSyncUsingPath() { + const file = "_fs_writeFileSync_test_file"; - writeFileSync(file, "hello world"); + writeFileSync(file, "hello world"); - const data = Deno.readFileSync(file); - Deno.removeSync(file); - assertEquals(decoder.decode(data), "hello world"); -}); + const data = Deno.readFileSync(file); + Deno.removeSync(file); + assertEquals(decoder.decode(data), "hello world"); + } +); -test("sync: Path can be an URL", function testCorrectWriteSyncUsingURL() { +Deno.test("sync: Path can be an URL", function testCorrectWriteSyncUsingURL() { const filePath = path.join( testDataDir, "_fs_writeFileSync_test_file_url.txt" @@ -244,14 +260,17 @@ test("sync: Path can be an URL", function testCorrectWriteSyncUsingURL() { assertEquals(decoder.decode(data), "hello world"); }); -test("Mode is correctly set when writing synchronously", function testCorrectFileModeSync() { - if (Deno.build.os === "windows") return; - const filename = "_fs_writeFileSync_test_file.txt"; +Deno.test( + "Mode is correctly set when writing synchronously", + function testCorrectFileModeSync() { + if (Deno.build.os === "windows") return; + const filename = "_fs_writeFileSync_test_file.txt"; - writeFileSync(filename, "hello world", { mode: 0o777 }); + writeFileSync(filename, "hello world", { mode: 0o777 }); - const fileInfo = Deno.statSync(filename); - Deno.removeSync(filename); - assert(fileInfo && fileInfo.mode); - assertEquals(fileInfo.mode & 0o777, 0o777); -}); + const fileInfo = Deno.statSync(filename); + Deno.removeSync(filename); + assert(fileInfo && fileInfo.mode); + assertEquals(fileInfo.mode & 0o777, 0o777); + } +); diff --git a/std/node/_fs/promises/_fs_readFile_test.ts b/std/node/_fs/promises/_fs_readFile_test.ts index ac3c8fdda..c92907fec 100644 --- a/std/node/_fs/promises/_fs_readFile_test.ts +++ b/std/node/_fs/promises/_fs_readFile_test.ts @@ -1,4 +1,3 @@ -const { test } = Deno; import { readFile } from "./_fs_readFile.ts"; import * as path from "../../../path/mod.ts"; import { assertEquals, assert } from "../../../testing/asserts.ts"; @@ -7,28 +6,28 @@ const testData = path.resolve( path.join("node", "_fs", "testdata", "hello.txt") ); -test("readFileSuccess", async function () { +Deno.test("readFileSuccess", async function () { const data = await readFile(testData); assert(data instanceof Uint8Array); assertEquals(new TextDecoder().decode(data as Uint8Array), "hello world"); }); -test("readFileEncodeUtf8Success", async function () { +Deno.test("readFileEncodeUtf8Success", async function () { const data = await readFile(testData, { encoding: "utf8" }); assertEquals(typeof data, "string"); assertEquals(data as string, "hello world"); }); -test("readFileEncodingAsString", async function () { +Deno.test("readFileEncodingAsString", async function () { const data = await readFile(testData, "utf8"); assertEquals(typeof data, "string"); assertEquals(data as string, "hello world"); }); -test("readFileError", async function () { +Deno.test("readFileError", async function () { try { await readFile("invalid-file", "utf8"); } catch (e) { diff --git a/std/node/_fs/promises/_fs_writeFile_test.ts b/std/node/_fs/promises/_fs_writeFile_test.ts index 171dbeb2c..574bbfc35 100644 --- a/std/node/_fs/promises/_fs_writeFile_test.ts +++ b/std/node/_fs/promises/_fs_writeFile_test.ts @@ -1,6 +1,4 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; - import { assert, assertEquals, @@ -11,7 +9,7 @@ import { writeFile } from "./_fs_writeFile.ts"; const decoder = new TextDecoder("utf-8"); -test("Invalid encoding results in error()", function testEncodingErrors() { +Deno.test("Invalid encoding results in error()", function testEncodingErrors() { assertThrowsAsync( async () => { await writeFile("some/path", "some data", "made-up-encoding"); @@ -30,53 +28,62 @@ test("Invalid encoding results in error()", function testEncodingErrors() { ); }); -test("Unsupported encoding results in error()", function testUnsupportedEncoding() { - assertThrowsAsync( - async () => { - await writeFile("some/path", "some data", "hex"); - }, - Error, - `Not implemented: "hex" encoding` - ); - assertThrowsAsync( - async () => { - await writeFile("some/path", "some data", { - encoding: "base64", - }); - }, - Error, - `Not implemented: "base64" encoding` - ); -}); - -test("Data is written to correct rid", async function testCorrectWriteUsingRid() { - const tempFile: string = await Deno.makeTempFile(); - const file: Deno.File = await Deno.open(tempFile, { - create: true, - write: true, - read: true, - }); - - await writeFile(file.rid, "hello world"); - Deno.close(file.rid); - - const data = await Deno.readFile(tempFile); - await Deno.remove(tempFile); - assertEquals(decoder.decode(data), "hello world"); -}); - -test("Data is written to correct file", async function testCorrectWriteUsingPath() { - const openResourcesBeforeWrite: Deno.ResourceMap = Deno.resources(); - - await writeFile("_fs_writeFile_test_file.txt", "hello world"); - - assertEquals(Deno.resources(), openResourcesBeforeWrite); - const data = await Deno.readFile("_fs_writeFile_test_file.txt"); - await Deno.remove("_fs_writeFile_test_file.txt"); - assertEquals(decoder.decode(data), "hello world"); -}); - -test("Mode is correctly set", async function testCorrectFileMode() { +Deno.test( + "Unsupported encoding results in error()", + function testUnsupportedEncoding() { + assertThrowsAsync( + async () => { + await writeFile("some/path", "some data", "hex"); + }, + Error, + `Not implemented: "hex" encoding` + ); + assertThrowsAsync( + async () => { + await writeFile("some/path", "some data", { + encoding: "base64", + }); + }, + Error, + `Not implemented: "base64" encoding` + ); + } +); + +Deno.test( + "Data is written to correct rid", + async function testCorrectWriteUsingRid() { + const tempFile: string = await Deno.makeTempFile(); + const file: Deno.File = await Deno.open(tempFile, { + create: true, + write: true, + read: true, + }); + + await writeFile(file.rid, "hello world"); + Deno.close(file.rid); + + const data = await Deno.readFile(tempFile); + await Deno.remove(tempFile); + assertEquals(decoder.decode(data), "hello world"); + } +); + +Deno.test( + "Data is written to correct file", + async function testCorrectWriteUsingPath() { + const openResourcesBeforeWrite: Deno.ResourceMap = Deno.resources(); + + await writeFile("_fs_writeFile_test_file.txt", "hello world"); + + assertEquals(Deno.resources(), openResourcesBeforeWrite); + const data = await Deno.readFile("_fs_writeFile_test_file.txt"); + await Deno.remove("_fs_writeFile_test_file.txt"); + assertEquals(decoder.decode(data), "hello world"); + } +); + +Deno.test("Mode is correctly set", async function testCorrectFileMode() { if (Deno.build.os === "windows") return; const filename = "_fs_writeFile_test_file.txt"; await writeFile(filename, "hello world", { mode: 0o777 }); @@ -87,21 +94,24 @@ test("Mode is correctly set", async function testCorrectFileMode() { assertEquals(fileInfo.mode & 0o777, 0o777); }); -test("Mode is not set when rid is passed", async function testCorrectFileModeRid() { - if (Deno.build.os === "windows") return; - - const filename: string = await Deno.makeTempFile(); - const file: Deno.File = await Deno.open(filename, { - create: true, - write: true, - read: true, - }); - - await writeFile(file.rid, "hello world", { mode: 0o777 }); - Deno.close(file.rid); - - const fileInfo = await Deno.stat(filename); - await Deno.remove(filename); - assert(fileInfo.mode); - assertNotEquals(fileInfo.mode & 0o777, 0o777); -}); +Deno.test( + "Mode is not set when rid is passed", + async function testCorrectFileModeRid() { + if (Deno.build.os === "windows") return; + + const filename: string = await Deno.makeTempFile(); + const file: Deno.File = await Deno.open(filename, { + create: true, + write: true, + read: true, + }); + + await writeFile(file.rid, "hello world", { mode: 0o777 }); + Deno.close(file.rid); + + const fileInfo = await Deno.stat(filename); + await Deno.remove(filename); + assert(fileInfo.mode); + assertNotEquals(fileInfo.mode & 0o777, 0o777); + } +); |