1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:34:45 -07:00

path/filepath: test EvalSymlinks returns canonical path on windows

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 <iant@golang.org>
This commit is contained in:
Alex Brainman 2015-09-09 10:54:25 +10:00
parent 5a68eb9f25
commit 72193c9824
2 changed files with 68 additions and 26 deletions

View File

@ -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 {
@ -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 {
@ -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]) {

View File

@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"reflect"
"strings"
"syscall"
"testing"
)
@ -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
}
}
}