diff options
author | Bert Belder <bertbelder@gmail.com> | 2018-08-24 00:36:45 +0200 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2018-08-29 22:40:05 +0200 |
commit | ceaf82268282d16b97101c00c75612745de416bb (patch) | |
tree | 6294eeeb063ca2196ef285ad7f125437cbc3bd45 /js | |
parent | a836c493f30323e7b40e988140ed2603f0e3d10f (diff) |
Implement makeTempDirSync()
Diffstat (limited to 'js')
-rw-r--r-- | js/deno.ts | 2 | ||||
-rw-r--r-- | js/os.ts | 51 | ||||
-rw-r--r-- | js/unit_tests.ts | 69 |
3 files changed, 99 insertions, 23 deletions
diff --git a/js/deno.ts b/js/deno.ts index 63bee0b7c..7a5c4217e 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -1,5 +1,5 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. // Public deno module. -export { exit, readFileSync, writeFileSync } from "./os"; +export { exit, makeTempDirSync, readFileSync, writeFileSync } from "./os"; export { libdeno } from "./libdeno"; export const argv: string[] = []; @@ -85,6 +85,57 @@ export function codeCache( } } +/** + * makeTempDirSync creates a new temporary directory in the directory `dir`, its + * name beginning with `prefix` and ending with `suffix`. + * It returns the full path to the newly created directory. + * If `dir` is unspecified, tempDir uses the default directory for temporary + * files. Multiple programs calling tempDir simultaneously will not choose the + * same directory. It is the caller's responsibility to remove the directory + * when no longer needed. + */ +export interface MakeTempDirOptions { + dir?: string; + prefix?: string; + suffix?: string; +} +export function makeTempDirSync({ + dir, + prefix, + suffix +}: MakeTempDirOptions = {}): string { + const builder = new flatbuffers.Builder(); + const fbDir = dir == null ? -1 : builder.createString(dir); + const fbPrefix = prefix == null ? -1 : builder.createString(prefix); + const fbSuffix = suffix == null ? -1 : builder.createString(suffix); + fbs.MakeTempDir.startMakeTempDir(builder); + if (dir != null) { + fbs.MakeTempDir.addDir(builder, fbDir); + } + if (prefix != null) { + fbs.MakeTempDir.addPrefix(builder, fbPrefix); + } + if (suffix != null) { + fbs.MakeTempDir.addSuffix(builder, fbSuffix); + } + const msg = fbs.MakeTempDir.endMakeTempDir(builder); + fbs.Base.startBase(builder); + fbs.Base.addMsg(builder, msg); + fbs.Base.addMsgType(builder, fbs.Any.MakeTempDir); + builder.finish(fbs.Base.endBase(builder)); + const resBuf = libdeno.send(builder.asUint8Array()); + assert(resBuf != null); + const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!)); + const baseRes = fbs.Base.getRootAsBase(bb); + maybeThrowError(baseRes); + assert(fbs.Any.MakeTempDirRes === baseRes.msgType()); + const res = new fbs.MakeTempDirRes(); + assert(baseRes.msg(res) != null); + const path = res.path(); + assert(path != null); + return path!; +} + export function readFileSync(filename: string): Uint8Array { /* Ideally we could write const res = send({ diff --git a/js/unit_tests.ts b/js/unit_tests.ts index 17ddd5256..92957c493 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -41,16 +41,16 @@ test(function tests_readFileSync_NotFound() { }); */ -/* TODO(ry) Add this once we can create a tmpDir to write the file into. -test(function writeFileSyncSuccess() { +testPerm({ write: true }, function writeFileSync() { const enc = new TextEncoder(); - const dataWritten = enc.encode("Hello"); - const filename = "TEMPDIR/test.txt"; - deno.writeFileSync(filename, dataWritten, 0o666); + const data = enc.encode("Hello"); + const filename = deno.makeTempDirSync() + "/test.txt"; + deno.writeFileSync(filename, data, 0o666); const dataRead = deno.readFileSync(filename); - assertEqual(dataRead, dataWritten); + const dec = new TextDecoder("utf-8"); + const actual = dec.decode(dataRead); + assertEqual("Hello", actual); }); -*/ // For this test to pass we need --allow-write permission. // Otherwise it will fail with deno.PermissionDenied instead of deno.NotFound. @@ -70,23 +70,48 @@ testPerm({ write: true }, function writeFileSyncFail() { assert(caughtError); }); +testPerm({ write: true }, function makeTempDirSync() { + const dir1 = deno.makeTempDirSync({ prefix: "hello", suffix: "world" }); + const dir2 = deno.makeTempDirSync({ prefix: "hello", suffix: "world" }); + // Check that both dirs are different. + assert(dir1 != dir2); + for (const dir of [dir1, dir2]) { + // Check that the prefix and suffix are applied. + const lastPart = dir.replace(/^.*[\\\/]/, ""); + assert(lastPart.startsWith("hello")); + assert(lastPart.endsWith("world")); + } + // Check that the `dir` option works. + const dir3 = deno.makeTempDirSync({ dir: dir1 }); + assert(dir3.startsWith(dir1)); + assert(/^[\\\/]/.test(dir3.slice(dir1.length))); + // Check that creating a temp dir inside a nonexisting directory fails. + let err; + try { + deno.makeTempDirSync({ dir: "/baddir" }); + } catch (err_) { + err = err_; + } + // TODO assert(err instanceof deno.NotFound). + assert(err); + assertEqual(err.name, "deno.NotFound"); +}); + +test(function makeTempDirSyncPerm() { + // makeTempDirSync should require write permissions (for now). + let err; + try { + deno.makeTempDirSync({ dir: "/baddir" }); + } catch (err_) { + err = err_; + } + // TODO assert(err instanceof deno.PermissionDenied). + assert(err); + assertEqual(err.name, "deno.PermissionDenied"); +}); + testPerm({ net: true }, async function tests_fetch() { const response = await fetch("http://localhost:4545/package.json"); const json = await response.json(); assertEqual(json.name, "deno"); }); - -/* -test(async function tests_writeFileSync() { - const enc = new TextEncoder(); - const data = enc.encode("Hello"); - // TODO need ability to get tmp dir. - const fn = "/tmp/test.txt"; - writeFileSync("/tmp/test.txt", data, 0o666); - const dataRead = deno.readFileSync("/tmp/test.txt"); - const dec = new TextDecoder("utf-8"); - const actual = dec.decode(dataRead); - assertEqual("Hello", actual); -}); - -*/ |