aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/path/filepath/path_test.go
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2011-10-04 11:27:06 -0300
committerGustavo Niemeyer <gustavo@niemeyer.net>2011-10-04 11:27:06 -0300
commitda99a5bca4950a1477913fc22ca43cde41d93e60 (patch)
treed5713a62bd00cf3f76fd8d1d2d1e7e989f5ce010 /src/pkg/path/filepath/path_test.go
parentaec89a6db957df460fa288843cf835adde8ceefd (diff)
downloadgo-da99a5bca4950a1477913fc22ca43cde41d93e60.tar.xz
path/filepath: added Rel as the complement of Abs
R=golang-dev, rsc, gustavo, r, borman CC=golang-dev https://golang.org/cl/4981049
Diffstat (limited to 'src/pkg/path/filepath/path_test.go')
-rw-r--r--src/pkg/path/filepath/path_test.go85
1 files changed, 79 insertions, 6 deletions
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