Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { quoteGitPatchPath, unquoteGitPatchPath } from "./gitPatchPath.ts";
4
5const BELL = "\u0007";
6const UNIT_SEPARATOR = "\u001f";
7const DELETE = "\u007f";
8
9describe("quoteGitPatchPath", () => {
10 it("leaves a name a header can carry as itself", () => {
11 expect(quoteGitPatchPath("src/app.ts")).toBe("src/app.ts");
12 expect(quoteGitPatchPath("with space.txt")).toBe("with space.txt");
13 expect(quoteGitPatchPath("café/résumé.ts")).toBe("café/résumé.ts");
14 expect(quoteGitPatchPath("🚀.ts")).toBe("🚀.ts");
15 });
16
17 it("writes git's escape for a character the header would read as its own", () => {
18 expect(quoteGitPatchPath("tab\tfile.txt")).toBe('"tab\\tfile.txt"');
19 expect(quoteGitPatchPath("line\nfile.txt")).toBe('"line\\nfile.txt"');
20 expect(quoteGitPatchPath('quo"te.txt')).toBe('"quo\\"te.txt"');
21 expect(quoteGitPatchPath("back\\slash.txt")).toBe('"back\\\\slash.txt"');
22 expect(quoteGitPatchPath(`ring${BELL}.txt`)).toBe('"ring\\a.txt"');
23 });
24
25 it("writes the octal of a control character git has no name for", () => {
26 expect(quoteGitPatchPath(`unit${UNIT_SEPARATOR}.txt`)).toBe('"unit\\037.txt"');
27 expect(quoteGitPatchPath(`del${DELETE}.txt`)).toBe('"del\\177.txt"');
28 });
29});
30
31describe("a name written into a header and read back out", () => {
32 const names = [
33 "src/app.ts",
34 "with space.txt",
35 "café/résumé.ts",
36 "🚀.ts",
37 "tab\tfile.txt",
38 "line\nfile.txt",
39 "carriage\rfile.txt",
40 'quo"te.txt',
41 "back\\slash.txt",
42 `ring${BELL}.txt`,
43 `unit${UNIT_SEPARATOR}.txt`,
44 `del${DELETE}.txt`,
45 `every\t\n\r"\\${BELL}.txt`,
46 ];
47
48 it.each(names)("is the name that went in: %j", (name) => {
49 const written = quoteGitPatchPath(name);
50 expect(unquoteGitPatchPath(written)).toBe(name);
51 const unwrapped = written.startsWith('"') ? written.slice(1, -1) : written;
52 expect(unquoteGitPatchPath(unwrapped)).toBe(name);
53 });
54
55 it("carries the whole name past the first thing a header stops at", () => {
56 const written = quoteGitPatchPath("tab\tfile.txt");
57
58 expect(written).not.toContain("\t");
59 expect(written).not.toContain("\n");
60 expect(unquoteGitPatchPath(written)).not.toBe("tab");
61 });
62});
63
64describe("unquoteGitPatchPath", () => {
65 it("leaves a token git saw no need to quote alone", () => {
66 expect(unquoteGitPatchPath("src/app.ts")).toBe("src/app.ts");
67 expect(unquoteGitPatchPath("with space.txt")).toBe("with space.txt");
68 expect(unquoteGitPatchPath("")).toBe("");
69 expect(unquoteGitPatchPath('"')).toBe('"');
70 });
71
72 it("reads the octal escapes a host with core.quotePath on writes", () => {
73 expect(unquoteGitPatchPath('"caf\\303\\251/r\\303\\251sum\\303\\251.ts"')).toBe(
74 "café/résumé.ts",
75 );
76 expect(unquoteGitPatchPath('"\\360\\237\\232\\200.ts"')).toBe("🚀.ts");
77 });
78});
79
80describe("a name a parser handed back with its quotes already off", () => {
81 it("is read for the escapes it still carries", () => {
82 expect(unquoteGitPatchPath("tab\\tfile.txt")).toBe("tab\tfile.txt");
83 expect(unquoteGitPatchPath("caf\\303\\251.ts")).toBe("café.ts");
84 });
85
86 it("reads an escape git would never write the way C reads it", () => {
87 expect(unquoteGitPatchPath("odd\\zname.txt")).toBe("oddzname.txt");
88 expect(unquoteGitPatchPath("trailing\\")).toBe("trailing\\");
89 expect(unquoteGitPatchPath("short\\12.txt")).toBe("short12.txt");
90 });
91});