mirror of
https://github.com/denoland/deno.git
synced 2025-08-03 10:33:54 +00:00
Add Deno.realpath (#3404)
This commit is contained in:
parent
658ec2aaf9
commit
f88dc4e197
7 changed files with 157 additions and 0 deletions
|
@ -56,6 +56,7 @@ export { chownSync, chown } from "./chown.ts";
|
|||
export { utimeSync, utime } from "./utime.ts";
|
||||
export { removeSync, remove, RemoveOption } from "./remove.ts";
|
||||
export { renameSync, rename } from "./rename.ts";
|
||||
export { realpathSync, realpath } from "./realpath.ts";
|
||||
export { readFileSync, readFile } from "./read_file.ts";
|
||||
export { readDirSync, readDir } from "./read_dir.ts";
|
||||
export { copyFileSync, copyFile } from "./copy_file.ts";
|
||||
|
|
|
@ -55,6 +55,7 @@ export let OP_CHOWN: number;
|
|||
export let OP_REMOVE: number;
|
||||
export let OP_COPY_FILE: number;
|
||||
export let OP_STAT: number;
|
||||
export let OP_REALPATH: number;
|
||||
export let OP_READ_DIR: number;
|
||||
export let OP_RENAME: number;
|
||||
export let OP_LINK: number;
|
||||
|
@ -97,6 +98,7 @@ export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
|
|||
case OP_REMOVE:
|
||||
case OP_COPY_FILE:
|
||||
case OP_STAT:
|
||||
case OP_REALPATH:
|
||||
case OP_READ_DIR:
|
||||
case OP_RENAME:
|
||||
case OP_LINK:
|
||||
|
|
15
cli/js/lib.deno_runtime.d.ts
vendored
15
cli/js/lib.deno_runtime.d.ts
vendored
|
@ -605,6 +605,21 @@ declare namespace Deno {
|
|||
isSymlink(): boolean;
|
||||
}
|
||||
|
||||
// @url js/realpath.d.ts
|
||||
|
||||
/** Returns absolute normalized path with symbolic links resolved
|
||||
* synchronously.
|
||||
*
|
||||
* const realPath = Deno.realpathSync("./some/path");
|
||||
*/
|
||||
export function realpathSync(path: string): string;
|
||||
|
||||
/** Returns absolute normalized path with symbolic links resolved.
|
||||
*
|
||||
* const realPath = await Deno.realpath("./some/path");
|
||||
*/
|
||||
export function realpath(path: string): Promise<string>;
|
||||
|
||||
// @url js/read_dir.d.ts
|
||||
|
||||
/** Reads the directory given by path and returns a list of file info
|
||||
|
|
19
cli/js/realpath.ts
Normal file
19
cli/js/realpath.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
import { sendSync, sendAsync } from "./dispatch_json.ts";
|
||||
import * as dispatch from "./dispatch.ts";
|
||||
|
||||
/** Returns absolute normalized path with symbolic links resolved synchronously.
|
||||
*
|
||||
* const realPath = Deno.realpathSync("./some/path");
|
||||
*/
|
||||
export function realpathSync(path: string): string {
|
||||
return sendSync(dispatch.OP_REALPATH, { path });
|
||||
}
|
||||
|
||||
/** Returns absolute normalized path with symbolic links resolved.
|
||||
*
|
||||
* const realPath = await Deno.realpath("./some/path");
|
||||
*/
|
||||
export async function realpath(path: string): Promise<string> {
|
||||
return await sendAsync(dispatch.OP_REALPATH, { path });
|
||||
}
|
91
cli/js/realpath_test.ts
Normal file
91
cli/js/realpath_test.ts
Normal file
|
@ -0,0 +1,91 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
import { testPerm, assert, assertEquals } from "./test_util.ts";
|
||||
|
||||
testPerm({ read: true }, function realpathSyncSuccess(): void {
|
||||
const incompletePath = "cli/tests/fixture.json";
|
||||
const realPath = Deno.realpathSync(incompletePath);
|
||||
assert(realPath.startsWith("/"));
|
||||
assert(realPath.endsWith(incompletePath));
|
||||
});
|
||||
|
||||
if (Deno.build.os !== "win") {
|
||||
testPerm({ read: true, write: true }, function realpathSyncSymlink(): void {
|
||||
const testDir = Deno.makeTempDirSync();
|
||||
const target = testDir + "/target";
|
||||
const symlink = testDir + "/symln";
|
||||
Deno.mkdirSync(target);
|
||||
Deno.symlinkSync(target, symlink);
|
||||
const targetPath = Deno.realpathSync(symlink);
|
||||
assert(targetPath.startsWith("/"));
|
||||
assert(targetPath.endsWith("/target"));
|
||||
});
|
||||
}
|
||||
|
||||
testPerm({ read: false }, function realpathSyncPerm(): void {
|
||||
let caughtError = false;
|
||||
try {
|
||||
Deno.realpathSync("some_file");
|
||||
} catch (e) {
|
||||
caughtError = true;
|
||||
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
|
||||
assertEquals(e.name, "PermissionDenied");
|
||||
}
|
||||
assert(caughtError);
|
||||
});
|
||||
|
||||
testPerm({ read: true }, function realpathSyncNotFound(): void {
|
||||
let caughtError = false;
|
||||
try {
|
||||
Deno.realpathSync("bad_filename");
|
||||
} catch (e) {
|
||||
caughtError = true;
|
||||
assertEquals(e.kind, Deno.ErrorKind.NotFound);
|
||||
}
|
||||
assert(caughtError);
|
||||
});
|
||||
|
||||
testPerm({ read: true }, async function realpathSuccess(): Promise<void> {
|
||||
const incompletePath = "cli/tests/fixture.json";
|
||||
const realPath = await Deno.realpath(incompletePath);
|
||||
assert(realPath.startsWith("/"));
|
||||
assert(realPath.endsWith(incompletePath));
|
||||
});
|
||||
|
||||
if (Deno.build.os !== "win") {
|
||||
testPerm(
|
||||
{ read: true, write: true },
|
||||
async function realpathSymlink(): Promise<void> {
|
||||
const testDir = Deno.makeTempDirSync();
|
||||
const target = testDir + "/target";
|
||||
const symlink = testDir + "/symln";
|
||||
Deno.mkdirSync(target);
|
||||
Deno.symlinkSync(target, symlink);
|
||||
const targetPath = await Deno.realpath(symlink);
|
||||
assert(targetPath.startsWith("/"));
|
||||
assert(targetPath.endsWith("/target"));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
testPerm({ read: false }, async function realpathPerm(): Promise<void> {
|
||||
let caughtError = false;
|
||||
try {
|
||||
await Deno.realpath("some_file");
|
||||
} catch (e) {
|
||||
caughtError = true;
|
||||
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
|
||||
assertEquals(e.name, "PermissionDenied");
|
||||
}
|
||||
assert(caughtError);
|
||||
});
|
||||
|
||||
testPerm({ read: true }, async function realpathNotFound(): Promise<void> {
|
||||
let caughtError = false;
|
||||
try {
|
||||
await Deno.realpath("bad_filename");
|
||||
} catch (e) {
|
||||
caughtError = true;
|
||||
assertEquals(e.kind, Deno.ErrorKind.NotFound);
|
||||
}
|
||||
assert(caughtError);
|
||||
});
|
|
@ -33,6 +33,7 @@ import "./mkdir_test.ts";
|
|||
import "./net_test.ts";
|
||||
import "./os_test.ts";
|
||||
import "./process_test.ts";
|
||||
import "./realpath_test.ts";
|
||||
import "./read_dir_test.ts";
|
||||
import "./read_file_test.ts";
|
||||
import "./read_link_test.ts";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue