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

path.test.ts

64 lines2.9 KB

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