1package tui
2
3import "testing"
4
5func TestQuotePastedFilePaths(t *testing.T) {
6 cases := []struct {
7 name string
8 in string
9 want string
10 }{
11 // macOS Terminal default: backslash-escaped spaces in path.
12 {"backslash-escaped space", `/Users/pat/foo\ bar.png`, `'/Users/pat/foo bar.png'`},
13
14 // Plain absolute path with no special chars.
15 {"plain absolute", `/Users/pat/file.png`, `'/Users/pat/file.png'`},
16
17 // file:// URL form; URL-decoded and scheme stripped.
18 {"file url", `file:///Users/pat/foo%20bar.png`, `'/Users/pat/foo bar.png'`},
19
20 // Tilde paths.
21 {"tilde", `~/Pictures/x.png`, `'~/Pictures/x.png'`},
22
23 // Multi-file drop, each path quoted independently.
24 {"two paths", `/a.png /b.png`, `'/a.png' '/b.png'`},
25
26 // Already single-quoted: re-normalise to consistent quoting.
27 {"already single quoted", `'/Users/pat/x.png'`, `'/Users/pat/x.png'`},
28
29 // Already double-quoted: re-normalise.
30 {"already double quoted", `"/Users/pat/x.png"`, `'/Users/pat/x.png'`},
31
32 // Embedded apostrophe gets the standard '\'' splice escape.
33 {"embedded apostrophe", `/Users/pat/it's.png`, `'/Users/pat/it'\''s.png'`},
34
35 // Plain prose left alone.
36 {"prose", `hello world`, `hello world`},
37
38 // Multi-line paste left alone (typical code paste).
39 {"multiline", "foo\nbar", "foo\nbar"},
40
41 // Anything containing shell metacharacters is left alone so a
42 // crafted "drop" can't smuggle a command.
43 {"metachar", `/foo;rm -rf /`, `/foo;rm -rf /`},
44
45 // Mixed: valid path token quoted, surrounding words preserved.
46 {"path mixed with prose", `/x.png is good`, `'/x.png' is good`},
47 }
48 for _, c := range cases {
49 t.Run(c.name, func(t *testing.T) {
50 got := quotePastedFilePaths(c.in)
51 if got != c.want {
52 t.Errorf("input %q\n want %q\n got %q", c.in, c.want, got)
53 }
54 })
55 }
56}