2020-05-04 18:59:37 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertNotEquals,
|
|
|
|
assertThrows,
|
|
|
|
} from "../../testing/asserts.ts";
|
2020-05-15 09:50:27 -04:00
|
|
|
import { writeFile, writeFileSync } from "./_fs_writeFile.ts";
|
2020-07-08 05:26:39 -04:00
|
|
|
import type { TextEncodings } from "./_fs_common.ts";
|
2020-05-22 22:11:10 -04:00
|
|
|
import * as path from "../../path/mod.ts";
|
2020-05-04 18:59:37 -04:00
|
|
|
|
2020-05-22 22:11:10 -04:00
|
|
|
const testDataDir = path.resolve(path.join("node", "_fs", "testdata"));
|
2020-05-04 18:59:37 -04:00
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Callback must be a function error", function fn() {
|
2020-05-04 18:59:37 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
writeFile("some/path", "some data", "utf8");
|
|
|
|
},
|
|
|
|
TypeError,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Callback must be a function.",
|
2020-05-04 18:59:37 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Invalid encoding results in error()", function testEncodingErrors() {
|
2020-05-04 18:59:37 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
2020-06-23 22:32:43 -04:00
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
2020-05-04 18:59:37 -04:00
|
|
|
writeFile("some/path", "some data", "made-up-encoding", () => {});
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`The value "made-up-encoding" is invalid for option "encoding"`,
|
2020-05-04 18:59:37 -04:00
|
|
|
);
|
2020-05-15 09:50:27 -04:00
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2020-06-23 22:32:43 -04:00
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
2020-05-15 09:50:27 -04:00
|
|
|
writeFileSync("some/path", "some data", "made-up-encoding");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`The value "made-up-encoding" is invalid for option "encoding"`,
|
2020-05-15 09:50:27 -04:00
|
|
|
);
|
|
|
|
|
2020-05-04 18:59:37 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
writeFile(
|
|
|
|
"some/path",
|
|
|
|
"some data",
|
|
|
|
{
|
2020-06-23 22:32:43 -04:00
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
2020-05-04 18:59:37 -04:00
|
|
|
encoding: "made-up-encoding",
|
|
|
|
},
|
2020-07-14 15:24:17 -04:00
|
|
|
() => {},
|
2020-05-04 18:59:37 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`The value "made-up-encoding" is invalid for option "encoding"`,
|
2020-05-04 18:59:37 -04:00
|
|
|
);
|
2020-05-15 09:50:27 -04:00
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
writeFileSync("some/path", "some data", {
|
2020-06-23 22:32:43 -04:00
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
2020-05-15 09:50:27 -04:00
|
|
|
encoding: "made-up-encoding",
|
|
|
|
});
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`The value "made-up-encoding" is invalid for option "encoding"`,
|
2020-05-15 09:50:27 -04:00
|
|
|
);
|
2020-05-04 18:59:37 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Unsupported encoding results in error()",
|
|
|
|
function testUnsupportedEncoding() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2020-06-27 16:52:49 -04:00
|
|
|
writeFile("some/path", "some data", "utf16le", () => {});
|
2020-06-12 15:23:38 -04:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`Not implemented: "utf16le" encoding`,
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2020-06-27 16:52:49 -04:00
|
|
|
writeFileSync("some/path", "some data", "utf16le");
|
2020-06-12 15:23:38 -04:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`Not implemented: "utf16le" encoding`,
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
2020-05-15 09:50:27 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
writeFile(file.rid, "hello world", (err) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve();
|
2020-05-15 09:50:27 -04:00
|
|
|
});
|
2020-05-04 18:59:37 -04:00
|
|
|
});
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.close(file.rid);
|
|
|
|
|
|
|
|
const data = await Deno.readFile(tempFile);
|
|
|
|
await Deno.remove(tempFile);
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2020-05-04 18:59:37 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
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");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-05-04 18:59:37 -04:00
|
|
|
|
2020-06-27 16:52:49 -04:00
|
|
|
Deno.test(
|
|
|
|
"Data is written to correct file encodings",
|
|
|
|
async function testCorrectWriteUsingDifferentEncodings() {
|
|
|
|
const encodings = [
|
|
|
|
["hex", "68656c6c6f20776f726c64"],
|
|
|
|
["HEX", "68656c6c6f20776f726c64"],
|
|
|
|
["base64", "aGVsbG8gd29ybGQ="],
|
|
|
|
["BASE64", "aGVsbG8gd29ybGQ="],
|
|
|
|
["utf8", "hello world"],
|
|
|
|
["utf-8", "hello world"],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const [encoding, value] of encodings) {
|
|
|
|
const res = await new Promise((resolve) => {
|
|
|
|
writeFile(
|
|
|
|
"_fs_writeFile_test_file.txt",
|
|
|
|
value,
|
|
|
|
encoding as TextEncodings,
|
2020-07-14 15:24:17 -04:00
|
|
|
resolve,
|
2020-06-27 16:52:49 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-27 16:52:49 -04:00
|
|
|
);
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Path can be an URL", async function testCorrectWriteUsingURL() {
|
2020-05-22 22:11:10 -04:00
|
|
|
const url = new URL(
|
|
|
|
Deno.build.os === "windows"
|
|
|
|
? "file:///" +
|
|
|
|
path
|
|
|
|
.join(testDataDir, "_fs_writeFile_test_file_url.txt")
|
|
|
|
.replace(/\\/g, "/")
|
2020-07-14 15:24:17 -04:00
|
|
|
: "file://" + path.join(testDataDir, "_fs_writeFile_test_file_url.txt"),
|
2020-05-22 22:11:10 -04:00
|
|
|
);
|
|
|
|
const filePath = path.fromFileUrl(url);
|
|
|
|
const res = await new Promise((resolve) => {
|
|
|
|
writeFile(url, "hello world", resolve);
|
|
|
|
});
|
|
|
|
assert(res === null);
|
|
|
|
|
|
|
|
const data = await Deno.readFile(filePath);
|
|
|
|
await Deno.remove(filePath);
|
|
|
|
assertEquals(res, null);
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Mode is correctly set", async function testCorrectFileMode() {
|
2020-05-04 18:59:37 -04:00
|
|
|
if (Deno.build.os === "windows") return;
|
|
|
|
const filename = "_fs_writeFile_test_file.txt";
|
|
|
|
|
|
|
|
const res = await new Promise((resolve) => {
|
|
|
|
writeFile(filename, "hello world", { mode: 0o777 }, resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
const fileInfo = await Deno.stat(filename);
|
|
|
|
await Deno.remove(filename);
|
|
|
|
assertEquals(res, null);
|
|
|
|
assert(fileInfo && fileInfo.mode);
|
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Mode is not set when rid is passed",
|
|
|
|
async function testCorrectFileModeRid() {
|
|
|
|
if (Deno.build.os === "windows") return;
|
2020-05-04 18:59:37 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
const filename: string = await Deno.makeTempFile();
|
|
|
|
const file: Deno.File = await Deno.open(filename, {
|
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
read: true,
|
2020-05-04 18:59:37 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
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);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
2020-05-15 09:50:27 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
writeFileSync(file.rid, "hello world");
|
|
|
|
Deno.close(file.rid);
|
2020-05-15 09:50:27 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
const data = Deno.readFileSync(tempFile);
|
|
|
|
Deno.removeSync(tempFile);
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-05-15 09:50:27 -04:00
|
|
|
|
2020-06-27 16:52:49 -04:00
|
|
|
Deno.test(
|
|
|
|
"Data is written to correct file encodings",
|
|
|
|
function testCorrectWriteSyncUsingDifferentEncodings() {
|
|
|
|
const encodings = [
|
|
|
|
["hex", "68656c6c6f20776f726c64"],
|
|
|
|
["HEX", "68656c6c6f20776f726c64"],
|
|
|
|
["base64", "aGVsbG8gd29ybGQ="],
|
|
|
|
["BASE64", "aGVsbG8gd29ybGQ="],
|
|
|
|
["utf8", "hello world"],
|
|
|
|
["utf-8", "hello world"],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const [encoding, value] of encodings) {
|
|
|
|
const file = "_fs_writeFileSync_test_file";
|
|
|
|
writeFileSync(file, value, encoding as TextEncodings);
|
|
|
|
|
|
|
|
const data = Deno.readFileSync(file);
|
|
|
|
Deno.removeSync(file);
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-27 16:52:49 -04:00
|
|
|
);
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Data is written synchronously to correct file",
|
|
|
|
function testCorrectWriteSyncUsingPath() {
|
|
|
|
const file = "_fs_writeFileSync_test_file";
|
2020-05-15 09:50:27 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
writeFileSync(file, "hello world");
|
2020-05-15 09:50:27 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
const data = Deno.readFileSync(file);
|
|
|
|
Deno.removeSync(file);
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-05-15 09:50:27 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("sync: Path can be an URL", function testCorrectWriteSyncUsingURL() {
|
2020-05-22 22:11:10 -04:00
|
|
|
const filePath = path.join(
|
|
|
|
testDataDir,
|
2020-07-14 15:24:17 -04:00
|
|
|
"_fs_writeFileSync_test_file_url.txt",
|
2020-05-22 22:11:10 -04:00
|
|
|
);
|
|
|
|
const url = new URL(
|
|
|
|
Deno.build.os === "windows"
|
|
|
|
? "file:///" + filePath.replace(/\\/g, "/")
|
2020-07-14 15:24:17 -04:00
|
|
|
: "file://" + filePath,
|
2020-05-22 22:11:10 -04:00
|
|
|
);
|
|
|
|
writeFileSync(url, "hello world");
|
|
|
|
|
|
|
|
const data = Deno.readFileSync(filePath);
|
|
|
|
Deno.removeSync(filePath);
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Mode is correctly set when writing synchronously",
|
|
|
|
function testCorrectFileModeSync() {
|
|
|
|
if (Deno.build.os === "windows") return;
|
|
|
|
const filename = "_fs_writeFileSync_test_file.txt";
|
2020-05-15 09:50:27 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
writeFileSync(filename, "hello world", { mode: 0o777 });
|
2020-05-15 09:50:27 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
const fileInfo = Deno.statSync(filename);
|
|
|
|
Deno.removeSync(filename);
|
|
|
|
assert(fileInfo && fileInfo.mode);
|
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|