mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
Deno.chdir should require allow-read not allow-write (#5033)
This commit is contained in:
parent
2872b362ff
commit
bbbf9f299c
3 changed files with 31 additions and 25 deletions
2
cli/js/lib.deno.ns.d.ts
vendored
2
cli/js/lib.deno.ns.d.ts
vendored
|
@ -140,7 +140,7 @@ declare namespace Deno {
|
||||||
* Throws `Deno.errors.PermissionDenied` if the user does not have access
|
* Throws `Deno.errors.PermissionDenied` if the user does not have access
|
||||||
* rights
|
* rights
|
||||||
*
|
*
|
||||||
* Requires --allow-write.
|
* Requires --allow-read.
|
||||||
*/
|
*/
|
||||||
export function chdir(directory: string): void;
|
export function chdir(directory: string): void;
|
||||||
|
|
||||||
|
|
|
@ -5,20 +5,23 @@ unitTest(function dirCwdNotNull(): void {
|
||||||
assert(Deno.cwd() != null);
|
assert(Deno.cwd() != null);
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest({ perms: { write: true } }, function dirCwdChdirSuccess(): void {
|
unitTest(
|
||||||
const initialdir = Deno.cwd();
|
{ perms: { read: true, write: true } },
|
||||||
const path = Deno.makeTempDirSync();
|
function dirCwdChdirSuccess(): void {
|
||||||
Deno.chdir(path);
|
const initialdir = Deno.cwd();
|
||||||
const current = Deno.cwd();
|
const path = Deno.makeTempDirSync();
|
||||||
if (Deno.build.os === "darwin") {
|
Deno.chdir(path);
|
||||||
assertEquals(current, "/private" + path);
|
const current = Deno.cwd();
|
||||||
} else {
|
if (Deno.build.os === "darwin") {
|
||||||
assertEquals(current, path);
|
assertEquals(current, "/private" + path);
|
||||||
|
} else {
|
||||||
|
assertEquals(current, path);
|
||||||
|
}
|
||||||
|
Deno.chdir(initialdir);
|
||||||
}
|
}
|
||||||
Deno.chdir(initialdir);
|
);
|
||||||
});
|
|
||||||
|
|
||||||
unitTest({ perms: { write: true } }, function dirCwdError(): void {
|
unitTest({ perms: { read: true, write: true } }, function dirCwdError(): void {
|
||||||
// excluding windows since it throws resource busy, while removeSync
|
// excluding windows since it throws resource busy, while removeSync
|
||||||
if (["linux", "darwin"].includes(Deno.build.os)) {
|
if (["linux", "darwin"].includes(Deno.build.os)) {
|
||||||
const initialdir = Deno.cwd();
|
const initialdir = Deno.cwd();
|
||||||
|
@ -39,16 +42,19 @@ unitTest({ perms: { write: true } }, function dirCwdError(): void {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest({ perms: { write: true } }, function dirChdirError(): void {
|
unitTest(
|
||||||
const path = Deno.makeTempDirSync() + "test";
|
{ perms: { read: true, write: true } },
|
||||||
try {
|
function dirChdirError(): void {
|
||||||
Deno.chdir(path);
|
const path = Deno.makeTempDirSync() + "test";
|
||||||
throw Error("directory not available, should throw error");
|
try {
|
||||||
} catch (err) {
|
Deno.chdir(path);
|
||||||
if (err instanceof Deno.errors.NotFound) {
|
throw Error("directory not available, should throw error");
|
||||||
assert(err.name === "NotFound");
|
} catch (err) {
|
||||||
} else {
|
if (err instanceof Deno.errors.NotFound) {
|
||||||
throw Error("raised different exception");
|
assert(err.name === "NotFound");
|
||||||
|
} else {
|
||||||
|
throw Error("raised different exception");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|
|
@ -254,7 +254,7 @@ fn op_chdir(
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: ChdirArgs = serde_json::from_value(args)?;
|
let args: ChdirArgs = serde_json::from_value(args)?;
|
||||||
let d = PathBuf::from(&args.directory);
|
let d = PathBuf::from(&args.directory);
|
||||||
state.check_write(&d)?;
|
state.check_read(&d)?;
|
||||||
set_current_dir(&d)?;
|
set_current_dir(&d)?;
|
||||||
Ok(JsonOp::Sync(json!({})))
|
Ok(JsonOp::Sync(json!({})))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue