2020-05-04 18:59:37 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertNotEquals,
|
|
|
|
assertThrowsAsync,
|
|
|
|
} from "../../../testing/asserts.ts";
|
|
|
|
import { writeFile } from "./_fs_writeFile.ts";
|
2020-07-08 05:26:39 -04:00
|
|
|
import type { TextEncodings } from "../_fs_common.ts";
|
2020-05-04 18:59:37 -04:00
|
|
|
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
|
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
|
|
|
assertThrowsAsync(
|
|
|
|
async () => {
|
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
|
|
|
await writeFile("some/path", "some data", "made-up-encoding");
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
`The value "made-up-encoding" is invalid for option "encoding"`
|
|
|
|
);
|
|
|
|
assertThrowsAsync(
|
|
|
|
async () => {
|
|
|
|
await 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",
|
|
|
|
});
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
`The value "made-up-encoding" is invalid for option "encoding"`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Unsupported encoding results in error()",
|
|
|
|
function testUnsupportedEncoding() {
|
|
|
|
assertThrowsAsync(
|
|
|
|
async () => {
|
2020-06-27 16:52:49 -04:00
|
|
|
await writeFile("some/path", "some data", "utf16le");
|
2020-06-12 15:23:38 -04:00
|
|
|
},
|
|
|
|
Error,
|
2020-06-27 16:52:49 -04:00
|
|
|
`Not implemented: "utf16le" encoding`
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-06-27 16:52:49 -04:00
|
|
|
Deno.test(
|
|
|
|
"Data is written to correct file encodings",
|
|
|
|
async function testCorrectWritePromiseUsingDifferentEncodings() {
|
|
|
|
const encodings = [
|
|
|
|
["hex", "68656c6c6f20776f726c64"],
|
|
|
|
["HEX", "68656c6c6f20776f726c64"],
|
|
|
|
["base64", "aGVsbG8gd29ybGQ="],
|
|
|
|
["BASE64", "aGVsbG8gd29ybGQ="],
|
|
|
|
["utf8", "hello world"],
|
|
|
|
["utf-8", "hello world"],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const [encoding, value] of encodings) {
|
|
|
|
await writeFile(
|
|
|
|
"_fs_writeFile_test_file.txt",
|
|
|
|
value,
|
|
|
|
encoding as TextEncodings
|
|
|
|
);
|
|
|
|
|
|
|
|
const data = await Deno.readFile("_fs_writeFile_test_file.txt");
|
|
|
|
await Deno.remove("_fs_writeFile_test_file.txt");
|
|
|
|
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";
|
|
|
|
await writeFile(filename, "hello world", { mode: 0o777 });
|
|
|
|
|
|
|
|
const fileInfo = await Deno.stat(filename);
|
|
|
|
await Deno.remove(filename);
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|