Skip to content

Commit

Permalink
fix realpath behavior in windows (denoland#3425)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhmushan authored and ry committed Dec 1, 2019
1 parent 81efa9d commit 537c6b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions cli/js/realpath_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ 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("/"));
if (Deno.build.os !== "win") {
assert(realPath.startsWith("/"));
} else {
assert(/^[A-Z]/.test(realPath));
}
assert(realPath.endsWith(incompletePath));
});

Expand Down Expand Up @@ -47,7 +51,11 @@ testPerm({ read: true }, function realpathSyncNotFound(): void {
testPerm({ read: true }, async function realpathSuccess(): Promise<void> {
const incompletePath = "cli/tests/fixture.json";
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));
});

Expand Down
6 changes: 5 additions & 1 deletion cli/ops/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@ fn op_realpath(
// corresponds to the realpath on Unix and
// CreateFile and GetFinalPathNameByHandle on Windows
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))
})
}
Expand Down

0 comments on commit 537c6b3

Please sign in to comment.