mirror of
https://github.com/denoland/deno.git
synced 2025-10-01 22:51:14 +00:00
Move writeFileSync to write_file.ts, add writeFile and tests (#728)
This commit is contained in:
parent
c2663e1d82
commit
05f87a0cf2
10 changed files with 192 additions and 83 deletions
89
js/write_file_test.ts
Normal file
89
js/write_file_test.ts
Normal file
|
@ -0,0 +1,89 @@
|
|||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||
import { testPerm, assert, assertEqual } from "./test_util.ts";
|
||||
import * as deno from "deno";
|
||||
|
||||
testPerm({ write: true }, function writeFileSyncSuccess() {
|
||||
const enc = new TextEncoder();
|
||||
const data = enc.encode("Hello");
|
||||
const filename = deno.makeTempDirSync() + "/test.txt";
|
||||
deno.writeFileSync(filename, data, 0o666);
|
||||
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);
|
||||
});
|
||||
|
||||
testPerm({ write: true }, async function writeFileSuccess() {
|
||||
const enc = new TextEncoder();
|
||||
const data = enc.encode("Hello");
|
||||
const filename = deno.makeTempDirSync() + "/test.txt";
|
||||
await deno.writeFile(filename, data, 0o666);
|
||||
const dataRead = deno.readFileSync(filename);
|
||||
const dec = new TextDecoder("utf-8");
|
||||
const actual = dec.decode(dataRead);
|
||||
assertEqual("Hello", actual);
|
||||
});
|
||||
|
||||
testPerm({ write: true }, async function writeFileNotFound() {
|
||||
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);
|
||||
});
|
||||
|
||||
testPerm({ write: false }, async function writeFilePerm() {
|
||||
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);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue