From da99a5bca4950a1477913fc22ca43cde41d93e60 Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Tue, 4 Oct 2011 11:27:06 -0300 Subject: path/filepath: added Rel as the complement of Abs R=golang-dev, rsc, gustavo, r, borman CC=golang-dev https://golang.org/cl/4981049 --- src/pkg/path/filepath/path_test.go | 85 +++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 6 deletions(-) (limited to 'src/pkg/path/filepath/path_test.go') diff --git a/src/pkg/path/filepath/path_test.go b/src/pkg/path/filepath/path_test.go index 850ead8e81..9234c0df6c 100644 --- a/src/pkg/path/filepath/path_test.go +++ b/src/pkg/path/filepath/path_test.go @@ -572,10 +572,11 @@ var abstests = []string{ "pkg/../../AUTHORS", "Make.pkg", "pkg/Makefile", - - // Already absolute + ".", "$GOROOT/src/Make.pkg", "$GOROOT/src/../src/Make.pkg", + "$GOROOT/misc/cgo", + "$GOROOT", } func TestAbs(t *testing.T) { @@ -589,14 +590,15 @@ func TestAbs(t *testing.T) { os.Chdir(cwd) for _, path := range abstests { path = strings.Replace(path, "$GOROOT", goroot, -1) - abspath, err := filepath.Abs(path) - if err != nil { - t.Errorf("Abs(%q) error: %v", path, err) - } info, err := os.Stat(path) if err != nil { t.Errorf("%s: %s", path, err) } + + abspath, err := filepath.Abs(path) + if err != nil { + t.Errorf("Abs(%q) error: %v", path, err) + } absinfo, err := os.Stat(abspath) if err != nil || absinfo.Ino != info.Ino { t.Errorf("Abs(%q)=%q, not the same file", path, abspath) @@ -610,6 +612,77 @@ func TestAbs(t *testing.T) { } } +type RelTests struct { + root, path, want string +} + +var reltests = []RelTests{ + {"a/b", "a/b", "."}, + {"a/b/.", "a/b", "."}, + {"a/b", "a/b/.", "."}, + {"./a/b", "a/b", "."}, + {"a/b", "./a/b", "."}, + {"ab/cd", "ab/cde", "../cde"}, + {"ab/cd", "ab/c", "../c"}, + {"a/b", "a/b/c/d", "c/d"}, + {"a/b", "a/b/../c", "../c"}, + {"a/b/../c", "a/b", "../b"}, + {"a/b/c", "a/c/d", "../../c/d"}, + {"a/b", "c/d", "../../c/d"}, + {"../../a/b", "../../a/b/c/d", "c/d"}, + {"/a/b", "/a/b", "."}, + {"/a/b/.", "/a/b", "."}, + {"/a/b", "/a/b/.", "."}, + {"/ab/cd", "/ab/cde", "../cde"}, + {"/ab/cd", "/ab/c", "../c"}, + {"/a/b", "/a/b/c/d", "c/d"}, + {"/a/b", "/a/b/../c", "../c"}, + {"/a/b/../c", "/a/b", "../b"}, + {"/a/b/c", "/a/c/d", "../../c/d"}, + {"/a/b", "/c/d", "../../c/d"}, + {"/../../a/b", "/../../a/b/c/d", "c/d"}, + {".", "a/b", "a/b"}, + {".", "..", ".."}, + + // can't do purely lexically + {"..", ".", "err"}, + {"..", "a", "err"}, + {"../..", "..", "err"}, + {"a", "/a", "err"}, + {"/a", "a", "err"}, +} + +var winreltests = []RelTests{ + {`C:a\b\c`, `C:a/b/d`, `..\d`}, + {`C:\`, `D:\`, `err`}, + {`C:`, `D:`, `err`}, +} + +func TestRel(t *testing.T) { + tests := append([]RelTests{}, reltests...) + if runtime.GOOS == "windows" { + for i := range tests { + tests[i].want = filepath.FromSlash(tests[i].want) + } + tests = append(tests, winreltests...) + } + for _, test := range tests { + got, err := filepath.Rel(test.root, test.path) + if test.want == "err" { + if err == nil { + t.Errorf("Rel(%q, %q)=%q, want error", test.root, test.path, got) + } + continue + } + if err != nil { + t.Errorf("Rel(%q, %q): want %q, got error: %s", test.root, test.path, test.want, err) + } + if got != test.want { + t.Errorf("Rel(%q, %q)=%q, want %q", test.root, test.path, got, test.want) + } + } +} + type VolumeNameTest struct { path string vol string -- cgit v1.3