diff options
author | Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com> | 2018-09-11 09:00:57 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-09-11 12:00:57 -0400 |
commit | 05f87a0cf23a370c0009db8343b3770b518799c8 (patch) | |
tree | b2279e3257382ea1c1ec8c259e9fecdac9e78820 /js/write_file_test.ts | |
parent | c2663e1d82521e9b68a7e2e96197030a4ee00c30 (diff) |
Move writeFileSync to write_file.ts, add writeFile and tests (#728)
Diffstat (limited to 'js/write_file_test.ts')
-rw-r--r-- | js/write_file_test.ts | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/js/write_file_test.ts b/js/write_file_test.ts new file mode 100644 index 000000000..f10343aeb --- /dev/null +++ b/js/write_file_test.ts @@ -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); +}); |