diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/deno.ts | 2 | ||||
-rw-r--r-- | js/os.ts | 25 | ||||
-rw-r--r-- | js/unit_tests.ts | 30 |
3 files changed, 52 insertions, 5 deletions
diff --git a/js/deno.ts b/js/deno.ts index 060b2d526..ff9939b1b 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -1,4 +1,4 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. // Public deno module. -export { exit, readFileSync } from "./os"; +export { exit, readFileSync, writeFileSync } from "./os"; export { libdeno } from "./globals"; @@ -121,15 +121,32 @@ export function readFileSync(filename: string): Uint8Array { export function writeFileSync( filename: string, data: Uint8Array, - perm: number + perm = 0o666 ): void { - util.notImplemented(); - /* - pubInternal("os", { + /* Ideally we could write: + const res = send({ command: fbs.Command.WRITE_FILE_SYNC, writeFileSyncFilename: filename, writeFileSyncData: data, writeFileSyncPerm: perm }); */ + const builder = new flatbuffers.Builder(); + const filename_ = builder.createString(filename); + const dataOffset = fbs.WriteFileSync.createDataVector(builder, data); + fbs.WriteFileSync.startWriteFileSync(builder); + fbs.WriteFileSync.addFilename(builder, filename_); + fbs.WriteFileSync.addData(builder, dataOffset); + fbs.WriteFileSync.addPerm(builder, perm); + const msg = fbs.WriteFileSync.endWriteFileSync(builder); + fbs.Base.startBase(builder); + fbs.Base.addMsg(builder, msg); + fbs.Base.addMsgType(builder, fbs.Any.WriteFileSync); + builder.finish(fbs.Base.endBase(builder)); + const resBuf = libdeno.send(builder.asUint8Array()); + if (resBuf != null) { + const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!)); + const baseRes = fbs.Base.getRootAsBase(bb); + maybeThrowError(baseRes); + } } diff --git a/js/unit_tests.ts b/js/unit_tests.ts index f203444fa..6eebf8860 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -5,6 +5,7 @@ import { test, assert, assertEqual } from "./testing/testing.ts"; import { readFileSync } from "deno"; +import * as deno from "deno"; import "./compiler_test.ts"; @@ -109,6 +110,35 @@ test(function tests_readFileSync_NotFound() { }); */ +/* TODO(ry) Add this once we can create a tmpDir to write the file into. +test(function writeFileSyncSuccess() { + const enc = new TextEncoder(); + const dataWritten = enc.encode("Hello"); + const filename = "TEMPDIR/test.txt"; + deno.writeFileSync(filename, dataWritten, 0o666); + const dataRead = readFileSync(filename); + assertEqual(dataRead, dataWritten); +}); +*/ + +// For this test to pass we need --allow-write permission. +// Otherwise it will fail with deno.PermissionDenied instead of deno.NotFound. +test(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; + // TODO assertEqual(e, deno.NotFound); + assertEqual(e.name, "deno.NotFound"); + } + assert(caughtError); +}); + test(async function tests_fetch() { const response = await fetch("http://localhost:4545/package.json"); const json = await response.json(); |