2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-24 18:57:08 -04:00
|
|
|
import {
|
|
|
|
unitTest,
|
|
|
|
assertEquals,
|
|
|
|
assertThrows,
|
|
|
|
assertThrowsAsync,
|
|
|
|
} from "./test_util.ts";
|
2018-09-11 12:00:57 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function writeFileSyncSuccess(): void {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
Deno.writeFileSync(filename, data);
|
|
|
|
const dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEquals("Hello", actual);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-09-11 12:00:57 -04:00
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function writeFileSyncUrl(): void {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fileUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
Deno.writeFileSync(fileUrl, data);
|
|
|
|
const dataRead = Deno.readFileSync(fileUrl);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEquals("Hello", actual);
|
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { write: true } }, function writeFileSyncFail(): void {
|
2018-09-11 12:00:57 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail because /baddir doesn't exist (hopefully).
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2018-09-11 12:00:57 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { write: false } }, function writeFileSyncPerm(): void {
|
2018-09-11 12:00:57 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail due to no write permission
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2018-09-11 12:00:57 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2020-03-07 22:29:12 -05:00
|
|
|
function writeFileSyncUpdateMode(): void {
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o755 });
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o666 });
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function writeFileSyncCreate(): void {
|
2019-02-02 14:26:18 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
2019-02-12 10:08:56 -05:00
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
2020-03-04 11:31:14 -05:00
|
|
|
// if create turned off, the file won't be created
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.writeFileSync(filename, data, { create: false });
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
// Turn on create, should have no error
|
|
|
|
Deno.writeFileSync(filename, data, { create: true });
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data, { create: false });
|
2020-03-04 11:31:14 -05:00
|
|
|
const dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEquals("Hello", actual);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function writeFileSyncAppend(): void {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
Deno.writeFileSync(filename, data);
|
|
|
|
Deno.writeFileSync(filename, data, { append: true });
|
|
|
|
let dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
let actual = dec.decode(dataRead);
|
|
|
|
assertEquals("HelloHello", actual);
|
|
|
|
// Now attempt overwrite
|
|
|
|
Deno.writeFileSync(filename, data, { append: false });
|
|
|
|
dataRead = Deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
|
|
|
assertEquals("Hello", actual);
|
|
|
|
// append not set should also overwrite
|
|
|
|
Deno.writeFileSync(filename, data);
|
|
|
|
dataRead = Deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
|
|
|
assertEquals("Hello", actual);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2019-04-21 16:40:10 -04:00
|
|
|
async function writeFileSuccess(): Promise<void> {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.writeFile(filename, data);
|
2019-04-21 16:40:10 -04:00
|
|
|
const dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEquals("Hello", actual);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function writeFileUrl(): Promise<void> {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
const fileUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
await Deno.writeFile(fileUrl, data);
|
|
|
|
const dataRead = Deno.readFileSync(fileUrl);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEquals("Hello", actual);
|
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2019-04-21 16:40:10 -04:00
|
|
|
async function writeFileNotFound(): Promise<void> {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail because /baddir doesn't exist (hopefully).
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2019-04-21 16:40:10 -04:00
|
|
|
await Deno.writeFile(filename, data);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2018-09-11 12:00:57 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: false } },
|
|
|
|
async function writeFilePerm(): Promise<void> {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail due to no write permission
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.writeFile(filename, data);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2020-03-07 22:29:12 -05:00
|
|
|
async function writeFileUpdateMode(): Promise<void> {
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
2019-04-21 16:40:10 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
2020-03-07 22:29:12 -05:00
|
|
|
await Deno.writeFile(filename, data, { mode: 0o755 });
|
2020-02-19 15:36:18 -05:00
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
|
2020-03-07 22:29:12 -05:00
|
|
|
await Deno.writeFile(filename, data, { mode: 0o666 });
|
2020-02-19 15:36:18 -05:00
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
|
2019-04-21 16:40:10 -04:00
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function writeFileCreate(): Promise<void> {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
// if create turned off, the file won't be created
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.writeFile(filename, data, { create: false });
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2020-03-04 11:31:14 -05:00
|
|
|
|
|
|
|
// Turn on create, should have no error
|
|
|
|
await Deno.writeFile(filename, data, { create: true });
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.writeFile(filename, data, { create: false });
|
2020-03-04 11:31:14 -05:00
|
|
|
const dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEquals("Hello", actual);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function writeFileAppend(): Promise<void> {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await Deno.writeFile(filename, data);
|
|
|
|
await Deno.writeFile(filename, data, { append: true });
|
|
|
|
let dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
let actual = dec.decode(dataRead);
|
|
|
|
assertEquals("HelloHello", actual);
|
|
|
|
// Now attempt overwrite
|
|
|
|
await Deno.writeFile(filename, data, { append: false });
|
|
|
|
dataRead = Deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
|
|
|
assertEquals("Hello", actual);
|
|
|
|
// append not set should also overwrite
|
|
|
|
await Deno.writeFile(filename, data);
|
|
|
|
dataRead = Deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
|
|
|
assertEquals("Hello", actual);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|