1
0
mirror of https://github.com/golang/go synced 2024-11-24 14:50:13 -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 package filepath_test
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -548,6 +549,7 @@ func TestIsAbs(t *testing.T) {
} }
type EvalSymlinksTest struct { type EvalSymlinksTest struct {
// If dest is empty, the path is created; otherwise the dest is symlinked to the path.
path, dest string path, dest string
} }
@ -575,34 +577,42 @@ var EvalSymlinksAbsWindowsTests = []EvalSymlinksTest{
{`c:\`, `c:\`}, {`c:\`, `c:\`},
} }
func testEvalSymlinks(t *testing.T, tests []EvalSymlinksTest) { // simpleJoin builds a file name from the directory and path.
for _, d := range tests { // It does not use Join because we don't want ".." to be evaluated.
if p, err := filepath.EvalSymlinks(d.path); err != nil { func simpleJoin(dir, path string) string {
t.Errorf("EvalSymlinks(%q) error: %v", d.path, err) return dir + string(filepath.Separator) + path
} else if filepath.Clean(p) != filepath.Clean(d.dest) {
t.Errorf("EvalSymlinks(%q)=%q, want %q", d.path, p, d.dest)
}
}
} }
func TestEvalSymlinks(t *testing.T) { func TestEvalSymlinks(t *testing.T) {
t.Logf("test needs to be rewritten; disabled") tmpDir, err := ioutil.TempDir("", "evalsymlink")
return 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 { for _, d := range EvalSymlinksTestDirs {
var err error var err error
path := simpleJoin(tmpDir, d.path)
if d.dest == "" { if d.dest == "" {
err = os.Mkdir(d.path, 0755) err = os.Mkdir(path, 0755)
} else { } else {
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
err = os.Symlink(d.dest, d.path) err = os.Symlink(d.dest, path)
} }
} }
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
var tests []EvalSymlinksTest var tests []EvalSymlinksTest
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
for _, d := range EvalSymlinksTests { for _, d := range EvalSymlinksTests {
@ -614,24 +624,17 @@ func TestEvalSymlinks(t *testing.T) {
} else { } else {
tests = EvalSymlinksTests tests = EvalSymlinksTests
} }
// relative
testEvalSymlinks(t, tests) // Evaluate the symlink farm.
// absolute for _, d := range tests {
goroot, err := filepath.EvalSymlinks(os.Getenv("GOROOT")) path := simpleJoin(tmpDir, d.path)
if err != nil { dest := simpleJoin(tmpDir, d.dest)
t.Fatalf("EvalSymlinks(%q) error: %v", os.Getenv("GOROOT"), err) if p, err := filepath.EvalSymlinks(path); err != nil {
} t.Errorf("EvalSymlinks(%q) error: %v", d.path, err)
testroot := filepath.Join(goroot, "src", "pkg", "path", "filepath") } else if filepath.Clean(p) != filepath.Clean(dest) {
for i, d := range tests { t.Errorf("Clean(%q)=%q, want %q", path, p, dest)
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)
} }
} }
testEvalSymlinks(t, tests)
} }
// Test paths relative to $GOROOT/src // Test paths relative to $GOROOT/src