package paths import "testing" func TestUnixJoin(t *testing.T) { cases := []struct { elems []string want string }{ {[]string{"/etc", "yet"}, "/etc/yet"}, {[]string{"/", "etc", "yet"}, "/etc/yet"}, {[]string{"relative", "path"}, "relative/path"}, {[]string{"/a/", "/b/", "c"}, "/a/b/c"}, } for _, c := range cases { if got := unixJoin(c.elems...); got != c.want { t.Errorf("unixJoin(%v) = %q, want %q", c.elems, got, c.want) } } } func TestWindowsJoin(t *testing.T) { cases := []struct { elems []string want string }{ {[]string{`C:\Users\test`, "yet"}, `C:\Users\test\yet`}, {[]string{`C:\Users\test\`, `\yet\`}, `C:\Users\test\yet`}, {[]string{`\\server\share`, "yet"}, `\\server\share\yet`}, {[]string{"relative", "path"}, `relative\path`}, } for _, c := range cases { if got := windowsJoin(c.elems...); got != c.want { t.Errorf("windowsJoin(%v) = %q, want %q", c.elems, got, c.want) } } } func TestSplitWindowsRoot(t *testing.T) { cases := []struct { path string wantRoot string wantRest string wantOK bool }{ {`C:\Users\test\yet`, `C:`, `Users\test\yet`, true}, {`C:\`, `C:`, ``, true}, {`\\server\share\yet`, `\\server\share`, `yet`, true}, {`\\server\share`, `\\server\share`, ``, true}, {`relative\path`, "", "", false}, } for _, c := range cases { root, rest, ok := splitWindowsRoot(c.path) if ok != c.wantOK || root != c.wantRoot || rest != c.wantRest { t.Errorf("splitWindowsRoot(%q) = (%q, %q, %v), want (%q, %q, %v)", c.path, root, rest, ok, c.wantRoot, c.wantRest, c.wantOK) } } } func TestApplyRootRebaseUnix(t *testing.T) { if got, want := applyRootRebase("", "/etc/yet", false), "/etc/yet"; got != want { t.Errorf("applyRootRebase(\"\", ...) = %q, want %q", got, want) } if got, want := applyRootRebase("/tmp/root", "/etc/yet", false), "/tmp/root/etc/yet"; got != want { t.Errorf("applyRootRebase = %q, want %q", got, want) } } func TestApplyRootRebaseWindows(t *testing.T) { if got, want := applyRootRebase("", `C:\ProgramData\yet`, true), `C:\ProgramData\yet`; got != want { t.Errorf("applyRootRebase(\"\", ...) = %q, want %q", got, want) } if got, want := applyRootRebase(`C:\sandbox`, `C:\ProgramData\yet`, true), `C:\sandbox\ProgramData\yet`; got != want { t.Errorf("applyRootRebase = %q, want %q", got, want) } if got, want := applyRootRebase(`C:\sandbox`, `\\server\share\yet`, true), `C:\sandbox\yet`; got != want { t.Errorf("applyRootRebase(UNC) = %q, want %q", got, want) } } // TestApplyRootRebaseMismatchedFamily covers the scenario that motivated // applyRootRebase's design: root in the REAL host's native family (e.g. // t.TempDir()'s result on a real Windows host) combined with p in a // *different*, simulated platform's family (see README's "Testing on // any host"). Before this was fixed, mismatched families produced a // mixed-separator (Unix root, Windows-native root) or doubly-drive- // lettered (Windows root, Unix p incorrectly windows-joined) result. func TestApplyRootRebaseMismatchedFamily(t *testing.T) { // Windows-native root (as a real t.TempDir() would return), Unix p // (a Linux-simulated resolver's unrooted result). if got, want := applyRootRebase(`C:\Users\test\AppData\Local\Temp\001`, "/home/test/.config/yet", false), `C:\Users\test\AppData\Local\Temp\001\home\test\.config\yet`; got != want { t.Errorf("applyRootRebase(windows root, unix p) = %q, want %q", got, want) } // Unix-native root, Windows p (a Windows-simulated resolver's // unrooted result) -- the mirror image, for symmetry. if got, want := applyRootRebase("/tmp/sandbox", `C:\Users\test\AppData\Roaming\yet`, true), "/tmp/sandbox/Users/test/AppData/Roaming/yet"; got != want { t.Errorf("applyRootRebase(unix root, windows p) = %q, want %q", got, want) } } func TestJoinDirFile(t *testing.T) { if got, want := JoinDirFile("/etc/yet", "yet.yaml"), "/etc/yet/yet.yaml"; got != want { t.Errorf("JoinDirFile(unix) = %q, want %q", got, want) } if got, want := JoinDirFile(`C:\ProgramData\yet`, "yet.yaml"), `C:\ProgramData\yet\yet.yaml`; got != want { t.Errorf("JoinDirFile(windows) = %q, want %q", got, want) } } func TestJoinDirFileFamily(t *testing.T) { // dir carries its own family: content wins regardless of platform. if got, want := JoinDirFileFamily("windows", "/etc/yet", "yet.yaml"), "/etc/yet/yet.yaml"; got != want { t.Errorf("JoinDirFileFamily(unix dir, windows platform) = %q, want %q", got, want) } if got, want := JoinDirFileFamily("linux", `C:\Users\test\workdir`, "yet.yaml"), `C:\Users\test\workdir\yet.yaml`; got != want { t.Errorf("JoinDirFileFamily(windows dir, linux platform) = %q, want %q", got, want) } // dir is ambiguous (no separator of its own, e.g. the default "."): // platform decides, same as JoinFamily. if got, want := JoinDirFileFamily("windows", ".", "yet.yaml"), `.\yet.yaml`; got != want { t.Errorf("JoinDirFileFamily(ambiguous, windows) = %q, want %q", got, want) } if got, want := JoinDirFileFamily("linux", ".", "yet.yaml"), "./yet.yaml"; got != want { t.Errorf("JoinDirFileFamily(ambiguous, linux) = %q, want %q", got, want) } }