Open History to see the last commit
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 it.each([
12 ["report:123", "/workspace", "/workspace/report:123"],
13 ["/workspace/report:12:3", "/other", "/workspace/report:12:3"],
14 ["src/file", "C:\\Workspace\\", "C:\\Workspace\\src\\file"],
15 ["C:/Workspace/file", "/other", "C:/Workspace/file"],
16 ["file", "\\\\host\\share\\", "\\\\host\\share\\file"],
17 ["file", "/", "/file"],
18 ["~/report:123", "/workspace", "~/report:123"],
19 ])("resolves literal path %s within %s", (path, root, expected) => {
20 expect(resolveWorkspaceFilePath(path, root)).toBe(expected);
21 });
22
23 it("normalizes a bare Windows drive root the same as one with a trailing separator", () => {
24 expect(normalizeProjectPathForDispatch("C:")).toBe("C:\\");
25 expect(normalizeProjectPathForComparison("C:")).toBe("c:\\");
26 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:\\"));
27 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:/"));
28 expect(normalizeProjectPathForDispatch("C:\\repo\\")).toBe("C:\\repo");
29 });
30
31 it("names a new project's folder from any typed name", () => {
32 expect(newProjectFolderName("Pinball Stats")).toBe("pinball-stats");
33 expect(newProjectFolderName(" Café & Crème! ")).toBe("cafe-creme");
34 expect(newProjectFolderName("../../etc")).toBe("etc");
35 expect(newProjectFolderName("🎱🎱")).toBe("project");
36 expect(newProjectFolderName(`${"a".repeat(63)} b`)).toBe("a".repeat(63));
37 expect(newProjectFolderName("Con")).toBe("con-project");
38 expect(newProjectFolderName("LPT1")).toBe("lpt1-project");
39 expect(newProjectFolderName("console")).toBe("console");
40 });
41});
42
43describe("workspaceRelativeFilePath", () => {
44 it.each([
45 ["/repo/project/src/main.ts", "/repo/project", "src/main.ts"],
46 ["/repo/project/src/main.ts", "/repo/project/", "src/main.ts"],
47 ["C:\\Users\\mike\\t3code\\apps\\web\\a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
48 ["/C:/Users/mike/t3code/apps/web/a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
49 ["/Repo/Project/src/main.ts", "/repo/project", null],
50 ["/tmp/case/project/probe.txt", "/tmp/case/Project", null],
51 ["//tmp/case/project/probe.txt", "//tmp/case/Project", null],
52 ["/tmp/case/Project/probe.txt", "/tmp/case/Project", "probe.txt"],
53 ["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],
54 ["/C:/USERS/mike/t3code/main.ts", "/c:/users/MIKE/t3code", "main.ts"],
55 ["\\\\server\\share\\PROJECT\\main.ts", "\\\\Server\\Share\\Project", "main.ts"],
56 ["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
57 ["C:/Users/MIKE/main.ts", "c:/", "Users/MIKE/main.ts"],
58 ["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
59 ["/tmp/repo/file.ts ", "/tmp/repo", "file.ts "],
60 ["/tmp/report.ts", "/repo/project", null],
61 ["/repo/project-two/a.ts", "/repo/project", null],
62 ["/repo/project/a.ts", undefined, null],
63 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
64 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
65 });
66});