2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-09-11 12:00:57 -04:00
|
|
|
import { testPerm, assert, assertEqual } from "./test_util.ts";
|
|
|
|
import * as deno from "deno";
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, function writeFileSyncSuccess() {
|
2018-09-11 12:00:57 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = deno.makeTempDirSync() + "/test.txt";
|
2019-02-02 14:26:18 -05:00
|
|
|
deno.writeFileSync(filename, data);
|
2018-09-11 12:00:57 -04:00
|
|
|
const dataRead = deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEqual("Hello", actual);
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, function writeFileSyncFail() {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail because /baddir doesn't exist (hopefully).
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
|
|
|
deno.writeFileSync(filename, data);
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assertEqual(e.kind, deno.ErrorKind.NotFound);
|
|
|
|
assertEqual(e.name, "NotFound");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: false }, function writeFileSyncPerm() {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail due to no write permission
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
|
|
|
deno.writeFileSync(filename, data);
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEqual(e.name, "PermissionDenied");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, function writeFileSyncUpdatePerm() {
|
2019-02-02 14:26:18 -05:00
|
|
|
if (deno.platform.os !== "win") {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = deno.makeTempDirSync() + "/test.txt";
|
|
|
|
deno.writeFileSync(filename, data, { perm: 0o755 });
|
|
|
|
assertEqual(deno.statSync(filename).mode & 0o777, 0o755);
|
|
|
|
deno.writeFileSync(filename, data, { perm: 0o666 });
|
|
|
|
assertEqual(deno.statSync(filename).mode & 0o777, 0o666);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, function writeFileSyncCreate() {
|
2019-02-02 14:26:18 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = deno.makeTempDirSync() + "/test.txt";
|
|
|
|
let caughtError = false;
|
|
|
|
// if create turned off, the file won't be created
|
|
|
|
try {
|
|
|
|
deno.writeFileSync(filename, data, { create: false });
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assertEqual(e.kind, deno.ErrorKind.NotFound);
|
|
|
|
assertEqual(e.name, "NotFound");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
|
|
|
|
// Turn on create, should have no error
|
|
|
|
deno.writeFileSync(filename, data, { create: true });
|
|
|
|
deno.writeFileSync(filename, data, { create: false });
|
|
|
|
const dataRead = deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEqual("Hello", actual);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, function writeFileSyncAppend() {
|
2019-02-02 14:26:18 -05:00
|
|
|
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);
|
|
|
|
assertEqual("HelloHello", actual);
|
|
|
|
// Now attempt overwrite
|
|
|
|
deno.writeFileSync(filename, data, { append: false });
|
|
|
|
dataRead = deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
|
|
|
assertEqual("Hello", actual);
|
|
|
|
// append not set should also overwrite
|
|
|
|
deno.writeFileSync(filename, data);
|
|
|
|
dataRead = deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
|
|
|
assertEqual("Hello", actual);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function writeFileSuccess() {
|
2018-09-11 12:00:57 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = deno.makeTempDirSync() + "/test.txt";
|
2019-02-02 14:26:18 -05:00
|
|
|
await deno.writeFile(filename, data);
|
2018-09-11 12:00:57 -04:00
|
|
|
const dataRead = deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEqual("Hello", actual);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function writeFileNotFound() {
|
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).
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
|
|
|
await deno.writeFile(filename, data);
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assertEqual(e.kind, deno.ErrorKind.NotFound);
|
|
|
|
assertEqual(e.name, "NotFound");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: false }, async function writeFilePerm() {
|
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
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
|
|
|
await deno.writeFile(filename, data);
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEqual(e.name, "PermissionDenied");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function writeFileUpdatePerm() {
|
2019-02-02 14:26:18 -05:00
|
|
|
if (deno.platform.os !== "win") {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await deno.writeFile(filename, data, { perm: 0o755 });
|
|
|
|
assertEqual(deno.statSync(filename).mode & 0o777, 0o755);
|
|
|
|
await deno.writeFile(filename, data, { perm: 0o666 });
|
|
|
|
assertEqual(deno.statSync(filename).mode & 0o777, 0o666);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function writeFileCreate() {
|
2019-02-02 14:26:18 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = deno.makeTempDirSync() + "/test.txt";
|
|
|
|
let caughtError = false;
|
|
|
|
// if create turned off, the file won't be created
|
|
|
|
try {
|
|
|
|
await deno.writeFile(filename, data, { create: false });
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assertEqual(e.kind, deno.ErrorKind.NotFound);
|
|
|
|
assertEqual(e.name, "NotFound");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
|
|
|
|
// Turn on create, should have no error
|
|
|
|
await deno.writeFile(filename, data, { create: true });
|
|
|
|
await deno.writeFile(filename, data, { create: false });
|
|
|
|
const dataRead = deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEqual("Hello", actual);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function writeFileAppend() {
|
2019-02-02 14:26:18 -05:00
|
|
|
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);
|
|
|
|
assertEqual("HelloHello", actual);
|
|
|
|
// Now attempt overwrite
|
|
|
|
await deno.writeFile(filename, data, { append: false });
|
|
|
|
dataRead = deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
|
|
|
assertEqual("Hello", actual);
|
|
|
|
// append not set should also overwrite
|
|
|
|
await deno.writeFile(filename, data);
|
|
|
|
dataRead = deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
|
|
|
assertEqual("Hello", actual);
|
|
|
|
});
|