1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-12 00:54:02 -05:00

fix realpath behavior in windows (#3425)

This commit is contained in:
木杉 2019-12-02 03:23:35 +08:00 committed by Ry Dahl
parent 81efa9d938
commit 537c6b3ed9
2 changed files with 15 additions and 3 deletions

View file

@ -4,7 +4,11 @@ import { testPerm, assert, assertEquals } from "./test_util.ts";
testPerm({ read: true }, function realpathSyncSuccess(): void { testPerm({ read: true }, function realpathSyncSuccess(): void {
const incompletePath = "cli/tests/fixture.json"; const incompletePath = "cli/tests/fixture.json";
const realPath = Deno.realpathSync(incompletePath); const realPath = Deno.realpathSync(incompletePath);
assert(realPath.startsWith("/")); if (Deno.build.os !== "win") {
assert(realPath.startsWith("/"));
} else {
assert(/^[A-Z]/.test(realPath));
}
assert(realPath.endsWith(incompletePath)); assert(realPath.endsWith(incompletePath));
}); });
@ -47,7 +51,11 @@ testPerm({ read: true }, function realpathSyncNotFound(): void {
testPerm({ read: true }, async function realpathSuccess(): Promise<void> { testPerm({ read: true }, async function realpathSuccess(): Promise<void> {
const incompletePath = "cli/tests/fixture.json"; const incompletePath = "cli/tests/fixture.json";
const realPath = await Deno.realpath(incompletePath); const realPath = await Deno.realpath(incompletePath);
assert(realPath.startsWith("/")); if (Deno.build.os !== "win") {
assert(realPath.startsWith("/"));
} else {
assert(/^[A-Z]/.test(realPath));
}
assert(realPath.endsWith(incompletePath)); assert(realPath.endsWith(incompletePath));
}); });

View file

@ -300,7 +300,11 @@ fn op_realpath(
// corresponds to the realpath on Unix and // corresponds to the realpath on Unix and
// CreateFile and GetFinalPathNameByHandle on Windows // CreateFile and GetFinalPathNameByHandle on Windows
let realpath = fs::canonicalize(&path)?; let realpath = fs::canonicalize(&path)?;
let realpath_str = realpath.to_str().unwrap().to_owned().replace("\\", "/"); let mut realpath_str =
realpath.to_str().unwrap().to_owned().replace("\\", "/");
if cfg!(windows) {
realpath_str = realpath_str.trim_start_matches("//?/").to_string();
}
Ok(json!(realpath_str)) Ok(json!(realpath_str))
}) })
} }