Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

path.test.ts

53 lines2.5 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 workspaceRelativeFilePath,
4 newProjectFolderName,
5 normalizeProjectPathForComparison,
6 normalizeProjectPathForDispatch,
7 resolveWorkspaceFilePath,
8} from "./path.ts";
9​
10describe("path helpers", () => {
11 // regression: #17923 literal colons in names are kept, not parsed as positions
12 it.each([
13 ["report:123", "/workspace", "/workspace/report:123"],
14 ["src/file", "C:\\Workspace\\", "C:\\Workspace\\src\\file"],
15 ["~/report:123", "/workspace", "~/report:123"],
16 ])("resolves literal path %s within %s", (path, root, expected) => {
17 expect(resolveWorkspaceFilePath(path, root)).toBe(expected);
18 });
19​
20 it("normalizes a bare Windows drive root the same as one with a trailing separator", () => {
21 expect(normalizeProjectPathForDispatch("C:")).toBe("C:\\");
22 expect(normalizeProjectPathForComparison("C:")).toBe("c:\\");
23 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:\\"));
24 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:/"));
25 expect(normalizeProjectPathForDispatch("C:\\repo\\")).toBe("C:\\repo");
26 });
27​
28 it("names a new project's folder from any typed name", () => {
29 expect(newProjectFolderName("Pinball Stats")).toBe("pinball-stats");
30 expect(newProjectFolderName(" Café & Crème! ")).toBe("cafe-creme");
31 expect(newProjectFolderName("../../etc")).toBe("etc");
32 expect(newProjectFolderName("🎱🎱")).toBe("project");
33 expect(newProjectFolderName(`${"a".repeat(63)} b`)).toBe("a".repeat(63));
34 expect(newProjectFolderName("Con")).toBe("con-project");
35 expect(newProjectFolderName("LPT1")).toBe("lpt1-project");
36 expect(newProjectFolderName("console")).toBe("console");
37 });
38});
39​
40describe("workspaceRelativeFilePath", () => {
41 // regression: #9309 POSIX paths compare case-sensitively, Windows paths do not
42 it.each([
43 ["/Repo/Project/src/main.ts", "/repo/project", null],
44 ["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],
45 ["/repo/project-two/a.ts", "/repo/project", null],
46 // regression: #16354 a `..` that leaves the root is a host path, not a workspace path
47 ["/repo/project/../other/notes.md", "/repo/project", null],
48 ["C:\\repo\\project\\src\\..\\..\\notes.md", "C:\\repo\\project", null],
49 ["/repo/project/docs/a/../b.md", "/repo/project", "docs/a/../b.md"],
50 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
51 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
52 });
53});