| 1 | package paths |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/sylvinus/agent-vm/internal/bashref" |
| 8 | ) |
| 9 | |
| 10 | |
| 11 | func simulateWindows(t *testing.T, on bool) { |
| 12 | old := Windows |
| 13 | Windows = func() bool { return on } |
| 14 | t.Cleanup(func() { Windows = old }) |
| 15 | } |
| 16 | |
| 17 | |
| 18 | func TestWindowsPaths(t *testing.T) { |
| 19 | simulateWindows(t, true) |
| 20 | for _, c := range [][3]string{ |
| 21 | {"C:/Users/me", "proj", "C:/Users/me/proj"}, |
| 22 | {"C:/Users/me", "../../../x", "C:/x"}, |
| 23 | {"C:/", "..", "C:/"}, |
| 24 | {"C:/Users", "D:/data", "D:/data"}, |
| 25 | {"C:/Users", "/x", "/x"}, |
| 26 | {"c:/a/", "./b/../c", "c:/a/c"}, |
| 27 | } { |
| 28 | if got := Abs(c[0], c[1]); got != c[2] { |
| 29 | t.Errorf("Abs(%q, %q) = %q, want %q", c[0], c[1], got, c[2]) |
| 30 | } |
| 31 | } |
| 32 | for p, want := range map[string]bool{"C:/x": true, "c:/": true, "/x": true, "C:x": false, "x": false, "1:/x": false} { |
| 33 | if IsAbs(p) != want { |
| 34 | t.Errorf("IsAbs(%q) != %v", p, want) |
| 35 | } |
| 36 | } |
| 37 | for p, want := range map[string]string{`C:\Users\me`: "/c/Users/me", "D:/x/y": "/d/x/y", "C:/": "/c/", "/tmp": "/tmp"} { |
| 38 | if got := Guest(p); got != want { |
| 39 | t.Errorf("Guest(%q) = %q, want %q", p, got, want) |
| 40 | } |
| 41 | } |
| 42 | for p, want := range map[string]string{"/c/Users/me": "C:/Users/me", "/d": "D:/", "/c/a/../b": "C:/b", "/cd/x": "/cd/x", "/1/x": "/1/x", "C:/x": "C:/x", "rel/x": "rel/x"} { |
| 43 | if got := FromGitBash(p); got != want { |
| 44 | t.Errorf("FromGitBash(%q) = %q, want %q", p, got, want) |
| 45 | } |
| 46 | } |
| 47 | if Host(`C:\Users\me\proj`) != "C:/Users/me/proj" || Root("C:/Users") != "C:/" || Root("/x") != "/" { |
| 48 | t.Error("Host or Root") |
| 49 | } |
| 50 | if rel, ok := In("C:/Users/me/proj/src", "c:/users/me/proj"); !ok || rel != "src" { |
| 51 | t.Errorf("In, case ignored: %q %v", rel, ok) |
| 52 | } |
| 53 | |
| 54 | simulateWindows(t, false) |
| 55 | if Guest("C:/x") != "C:/x" || FromGitBash("/c/x") != "/c/x" || IsAbs("C:/x") || Host(`a\b`) != `a\b` || Join("C:", "x") != "C:/x" { |
| 56 | t.Error("not Windows") |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestJoinBash(t *testing.T) { |
| 61 | cases := [][2]string{ |
| 62 | {"/a/b", "c"}, {"/a/b/", "./c/"}, {"/a/b", "../c"}, {"/a/b", "../../.."}, {"/a/b", "../../../x"}, |
| 63 | {"/", "x"}, {"/", ".."}, {"", "x"}, {"/a", "b//c/./d/../e"}, {"/a/b", ""}, {"C:", "Users/x/../y"}, |
| 64 | {"/a", "../"}, {"/a/b", "c/.."}, |
| 65 | } |
| 66 | for _, c := range cases { |
| 67 | r := bashref.Run(t, "", nil, "_agent_vm_path_join", c[0], c[1]) |
| 68 | if want := strings.TrimSuffix(r.Stdout, "\n"); Join(c[0], c[1]) != want { |
| 69 | t.Errorf("Join(%q, %q) = %q, bash %q", c[0], c[1], Join(c[0], c[1]), want) |
| 70 | } |
| 71 | } |
| 72 | } |