From bfa2bbe525a74fc71433bab043093ba0507b8bfe Mon Sep 17 00:00:00 2001 From: Pooya Date: Wed, 21 Feb 2024 08:06:07 -0800 Subject: [PATCH] test(fileURLToPath, pathToFileURL): add windows specific tests --- package.json | 1 + pnpm-lock.yaml | 3 +++ test/utils.test.ts | 28 +++++++++++++++++----------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 74cb6e3..542aa4b 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "import-meta-resolve": "^4.0.0", "jiti": "^1.21.0", "prettier": "^3.2.4", + "std-env": "^3.7.0", "typescript": "^5.3.3", "unbuild": "^2.0.0", "vitest": "^1.2.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dd53938..e0af60b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -43,6 +43,9 @@ devDependencies: prettier: specifier: ^3.2.4 version: 3.2.4 + std-env: + specifier: ^3.7.0 + version: 3.7.0 typescript: specifier: ^5.3.3 version: 5.3.3 diff --git a/test/utils.test.ts b/test/utils.test.ts index 3fbc6ba..2aed018 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from "vitest"; - +import { isWindows } from 'std-env' import { isNodeBuiltin, sanitizeFilePath, @@ -159,27 +159,33 @@ describe("lookupNodeModuleSubpath", () => { }); describe("fileURLToPath", () => { - const tests = [ - // ["file:///C:/path/", "C:\\path\\"], // TODO - // ["file://nas/foo.txt", "\\\\nas\\foo.txt"], // TODO + const tests = isWindows ? [ + ["file:///C:/path/", "C:/path/"], + ["file://nas/foo.txt", "//nas/foo.txt"], + ["file://C:/你好.txt", "C:/你好.txt"], + ["file://C:/hello world", "C:/hello world"], + ] as const : [ ["file:///你好.txt", "/你好.txt"], ["file:///hello world", "/hello world"], ] as const; - for (const t of tests) { - it(`${t[0]} should resolve to ${t[1]}`, () => { - expect(fileURLToPath(t[0])).toBe(t[1]); + for (const [input, output] of tests) { + it(`${input} should resolve to ${output}`, () => { + expect(fileURLToPath(input)).toBe(output); }); } }); describe("pathToFileURL", () => { - const tests = [ + const tests = isWindows ? [ + ["/foo#1", /file:\/\/\/\w:\/foo%231/ ], + ["/some/path%.c", /file:\/\/\/\w:\/some\/path%25.c/], + ] as const :[ ["/foo#1", "file:///foo%231"], ["/some/path%.c", "file:///some/path%25.c"], ] as const; - for (const t of tests) { - it(`${t[0]} should resolve to ${t[1]}`, () => { - expect(pathToFileURL(t[0])).toBe(t[1]); + for (const [input, output] of tests) { + it(`${input} should resolve to ${output}`, () => { + expect(pathToFileURL(input)).toMatch(output); }); } });