| 1 | package cli |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestResolveExistingPastedPathWindowsCandidates(t *testing.T) { |
| 6 | tests := []struct { |
| 7 | name string |
| 8 | input string |
| 9 | exists []string |
| 10 | want string |
| 11 | ok bool |
| 12 | }{ |
| 13 | { |
| 14 | name: "native windows path wins", |
| 15 | input: `C:\Users\me\shot.png`, |
| 16 | exists: []string{`C:\Users\me\shot.png`}, |
| 17 | want: `C:\Users\me\shot.png`, |
| 18 | ok: true, |
| 19 | }, |
| 20 | { |
| 21 | name: "git bash slash path", |
| 22 | input: `C:/Users/me/My\ Shot.png`, |
| 23 | exists: []string{`C:/Users/me/My Shot.png`}, |
| 24 | want: `C:/Users/me/My Shot.png`, |
| 25 | ok: true, |
| 26 | }, |
| 27 | { |
| 28 | name: "git bash backslash path", |
| 29 | input: `C:\Users\me\My\ Shot.png`, |
| 30 | exists: []string{`C:\Users\me\My Shot.png`}, |
| 31 | want: `C:\Users\me\My Shot.png`, |
| 32 | ok: true, |
| 33 | }, |
| 34 | { |
| 35 | name: "msys drive path", |
| 36 | input: `/c/Users/me/My\ Shot.png`, |
| 37 | exists: []string{`C:/Users/me/My Shot.png`}, |
| 38 | want: `C:/Users/me/My Shot.png`, |
| 39 | ok: true, |
| 40 | }, |
| 41 | { |
| 42 | name: "wsl mounted drive path", |
| 43 | input: `/mnt/c/Users/me/My\ Shot.png`, |
| 44 | exists: []string{`C:/Users/me/My Shot.png`}, |
| 45 | want: `C:/Users/me/My Shot.png`, |
| 46 | ok: true, |
| 47 | }, |
| 48 | { |
| 49 | name: "cygwin drive path", |
| 50 | input: `/cygdrive/c/Users/me/My\ Shot.png`, |
| 51 | exists: []string{`C:/Users/me/My Shot.png`}, |
| 52 | want: `C:/Users/me/My Shot.png`, |
| 53 | ok: true, |
| 54 | }, |
| 55 | { |
| 56 | name: "unc path", |
| 57 | input: `//server/share/My\ Shot.png`, |
| 58 | exists: []string{`//server/share/My Shot.png`}, |
| 59 | want: `//server/share/My Shot.png`, |
| 60 | ok: true, |
| 61 | }, |
| 62 | { |
| 63 | name: "unc backslash path", |
| 64 | input: `\\server\share\My\ Shot.png`, |
| 65 | exists: []string{`\\server\share\My Shot.png`}, |
| 66 | want: `\\server\share\My Shot.png`, |
| 67 | ok: true, |
| 68 | }, |
| 69 | { |
| 70 | name: "extended-length path", |
| 71 | input: `\\?\C:\Users\me\My\ Shot.png`, |
| 72 | exists: []string{`\\?\C:\Users\me\My Shot.png`}, |
| 73 | want: `\\?\C:\Users\me\My Shot.png`, |
| 74 | ok: true, |
| 75 | }, |
| 76 | { |
| 77 | name: "native interpretation precedes shell fallback", |
| 78 | input: `C:\work\(draft)\shot.png`, |
| 79 | exists: []string{ |
| 80 | `C:\work\(draft)\shot.png`, |
| 81 | `C:\work(draft)\shot.png`, |
| 82 | }, |
| 83 | want: `C:\work\(draft)\shot.png`, |
| 84 | ok: true, |
| 85 | }, |
| 86 | { |
| 87 | name: "missing candidates rejected", |
| 88 | input: `C:/Users/me/Missing\ Shot.png`, |
| 89 | }, |
| 90 | } |
| 91 | |
| 92 | for _, tt := range tests { |
| 93 | t.Run(tt.name, func(t *testing.T) { |
| 94 | existing := make(map[string]bool, len(tt.exists)) |
| 95 | for _, path := range tt.exists { |
| 96 | existing[path] = true |
| 97 | } |
| 98 | got, ok := resolveExistingPastedPath(tt.input, "windows", false, func(path string) bool { |
| 99 | return existing[path] |
| 100 | }) |
| 101 | if ok != tt.ok || got != tt.want { |
| 102 | t.Fatalf("resolveExistingPastedPath(%q) = %q, %v; want %q, %v", tt.input, got, ok, tt.want, tt.ok) |
| 103 | } |
| 104 | }) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestPastedImageSourcesUsesStaticShellFields(t *testing.T) { |
| 109 | input := `C:/Users/me/first" image".png C:/Users/me/second\ image.jpg` |
| 110 | got, ok := pastedImageSourcesForOS(input, "windows") |
| 111 | if !ok { |
| 112 | t.Fatal("pastedImageSourcesForOS rejected static shell paths") |
| 113 | } |
| 114 | want := []pastedImageSource{ |
| 115 | {value: "C:/Users/me/first image.png", shellDecoded: true}, |
| 116 | {value: "C:/Users/me/second image.jpg", shellDecoded: true}, |
| 117 | } |
| 118 | if len(got) != len(want) { |
| 119 | t.Fatalf("sources = %#v, want %#v", got, want) |
| 120 | } |
| 121 | for i := range want { |
| 122 | if got[i] != want[i] { |
| 123 | t.Fatalf("source[%d] = %#v, want %#v", i, got[i], want[i]) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 |