From 2bba886c3f6549c92325507a634e23d285ac9fd1 Mon Sep 17 00:00:00 2001 From: ayame113 <40050810+ayame113@users.noreply.github.com> Date: Fri, 6 Aug 2021 06:10:21 +0900 Subject: [PATCH 1/4] fix(convert/pathToUri): improve encoding of path other than the file --- lib/convert.ts | 3 +++ test/convert.test.ts | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/convert.ts b/lib/convert.ts index 5c54859f..8ae224f1 100644 --- a/lib/convert.ts +++ b/lib/convert.ts @@ -17,6 +17,9 @@ export default class Convert { * @returns The Uri corresponding to the path. e.g. file:///a/b/c.txt */ public static pathToUri(filePath: string): string { + if (new URL(filePath, "file://").protocol !== "file:") { + return filePath + } let newPath = filePath.replace(/\\/g, "/") if (newPath[0] !== "/") { newPath = `/${newPath}` diff --git a/test/convert.test.ts b/test/convert.test.ts index 78ef91ee..45167c8e 100644 --- a/test/convert.test.ts +++ b/test/convert.test.ts @@ -25,6 +25,18 @@ describe("Convert", () => { }) describe("pathToUri", () => { + it("does not convert path other than file:", () => { + expect(Convert.pathToUri("http://atom.io/a")).toBe("http://atom.io/a") + expect(Convert.pathToUri("https://atom.io/b")).toBe("https://atom.io/b") + expect(Convert.pathToUri("deno:/hello.js")).toBe("deno:/hello.js") + }) + + it("does not convert non-alphanumeric path other than file:", () => { + expect(Convert.pathToUri("http://atom.io/a%40%E3%81%82")).toBe("http://atom.io/a%40%E3%81%82") + expect(Convert.pathToUri("https://atom.io/b?foo=bar")).toBe("https://atom.io/b?foo=bar") + expect(Convert.pathToUri("deno:/hello%40%E3%81%82.js")).toBe("deno:/hello%40%E3%81%82.js") + }) + it("prefixes an absolute path with file://", () => { expect(Convert.pathToUri("/a/b/c/d.txt")).toBe("file:///a/b/c/d.txt") }) @@ -47,10 +59,18 @@ describe("Convert", () => { }) describe("uriToPath", () => { - it("does not convert http: and https: uri's", () => { + it("does not convert uri other than file:", () => { setProcessPlatform("darwin") expect(Convert.uriToPath("http://atom.io/a")).toBe("http://atom.io/a") expect(Convert.uriToPath("https://atom.io/b")).toBe("https://atom.io/b") + expect(Convert.uriToPath("deno:/hello.js")).toBe("deno:/hello.js") + }) + + it("does not convert non-alphanumeric uri other than file:", () => { + setProcessPlatform("darwin") + expect(Convert.uriToPath("http://atom.io/a%40%E3%81%82")).toBe("http://atom.io/a%40%E3%81%82") + expect(Convert.uriToPath("https://atom.io/b?foo=bar")).toBe("https://atom.io/b?foo=bar") + expect(Convert.uriToPath("deno:/hello%40%E3%81%82.js")).toBe("deno:/hello%40%E3%81%82.js") }) it("converts a file:// path to an absolute path", () => { From 6d49d816347084db2076557500e18b289e42189a Mon Sep 17 00:00:00 2001 From: ayame113 <40050810+ayame113@users.noreply.github.com> Date: Fri, 6 Aug 2021 08:45:20 +0900 Subject: [PATCH 2/4] fix: path test --- test/convert.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/convert.test.ts b/test/convert.test.ts index 45167c8e..24acdeeb 100644 --- a/test/convert.test.ts +++ b/test/convert.test.ts @@ -50,6 +50,7 @@ describe("Convert", () => { }) it("does not encode Windows drive specifiers", () => { + setProcessPlatform("win32") expect(Convert.pathToUri("d:\\ee\\ff.txt")).toBe("file:///d:/ee/ff.txt") }) From 91443a276b128ceb020723be787b4e2649e50e9d Mon Sep 17 00:00:00 2001 From: ayame113 <40050810+ayame113@users.noreply.github.com> Date: Fri, 6 Aug 2021 09:05:15 +0900 Subject: [PATCH 3/4] fix: disable test for drive characters except windows --- test/convert.test.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/convert.test.ts b/test/convert.test.ts index 24acdeeb..0939c08c 100644 --- a/test/convert.test.ts +++ b/test/convert.test.ts @@ -50,8 +50,11 @@ describe("Convert", () => { }) it("does not encode Windows drive specifiers", () => { - setProcessPlatform("win32") - expect(Convert.pathToUri("d:\\ee\\ff.txt")).toBe("file:///d:/ee/ff.txt") + // This test only succeeds on windows. (Because of the difference in the processing method of drive characters) + // However, it is enough to test the windows drive character only on windows. + if (process.platform === "win32") { + expect(Convert.pathToUri("d:\\ee\\ff.txt")).toBe("file:///d:/ee/ff.txt") + } }) it("URI encodes special characters", () => { From b32cadd5cbffe85bdedf719c5d61c39e06351c71 Mon Sep 17 00:00:00 2001 From: ayame113 <40050810+ayame113@users.noreply.github.com> Date: Fri, 6 Aug 2021 11:04:50 +0900 Subject: [PATCH 4/4] fix: use pending() in windows test --- test/convert.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/convert.test.ts b/test/convert.test.ts index 0939c08c..9abacebd 100644 --- a/test/convert.test.ts +++ b/test/convert.test.ts @@ -52,9 +52,10 @@ describe("Convert", () => { it("does not encode Windows drive specifiers", () => { // This test only succeeds on windows. (Because of the difference in the processing method of drive characters) // However, it is enough to test the windows drive character only on windows. - if (process.platform === "win32") { - expect(Convert.pathToUri("d:\\ee\\ff.txt")).toBe("file:///d:/ee/ff.txt") + if (process.platform !== "win32") { + pending("Only test on windows") } + expect(Convert.pathToUri("d:\\ee\\ff.txt")).toBe("file:///d:/ee/ff.txt") }) it("URI encodes special characters", () => {