Skip to content

Commit 72193c9

Browse files
committed
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 <[email protected]>
1 parent 5a68eb9 commit 72193c9

File tree

2 files changed

+68
-26
lines changed

2 files changed

+68
-26
lines changed

src/path/filepath/path_test.go

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,6 @@ var EvalSymlinksTests = []EvalSymlinksTest{
752752
{"test/linkabs", "/"},
753753
}
754754

755-
var EvalSymlinksAbsWindowsTests = []EvalSymlinksTest{
756-
{`c:\`, `c:\`},
757-
}
758-
759755
// simpleJoin builds a file name from the directory and path.
760756
// It does not use Join because we don't want ".." to be evaluated.
761757
func simpleJoin(dir, path string) string {
@@ -767,6 +763,9 @@ func TestEvalSymlinks(t *testing.T) {
767763
case "android", "nacl", "plan9":
768764
t.Skipf("skipping on %s", runtime.GOOS)
769765
}
766+
if !supportsSymlinks {
767+
t.Skip("skipping because symlinks are not supported")
768+
}
770769

771770
tmpDir, err := ioutil.TempDir("", "evalsymlink")
772771
if err != nil {
@@ -788,35 +787,15 @@ func TestEvalSymlinks(t *testing.T) {
788787
if d.dest == "" {
789788
err = os.Mkdir(path, 0755)
790789
} else {
791-
if supportsSymlinks {
792-
err = os.Symlink(d.dest, path)
793-
}
790+
err = os.Symlink(d.dest, path)
794791
}
795792
if err != nil {
796793
t.Fatal(err)
797794
}
798795
}
799796

800-
var tests []EvalSymlinksTest
801-
if supportsSymlinks {
802-
tests = EvalSymlinksTests
803-
} else {
804-
for _, d := range EvalSymlinksTests {
805-
if d.path == d.dest {
806-
// will test only real files and directories
807-
tests = append(tests, d)
808-
// test "canonical" names
809-
d2 := EvalSymlinksTest{
810-
path: strings.ToUpper(d.path),
811-
dest: d.dest,
812-
}
813-
tests = append(tests, d2)
814-
}
815-
}
816-
}
817-
818797
// Evaluate the symlink farm.
819-
for _, d := range tests {
798+
for _, d := range EvalSymlinksTests {
820799
path := simpleJoin(tmpDir, d.path)
821800
dest := simpleJoin(tmpDir, d.dest)
822801
if filepath.IsAbs(d.dest) || os.IsPathSeparator(d.dest[0]) {

src/path/filepath/path_windows_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"reflect"
13+
"strings"
1314
"syscall"
1415
"testing"
1516
)
@@ -111,3 +112,65 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
111112
}
112113
}
113114
}
115+
116+
// TestEvalSymlinksCanonicalNames verify that EvalSymlinks
117+
// returns "canonical" path names on windows.
118+
func TestEvalSymlinksCanonicalNames(t *testing.T) {
119+
tmp, err := ioutil.TempDir("", "evalsymlinkcanonical")
120+
if err != nil {
121+
t.Fatal("creating temp dir:", err)
122+
}
123+
defer os.RemoveAll(tmp)
124+
125+
// ioutil.TempDir might return "non-canonical" name.
126+
cTmpName, err := filepath.EvalSymlinks(tmp)
127+
if err != nil {
128+
t.Errorf("EvalSymlinks(%q) error: %v", tmp, err)
129+
}
130+
131+
dirs := []string{
132+
"test",
133+
"test/dir",
134+
"testing_long_dir",
135+
"TEST2",
136+
}
137+
138+
for _, d := range dirs {
139+
dir := filepath.Join(cTmpName, d)
140+
err := os.Mkdir(dir, 0755)
141+
if err != nil {
142+
t.Fatal(err)
143+
}
144+
cname, err := filepath.EvalSymlinks(dir)
145+
if err != nil {
146+
t.Errorf("EvalSymlinks(%q) error: %v", dir, err)
147+
continue
148+
}
149+
if dir != cname {
150+
t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", dir, cname, dir)
151+
continue
152+
}
153+
// test non-canonical names
154+
test := strings.ToUpper(dir)
155+
p, err := filepath.EvalSymlinks(test)
156+
if err != nil {
157+
t.Errorf("EvalSymlinks(%q) error: %v", test, err)
158+
continue
159+
}
160+
if p != cname {
161+
t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname)
162+
continue
163+
}
164+
// another test
165+
test = strings.ToLower(dir)
166+
p, err = filepath.EvalSymlinks(test)
167+
if err != nil {
168+
t.Errorf("EvalSymlinks(%q) error: %v", test, err)
169+
continue
170+
}
171+
if p != cname {
172+
t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname)
173+
continue
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)