diff options
| author | Alex Brainman <alex.brainman@gmail.com> | 2015-09-21 16:12:24 +1000 |
|---|---|---|
| committer | Alex Brainman <alex.brainman@gmail.com> | 2015-11-01 04:45:37 +0000 |
| commit | 3d5163cf4348b5e697dcbf09897a62893876ac3c (patch) | |
| tree | 4bfea22b4c92f0556928d2c450a51354d0a81fe9 /src/path/filepath/path_test.go | |
| parent | 914db9f060b1fd3eb1f74d48f3bd46a73d4ae9c7 (diff) | |
| download | go-3d5163cf4348b5e697dcbf09897a62893876ac3c.tar.xz | |
path/filepath: fix EvalSymlinks(".") on windows
Also new tests added. So, perhaps, this CL corrects
even more broken EvalSymlinks behaviour.
Fixes #12451
Change-Id: I81b9d92bab74bcb8eca6db6633546982fe5cec87
Reviewed-on: https://go-review.googlesource.com/16192
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/path/filepath/path_test.go')
| -rw-r--r-- | src/path/filepath/path_test.go | 85 |
1 files changed, 83 insertions, 2 deletions
diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go index b0c37b0f4c..09e7be228a 100644 --- a/src/path/filepath/path_test.go +++ b/src/path/filepath/path_test.go @@ -752,6 +752,18 @@ var EvalSymlinksTests = []EvalSymlinksTest{ {"test/linkabs", "/"}, } +// findEvalSymlinksTestDirsDest searches testDirs +// for matching path and returns correspondent dest. +func findEvalSymlinksTestDirsDest(t *testing.T, testDirs []EvalSymlinksTest, path string) string { + for _, d := range testDirs { + if d.path == path { + return d.dest + } + } + t.Fatalf("did not find %q in testDirs slice", path) + return "" +} + // simpleJoin builds a file name from the directory and path. // It does not use Join because we don't want ".." to be evaluated. func simpleJoin(dir, path string) string { @@ -780,8 +792,22 @@ func TestEvalSymlinks(t *testing.T) { t.Fatal("eval symlink for tmp dir:", err) } + tests := EvalSymlinksTests + testdirs := EvalSymlinksTestDirs + if runtime.GOOS == "windows" { + if len(tmpDir) < 3 { + t.Fatalf("tmpDir path %q is too short", tmpDir) + } + if tmpDir[1] != ':' { + t.Fatalf("tmpDir path %q must have drive letter in it", tmpDir) + } + newtest := EvalSymlinksTest{"test/linkabswin", tmpDir[:3]} + tests = append(tests, newtest) + testdirs = append(testdirs, newtest) + } + // Create the symlink farm using relative paths. - for _, d := range EvalSymlinksTestDirs { + for _, d := range testdirs { var err error path := simpleJoin(tmpDir, d.path) if d.dest == "" { @@ -794,8 +820,13 @@ func TestEvalSymlinks(t *testing.T) { } } + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + // Evaluate the symlink farm. - for _, d := range EvalSymlinksTests { + for _, d := range tests { path := simpleJoin(tmpDir, d.path) dest := simpleJoin(tmpDir, d.dest) if filepath.IsAbs(d.dest) || os.IsPathSeparator(d.dest[0]) { @@ -806,6 +837,56 @@ func TestEvalSymlinks(t *testing.T) { } else if filepath.Clean(p) != filepath.Clean(dest) { t.Errorf("Clean(%q)=%q, want %q", path, p, dest) } + + // test EvalSymlinks(".") + func() { + defer func() { + err := os.Chdir(wd) + if err != nil { + t.Fatal(err) + } + }() + + err := os.Chdir(path) + if err != nil { + t.Error(err) + return + } + p, err := filepath.EvalSymlinks(".") + if err != nil { + t.Errorf(`EvalSymlinks(".") in %q directory error: %v`, d.path, err) + return + } + if p == "." { + return + } + want := filepath.Clean(findEvalSymlinksTestDirsDest(t, testdirs, d.path)) + if p == want { + return + } + t.Errorf(`EvalSymlinks(".") in %q directory returns %q, want "." or %q`, d.path, p, want) + }() + + // test EvalSymlinks where parameter is relative path + func() { + defer func() { + err := os.Chdir(wd) + if err != nil { + t.Fatal(err) + } + }() + + err := os.Chdir(tmpDir) + if err != nil { + t.Error(err) + return + } + if p, err := filepath.EvalSymlinks(d.path); err != nil { + t.Errorf("EvalSymlinks(%q) error: %v", d.path, err) + } else if filepath.Clean(p) != filepath.Clean(d.dest) { + t.Errorf("Clean(%q)=%q, want %q", d.path, p, d.dest) + } + }() } } |
