summaryrefslogtreecommitdiff
path: root/std/node/_fs/_fs_writeFile_test.ts
diff options
context:
space:
mode:
authorMarcos Casagrande <marcoscvp90@gmail.com>2020-05-15 15:50:27 +0200
committerGitHub <noreply@github.com>2020-05-15 09:50:27 -0400
commitce57a1824d3c89d19460efb315b273a43d18373e (patch)
treec8b6ba52ec4834cd4398224cb3fe73c6b3e7b0f2 /std/node/_fs/_fs_writeFile_test.ts
parent62a7fcbdc40479b7fdae98eeaedab1efc7b95d4a (diff)
feat(std/node): fs.writeFileSync polyfill (#5414)
Diffstat (limited to 'std/node/_fs/_fs_writeFile_test.ts')
-rw-r--r--std/node/_fs/_fs_writeFile_test.ts80
1 files changed, 78 insertions, 2 deletions
diff --git a/std/node/_fs/_fs_writeFile_test.ts b/std/node/_fs/_fs_writeFile_test.ts
index 427062034..f3598f0b0 100644
--- a/std/node/_fs/_fs_writeFile_test.ts
+++ b/std/node/_fs/_fs_writeFile_test.ts
@@ -7,11 +7,11 @@ import {
assertNotEquals,
assertThrows,
} from "../../testing/asserts.ts";
-import { writeFile } from "./_fs_writeFile.ts";
+import { writeFile, writeFileSync } from "./_fs_writeFile.ts";
const decoder = new TextDecoder("utf-8");
-test("Invalid encoding results in error()", function fn() {
+test("Callback must be a function error", function fn() {
assertThrows(
() => {
writeFile("some/path", "some data", "utf8");
@@ -29,6 +29,15 @@ test("Invalid encoding results in error()", function testEncodingErrors() {
Error,
`The value "made-up-encoding" is invalid for option "encoding"`
);
+
+ assertThrows(
+ () => {
+ writeFileSync("some/path", "some data", "made-up-encoding");
+ },
+ Error,
+ `The value "made-up-encoding" is invalid for option "encoding"`
+ );
+
assertThrows(
() => {
writeFile(
@@ -43,6 +52,16 @@ test("Invalid encoding results in error()", function testEncodingErrors() {
Error,
`The value "made-up-encoding" is invalid for option "encoding"`
);
+
+ assertThrows(
+ () => {
+ writeFileSync("some/path", "some data", {
+ encoding: "made-up-encoding",
+ });
+ },
+ Error,
+ `The value "made-up-encoding" is invalid for option "encoding"`
+ );
});
test("Unsupported encoding results in error()", function testUnsupportedEncoding() {
@@ -53,6 +72,15 @@ test("Unsupported encoding results in error()", function testUnsupportedEncoding
Error,
`Not implemented: "hex" encoding`
);
+
+ assertThrows(
+ () => {
+ writeFileSync("some/path", "some data", "hex");
+ },
+ Error,
+ `Not implemented: "hex" encoding`
+ );
+
assertThrows(
() => {
writeFile(
@@ -67,6 +95,16 @@ test("Unsupported encoding results in error()", function testUnsupportedEncoding
Error,
`Not implemented: "base64" encoding`
);
+
+ assertThrows(
+ () => {
+ writeFileSync("some/path", "some data", {
+ encoding: "base64",
+ });
+ },
+ Error,
+ `Not implemented: "base64" encoding`
+ );
});
test("Data is written to correct rid", async function testCorrectWriteUsingRid() {
@@ -160,3 +198,41 @@ test("Mode is not set when rid is passed", async function testCorrectFileModeRid
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,
+ });
+
+ writeFileSync(file.rid, "hello world");
+ Deno.close(file.rid);
+
+ 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";
+
+ writeFileSync(file, "hello world");
+
+ const data = Deno.readFileSync(file);
+ Deno.removeSync(file);
+ 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";
+
+ writeFileSync(filename, "hello world", { mode: 0o777 });
+
+ const fileInfo = Deno.statSync(filename);
+ Deno.removeSync(filename);
+ assert(fileInfo && fileInfo.mode);
+ assertEquals(fileInfo.mode & 0o777, 0o777);
+});