diff options
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); +}); |