From c226d3af2572c93af21f5a3261ede4dd8855685e Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Wed, 7 Oct 2020 13:05:43 +0100 Subject: [PATCH] fix(cli/ops/fs): Don't force Windows paths separate paths with forward slash (#7833) --- cli/ops/fs.rs | 10 ++++----- cli/tests/unit/real_path_test.ts | 37 ++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/cli/ops/fs.rs b/cli/ops/fs.rs index c5d1458fc9..8542b5230f 100644 --- a/cli/ops/fs.rs +++ b/cli/ops/fs.rs @@ -941,10 +941,9 @@ fn op_realpath_sync( // corresponds to the realpath on Unix and // CreateFile and GetFinalPathNameByHandle on Windows let realpath = std::fs::canonicalize(&path)?; - let mut realpath_str = - into_string(realpath.into_os_string())?.replace("\\", "/"); + let mut realpath_str = into_string(realpath.into_os_string())?; if cfg!(windows) { - realpath_str = realpath_str.trim_start_matches("//?/").to_string(); + realpath_str = realpath_str.trim_start_matches("\\\\?\\").to_string(); } Ok(json!(realpath_str)) } @@ -971,10 +970,9 @@ async fn op_realpath_async( // corresponds to the realpath on Unix and // CreateFile and GetFinalPathNameByHandle on Windows let realpath = std::fs::canonicalize(&path)?; - let mut realpath_str = - into_string(realpath.into_os_string())?.replace("\\", "/"); + let mut realpath_str = into_string(realpath.into_os_string())?; if cfg!(windows) { - realpath_str = realpath_str.trim_start_matches("//?/").to_string(); + realpath_str = realpath_str.trim_start_matches("\\\\?\\").to_string(); } Ok(json!(realpath_str)) }) diff --git a/cli/tests/unit/real_path_test.ts b/cli/tests/unit/real_path_test.ts index d00aed0dd9..f82929ed53 100644 --- a/cli/tests/unit/real_path_test.ts +++ b/cli/tests/unit/real_path_test.ts @@ -1,20 +1,22 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { assert, + assertMatch, assertThrows, assertThrowsAsync, unitTest, } from "./test_util.ts"; unitTest({ perms: { read: true } }, function realPathSyncSuccess(): void { - const incompletePath = "cli/tests/fixture.json"; - const realPath = Deno.realPathSync(incompletePath); + const relative = "cli/tests/fixture.json"; + const realPath = Deno.realPathSync(relative); if (Deno.build.os !== "windows") { assert(realPath.startsWith("/")); + assert(realPath.endsWith(relative)); } else { - assert(/^[A-Z]/.test(realPath)); + assertMatch(realPath, /^[A-Z]:\\/); + assert(realPath.endsWith(relative.replace(/\//g, "\\"))); } - assert(realPath.endsWith(incompletePath)); }); unitTest( @@ -27,13 +29,14 @@ unitTest( const symlink = testDir + "/symln"; Deno.mkdirSync(target); Deno.symlinkSync(target, symlink); - const targetPath = Deno.realPathSync(symlink); + const realPath = Deno.realPathSync(symlink); if (Deno.build.os !== "windows") { - assert(targetPath.startsWith("/")); + assert(realPath.startsWith("/")); + assert(realPath.endsWith("/target")); } else { - assert(/^[A-Z]/.test(targetPath)); + assertMatch(realPath, /^[A-Z]:\\/); + assert(realPath.endsWith("\\target")); } - assert(targetPath.endsWith("/target")); }, ); @@ -52,14 +55,15 @@ unitTest({ perms: { read: true } }, function realPathSyncNotFound(): void { unitTest({ perms: { read: true } }, async function realPathSuccess(): Promise< void > { - const incompletePath = "cli/tests/fixture.json"; - const realPath = await Deno.realPath(incompletePath); + const relativePath = "cli/tests/fixture.json"; + const realPath = await Deno.realPath(relativePath); if (Deno.build.os !== "windows") { assert(realPath.startsWith("/")); + assert(realPath.endsWith(relativePath)); } else { - assert(/^[A-Z]/.test(realPath)); + assertMatch(realPath, /^[A-Z]:\\/); + assert(realPath.endsWith(relativePath.replace(/\//g, "\\"))); } - assert(realPath.endsWith(incompletePath)); }); unitTest( @@ -72,13 +76,14 @@ unitTest( const symlink = testDir + "/symln"; Deno.mkdirSync(target); Deno.symlinkSync(target, symlink); - const targetPath = await Deno.realPath(symlink); + const realPath = await Deno.realPath(symlink); if (Deno.build.os !== "windows") { - assert(targetPath.startsWith("/")); + assert(realPath.startsWith("/")); + assert(realPath.endsWith("/target")); } else { - assert(/^[A-Z]/.test(targetPath)); + assertMatch(realPath, /^[A-Z]:\\/); + assert(realPath.endsWith("\\target")); } - assert(targetPath.endsWith("/target")); }, );