1
0
mirror of https://github.com/golang/go synced 2024-11-24 12:10:03 -07:00

path/filepath: repair and simplify the symlink test

I hate symlinks.
Fixes #2787.

R=golang-dev, dsymonds, rsc
CC=golang-dev
https://golang.org/cl/5638043
This commit is contained in:
Rob Pike 2012-02-07 11:00:13 +11:00
parent d887a31b7c
commit 97ef437212

View File

@ -5,6 +5,7 @@
package filepath_test
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -548,6 +549,7 @@ func TestIsAbs(t *testing.T) {
}
type EvalSymlinksTest struct {
// If dest is empty, the path is created; otherwise the dest is symlinked to the path.
path, dest string
}
@ -575,34 +577,42 @@ var EvalSymlinksAbsWindowsTests = []EvalSymlinksTest{
{`c:\`, `c:\`},
}
func testEvalSymlinks(t *testing.T, tests []EvalSymlinksTest) {
for _, d := range tests {
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("EvalSymlinks(%q)=%q, want %q", d.path, p, d.dest)
}
}
// 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 {
return dir + string(filepath.Separator) + path
}
func TestEvalSymlinks(t *testing.T) {
t.Logf("test needs to be rewritten; disabled")
return
tmpDir, err := ioutil.TempDir("", "evalsymlink")
if err != nil {
t.Fatal("creating temp dir:", err)
}
defer os.RemoveAll(tmpDir)
defer os.RemoveAll("test")
// /tmp may itself be a symlink! Avoid the confusion, although
// it means trusting the thing we're testing.
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal("eval symlink for tmp dir:", err)
}
// Create the symlink farm using relative paths.
for _, d := range EvalSymlinksTestDirs {
var err error
path := simpleJoin(tmpDir, d.path)
if d.dest == "" {
err = os.Mkdir(d.path, 0755)
err = os.Mkdir(path, 0755)
} else {
if runtime.GOOS != "windows" {
err = os.Symlink(d.dest, d.path)
err = os.Symlink(d.dest, path)
}
}
if err != nil {
t.Fatal(err)
}
}
var tests []EvalSymlinksTest
if runtime.GOOS == "windows" {
for _, d := range EvalSymlinksTests {
@ -614,24 +624,17 @@ func TestEvalSymlinks(t *testing.T) {
} else {
tests = EvalSymlinksTests
}
// relative
testEvalSymlinks(t, tests)
// absolute
goroot, err := filepath.EvalSymlinks(os.Getenv("GOROOT"))
if err != nil {
t.Fatalf("EvalSymlinks(%q) error: %v", os.Getenv("GOROOT"), err)
}
testroot := filepath.Join(goroot, "src", "pkg", "path", "filepath")
for i, d := range tests {
tests[i].path = filepath.Join(testroot, d.path)
tests[i].dest = filepath.Join(testroot, d.dest)
}
if runtime.GOOS == "windows" {
for _, d := range EvalSymlinksAbsWindowsTests {
tests = append(tests, d)
// Evaluate the symlink farm.
for _, d := range tests {
path := simpleJoin(tmpDir, d.path)
dest := simpleJoin(tmpDir, d.dest)
if p, err := filepath.EvalSymlinks(path); err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", d.path, err)
} else if filepath.Clean(p) != filepath.Clean(dest) {
t.Errorf("Clean(%q)=%q, want %q", path, p, dest)
}
}
testEvalSymlinks(t, tests)
}
// Test paths relative to $GOROOT/src