Skip to content

Commit

Permalink
test(fileURLToPath, pathToFileURL): add windows specific tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 21, 2024
1 parent 77ebe50 commit bfa2bbe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 17 additions & 11 deletions test/utils.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";

import { isWindows } from 'std-env'
import {
isNodeBuiltin,
sanitizeFilePath,
Expand Down Expand Up @@ -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 ? [

Check failure on line 162 in test/utils.test.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Expected newline between test and consequent of ternary expression

Check failure on line 162 in test/utils.test.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Expected newline between consequent and alternate of ternary expression
["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 ? [

Check failure on line 179 in test/utils.test.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Expected newline between test and consequent of ternary expression

Check failure on line 179 in test/utils.test.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Expected newline between consequent and alternate of ternary expression
["/foo#1", /file:\/\/\/\w:\/foo%231/ ],

Check failure on line 180 in test/utils.test.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

There should be no space before ']'
["/some/path%.c", /file:\/\/\/\w:\/some\/path%25.c/],

Check failure on line 181 in test/utils.test.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

/file:\/\/\/\w:\/some\/path%25.c/ can be optimized to /file:\/{3}\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);
});
}
});

0 comments on commit bfa2bbe

Please sign in to comment.