diff options
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/deno.ts | 6 | ||||
-rw-r--r-- | cli/js/dispatch.ts | 1 | ||||
-rw-r--r-- | cli/js/lib.deno.ns.d.ts | 28 | ||||
-rw-r--r-- | cli/js/make_temp.ts | 61 | ||||
-rw-r--r-- | cli/js/make_temp_dir.ts | 35 | ||||
-rw-r--r-- | cli/js/make_temp_test.ts (renamed from cli/js/make_temp_dir_test.ts) | 66 | ||||
-rw-r--r-- | cli/js/unit_tests.ts | 2 |
7 files changed, 157 insertions, 42 deletions
diff --git a/cli/js/deno.ts b/cli/js/deno.ts index c52e6dc2d..cdfedcd76 100644 --- a/cli/js/deno.ts +++ b/cli/js/deno.ts @@ -66,8 +66,10 @@ export { linkSync, link } from "./link.ts"; export { makeTempDirSync, makeTempDir, - MakeTempDirOptions -} from "./make_temp_dir.ts"; + makeTempFileSync, + makeTempFile, + MakeTempOptions +} from "./make_temp.ts"; export { metrics, Metrics } from "./metrics.ts"; export { mkdirSync, mkdir } from "./mkdir.ts"; export { diff --git a/cli/js/dispatch.ts b/cli/js/dispatch.ts index 4493d3771..b4677b219 100644 --- a/cli/js/dispatch.ts +++ b/cli/js/dispatch.ts @@ -65,6 +65,7 @@ export let OP_SYMLINK: number; export let OP_READ_LINK: number; export let OP_TRUNCATE: number; export let OP_MAKE_TEMP_DIR: number; +export let OP_MAKE_TEMP_FILE: number; export let OP_CWD: number; export let OP_CONNECT_TLS: number; export let OP_HOSTNAME: number; diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index b96e108c3..12e8ae4ba 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -680,9 +680,9 @@ declare namespace Deno { mode?: number ): Promise<void>; - // @url js/make_temp_dir.d.ts + // @url js/make_temp.d.ts - export interface MakeTempDirOptions { + export interface MakeTempOptions { dir?: string; prefix?: string; suffix?: string; @@ -696,7 +696,7 @@ declare namespace Deno { * Requires allow-write. */ // TODO(ry) Doesn't check permissions. - export function makeTempDirSync(options?: MakeTempDirOptions): string; + export function makeTempDirSync(options?: MakeTempOptions): string; /** makeTempDir creates a new temporary directory in the directory `dir`, its * name beginning with `prefix` and ending with `suffix`. @@ -712,7 +712,27 @@ declare namespace Deno { * Requires allow-write. */ // TODO(ry) Doesn't check permissions. - export function makeTempDir(options?: MakeTempDirOptions): Promise<string>; + export function makeTempDir(options?: MakeTempOptions): Promise<string>; + + /** makeTempFileSync is the synchronous version of `makeTempFile`. + * + * const tempFileName0 = Deno.makeTempFileSync(); + * const tempFileName1 = Deno.makeTempFileSync({ prefix: 'my_temp' }); + */ + export function makeTempFileSync(options?: MakeTempOptions): string; + + /** makeTempFile creates a new temporary file in the directory `dir`, its + * name beginning with `prefix` and ending with `suffix`. + * It returns the full path to the newly created file. + * If `dir` is unspecified, tempFile uses the default directory for temporary + * files. Multiple programs calling tempFile simultaneously will not choose the + * same directory. It is the caller's responsibility to remove the file + * when no longer needed. + * + * const tempFileName0 = await Deno.makeTempFile(); + * const tempFileName1 = await Deno.makeTempFile({ prefix: 'my_temp' }); + */ + export function makeTempFile(options?: MakeTempOptions): Promise<string>; /** Changes the permission of a specific file/directory of specified path * synchronously. diff --git a/cli/js/make_temp.ts b/cli/js/make_temp.ts new file mode 100644 index 000000000..8b15ab184 --- /dev/null +++ b/cli/js/make_temp.ts @@ -0,0 +1,61 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "./dispatch_json.ts"; +import * as dispatch from "./dispatch.ts"; + +export interface MakeTempOptions { + dir?: string; + prefix?: string; + suffix?: string; +} + +/** makeTempDirSync is the synchronous version of `makeTempDir`. + * + * const tempDirName0 = Deno.makeTempDirSync(); + * const tempDirName1 = Deno.makeTempDirSync({ prefix: 'my_temp' }); + */ +export function makeTempDirSync(options: MakeTempOptions = {}): string { + return sendSync(dispatch.OP_MAKE_TEMP_DIR, options); +} + +/** makeTempDir 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. + * + * const tempDirName0 = await Deno.makeTempDir(); + * const tempDirName1 = await Deno.makeTempDir({ prefix: 'my_temp' }); + */ +export async function makeTempDir( + options: MakeTempOptions = {} +): Promise<string> { + return await sendAsync(dispatch.OP_MAKE_TEMP_DIR, options); +} + +/** makeTempFileSync is the synchronous version of `makeTempFile`. + * + * const tempFileName0 = Deno.makeTempFileSync(); + * const tempFileName1 = Deno.makeTempFileSync({ prefix: 'my_temp' }); + */ +export function makeTempFileSync(options: MakeTempOptions = {}): string { + return sendSync(dispatch.OP_MAKE_TEMP_FILE, options); +} + +/** makeTempFile creates a new temporary file in the directory `dir`, its + * name beginning with `prefix` and ending with `suffix`. + * It returns the full path to the newly created file. + * If `dir` is unspecified, tempFile uses the default directory for temporary + * files. Multiple programs calling tempFile simultaneously will not choose the + * same directory. It is the caller's responsibility to remove the file + * when no longer needed. + * + * const tempFileName0 = await Deno.makeTempFile(); + * const tempFileName1 = await Deno.makeTempFile({ prefix: 'my_temp' }); + */ +export async function makeTempFile( + options: MakeTempOptions = {} +): Promise<string> { + return await sendAsync(dispatch.OP_MAKE_TEMP_FILE, options); +} diff --git a/cli/js/make_temp_dir.ts b/cli/js/make_temp_dir.ts deleted file mode 100644 index 320f9a964..000000000 --- a/cli/js/make_temp_dir.ts +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./dispatch_json.ts"; -import * as dispatch from "./dispatch.ts"; - -export interface MakeTempDirOptions { - dir?: string; - prefix?: string; - suffix?: string; -} - -/** makeTempDirSync is the synchronous version of `makeTempDir`. - * - * const tempDirName0 = Deno.makeTempDirSync(); - * const tempDirName1 = Deno.makeTempDirSync({ prefix: 'my_temp' }); - */ -export function makeTempDirSync(options: MakeTempDirOptions = {}): string { - return sendSync(dispatch.OP_MAKE_TEMP_DIR, options); -} - -/** makeTempDir 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. - * - * const tempDirName0 = await Deno.makeTempDir(); - * const tempDirName1 = await Deno.makeTempDir({ prefix: 'my_temp' }); - */ -export async function makeTempDir( - options: MakeTempDirOptions = {} -): Promise<string> { - return await sendAsync(dispatch.OP_MAKE_TEMP_DIR, options); -} diff --git a/cli/js/make_temp_dir_test.ts b/cli/js/make_temp_test.ts index ef6e7a67e..f6bf59d32 100644 --- a/cli/js/make_temp_dir_test.ts +++ b/cli/js/make_temp_test.ts @@ -64,3 +64,69 @@ testPerm({ write: true }, async function makeTempDirSuccess(): Promise<void> { assertEquals(err.kind, Deno.ErrorKind.NotFound); assertEquals(err.name, "NotFound"); }); + +testPerm({ write: true }, function makeTempFileSyncSuccess(): void { + const file1 = Deno.makeTempFileSync({ prefix: "hello", suffix: "world" }); + const file2 = Deno.makeTempFileSync({ prefix: "hello", suffix: "world" }); + // Check that both dirs are different. + assert(file1 !== file2); + for (const dir of [file1, file2]) { + // 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 dir = Deno.makeTempDirSync({ prefix: "tempdir" }); + const file3 = Deno.makeTempFileSync({ dir }); + assert(file3.startsWith(dir)); + assert(/^[\\\/]/.test(file3.slice(dir.length))); + // Check that creating a temp file inside a nonexisting directory fails. + let err; + try { + Deno.makeTempFileSync({ dir: "/baddir" }); + } catch (err_) { + err = err_; + } + assertEquals(err.kind, Deno.ErrorKind.NotFound); + assertEquals(err.name, "NotFound"); +}); + +test(function makeTempFileSyncPerm(): void { + // makeTempFileSync should require write permissions (for now). + let err; + try { + Deno.makeTempFileSync({ dir: "/baddir" }); + } catch (err_) { + err = err_; + } + assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assertEquals(err.name, "PermissionDenied"); +}); + +testPerm({ write: true }, async function makeTempFileSuccess(): Promise<void> { + const file1 = await Deno.makeTempFile({ prefix: "hello", suffix: "world" }); + const file2 = await Deno.makeTempFile({ prefix: "hello", suffix: "world" }); + // Check that both dirs are different. + assert(file1 !== file2); + for (const dir of [file1, file2]) { + // 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 dir = Deno.makeTempDirSync({ prefix: "tempdir" }); + const file3 = await Deno.makeTempFile({ dir }); + assert(file3.startsWith(dir)); + assert(/^[\\\/]/.test(file3.slice(dir.length))); + // Check that creating a temp file inside a nonexisting directory fails. + let err; + try { + await Deno.makeTempFile({ dir: "/baddir" }); + } catch (err_) { + err = err_; + } + assertEquals(err.kind, Deno.ErrorKind.NotFound); + assertEquals(err.name, "NotFound"); +}); diff --git a/cli/js/unit_tests.ts b/cli/js/unit_tests.ts index 3c808fe46..c43abaa5c 100644 --- a/cli/js/unit_tests.ts +++ b/cli/js/unit_tests.ts @@ -29,7 +29,7 @@ import "./headers_test.ts"; import "./internals_test.ts"; import "./link_test.ts"; import "./location_test.ts"; -import "./make_temp_dir_test.ts"; +import "./make_temp_test.ts"; import "./metrics_test.ts"; import "./mixins/dom_iterable_test.ts"; import "./mkdir_test.ts"; |