Skip to content

Commit

Permalink
path/filepath: test EvalSymlinks returns canonical path on windows
Browse files Browse the repository at this point in the history
When you create C:\A.TXT file on windows, you can open it as c:\a.txt.
EvalSymlinks("c:\a.txt") returns C:\A.TXT. This is all EvalSymlinks
did in the past, but recently symlinks functionality been implemented on
some Windows version (where symlinks are supported). So now EvalSymlinks
handles both: searching for file canonical name and resolving symlinks.

Unfortunately TestEvalSymlinks has not been adjusted properly. The test
tests either canonical paths or symlinks, but not both. This CL separates
canonical paths tests into new TestEvalSymlinksCanonicalNames, so all
functionality is covered. Tests are simplified somewhat too.

Also remove EvalSymlinksAbsWindowsTests - it seems not used anywhere.

Change-Id: Id12e9f1441c1e30f15c523b250469978e4511a84
Reviewed-on: https://go-review.googlesource.com/14412
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
alexbrainman committed Oct 22, 2015
1 parent 5a68eb9 commit 72193c9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
31 changes: 5 additions & 26 deletions src/path/filepath/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,6 @@ var EvalSymlinksTests = []EvalSymlinksTest{
{"test/linkabs", "/"},
}

var EvalSymlinksAbsWindowsTests = []EvalSymlinksTest{
{`c:\`, `c:\`},
}

// 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 {
Expand All @@ -767,6 +763,9 @@ func TestEvalSymlinks(t *testing.T) {
case "android", "nacl", "plan9":
t.Skipf("skipping on %s", runtime.GOOS)
}
if !supportsSymlinks {
t.Skip("skipping because symlinks are not supported")
}

tmpDir, err := ioutil.TempDir("", "evalsymlink")
if err != nil {
Expand All @@ -788,35 +787,15 @@ func TestEvalSymlinks(t *testing.T) {
if d.dest == "" {
err = os.Mkdir(path, 0755)
} else {
if supportsSymlinks {
err = os.Symlink(d.dest, path)
}
err = os.Symlink(d.dest, path)
}
if err != nil {
t.Fatal(err)
}
}

var tests []EvalSymlinksTest
if supportsSymlinks {
tests = EvalSymlinksTests
} else {
for _, d := range EvalSymlinksTests {
if d.path == d.dest {
// will test only real files and directories
tests = append(tests, d)
// test "canonical" names
d2 := EvalSymlinksTest{
path: strings.ToUpper(d.path),
dest: d.dest,
}
tests = append(tests, d2)
}
}
}

// Evaluate the symlink farm.
for _, d := range tests {
for _, d := range EvalSymlinksTests {
path := simpleJoin(tmpDir, d.path)
dest := simpleJoin(tmpDir, d.dest)
if filepath.IsAbs(d.dest) || os.IsPathSeparator(d.dest[0]) {
Expand Down
63 changes: 63 additions & 0 deletions src/path/filepath/path_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"reflect"
"strings"
"syscall"
"testing"
)
Expand Down Expand Up @@ -111,3 +112,65 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
}
}
}

// TestEvalSymlinksCanonicalNames verify that EvalSymlinks
// returns "canonical" path names on windows.
func TestEvalSymlinksCanonicalNames(t *testing.T) {
tmp, err := ioutil.TempDir("", "evalsymlinkcanonical")
if err != nil {
t.Fatal("creating temp dir:", err)
}
defer os.RemoveAll(tmp)

// ioutil.TempDir might return "non-canonical" name.
cTmpName, err := filepath.EvalSymlinks(tmp)
if err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", tmp, err)
}

dirs := []string{
"test",
"test/dir",
"testing_long_dir",
"TEST2",
}

for _, d := range dirs {
dir := filepath.Join(cTmpName, d)
err := os.Mkdir(dir, 0755)
if err != nil {
t.Fatal(err)
}
cname, err := filepath.EvalSymlinks(dir)
if err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", dir, err)
continue
}
if dir != cname {
t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", dir, cname, dir)
continue
}
// test non-canonical names
test := strings.ToUpper(dir)
p, err := filepath.EvalSymlinks(test)
if err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", test, err)
continue
}
if p != cname {
t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname)
continue
}
// another test
test = strings.ToLower(dir)
p, err = filepath.EvalSymlinks(test)
if err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", test, err)
continue
}
if p != cname {
t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname)
continue
}
}
}

0 comments on commit 72193c9

Please sign in to comment.