Skip to content

Commit bfa2bbe

Browse files
committedFeb 21, 2024·
test(fileURLToPath, pathToFileURL): add windows specific tests
1 parent 77ebe50 commit bfa2bbe

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"import-meta-resolve": "^4.0.0",
4242
"jiti": "^1.21.0",
4343
"prettier": "^3.2.4",
44+
"std-env": "^3.7.0",
4445
"typescript": "^5.3.3",
4546
"unbuild": "^2.0.0",
4647
"vitest": "^1.2.2"

‎pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/utils.test.ts

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
2+
import { isWindows } from 'std-env'
33
import {
44
isNodeBuiltin,
55
sanitizeFilePath,
@@ -159,27 +159,33 @@ describe("lookupNodeModuleSubpath", () => {
159159
});
160160

161161
describe("fileURLToPath", () => {
162-
const tests = [
163-
// ["file:///C:/path/", "C:\\path\\"], // TODO
164-
// ["file://nas/foo.txt", "\\\\nas\\foo.txt"], // TODO
162+
const tests = isWindows ? [
163+
["file:///C:/path/", "C:/path/"],
164+
["file://nas/foo.txt", "//nas/foo.txt"],
165+
["file://C:/你好.txt", "C:/你好.txt"],
166+
["file://C:/hello world", "C:/hello world"],
167+
] as const : [
165168
["file:///你好.txt", "/你好.txt"],
166169
["file:///hello world", "/hello world"],
167170
] as const;
168-
for (const t of tests) {
169-
it(`${t[0]} should resolve to ${t[1]}`, () => {
170-
expect(fileURLToPath(t[0])).toBe(t[1]);
171+
for (const [input, output] of tests) {
172+
it(`${input} should resolve to ${output}`, () => {
173+
expect(fileURLToPath(input)).toBe(output);
171174
});
172175
}
173176
});
174177

175178
describe("pathToFileURL", () => {
176-
const tests = [
179+
const tests = isWindows ? [
180+
["/foo#1", /file:\/\/\/\w:\/foo%231/ ],
181+
["/some/path%.c", /file:\/\/\/\w:\/some\/path%25.c/],
182+
] as const :[
177183
["/foo#1", "file:///foo%231"],
178184
["/some/path%.c", "file:///some/path%25.c"],
179185
] as const;
180-
for (const t of tests) {
181-
it(`${t[0]} should resolve to ${t[1]}`, () => {
182-
expect(pathToFileURL(t[0])).toBe(t[1]);
186+
for (const [input, output] of tests) {
187+
it(`${input} should resolve to ${output}`, () => {
188+
expect(pathToFileURL(input)).toMatch(output);
183189
});
184190
}
185191
});

0 commit comments

Comments
 (0)
Please sign in to comment.