1
0
mirror of https://github.com/golang/go synced 2024-11-21 22:14:41 -07:00

filepath: make EvalSymlinks work on Windows

Fixes #1830.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/4526060
This commit is contained in:
Alex Brainman 2011-05-17 12:33:36 +10:00
parent f570d9d765
commit 5342aedeed
2 changed files with 45 additions and 20 deletions

View File

@ -9,6 +9,7 @@ package filepath
import (
"bytes"
"os"
"runtime"
"sort"
"strings"
)
@ -178,6 +179,14 @@ func Ext(path string) string {
// links.
// If path is relative it will be evaluated relative to the current directory.
func EvalSymlinks(path string) (string, os.Error) {
if runtime.GOOS == "windows" {
// Symlinks are not supported under windows.
_, err := os.Lstat(path)
if err != nil {
return "", err
}
return Clean(path), nil
}
const maxIter = 255
originalPath := path
// consume path by taking each frontmost path element,

View File

@ -440,48 +440,64 @@ var EvalSymlinksTests = []EvalSymlinksTest{
{"test/link2/link3/test", "test"},
}
func TestEvalSymlinks(t *testing.T) {
// Symlinks are not supported under windows.
if runtime.GOOS == "windows" {
return
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)
}
}
}
func TestEvalSymlinks(t *testing.T) {
defer os.RemoveAll("test")
for _, d := range EvalSymlinksTestDirs {
var err os.Error
if d.dest == "" {
err = os.Mkdir(d.path, 0755)
} else {
if runtime.GOOS != "windows" {
err = os.Symlink(d.dest, d.path)
}
}
if err != nil {
t.Fatal(err)
}
}
// relative
var tests []EvalSymlinksTest
if runtime.GOOS == "windows" {
for _, d := range EvalSymlinksTests {
if p, err := filepath.EvalSymlinks(d.path); err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", d.path, err)
} else if p != d.dest {
t.Errorf("EvalSymlinks(%q)=%q, want %q", d.path, p, d.dest)
if d.path == d.dest {
// will test only real files and directories
tests = append(tests, d)
}
}
} 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 _, d := range EvalSymlinksTests {
a := EvalSymlinksTest{
filepath.Join(testroot, d.path),
filepath.Join(testroot, d.dest),
for i, d := range tests {
tests[i].path = filepath.Join(testroot, d.path)
tests[i].dest = filepath.Join(testroot, d.dest)
}
if p, err := filepath.EvalSymlinks(a.path); err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", a.path, err)
} else if p != a.dest {
t.Errorf("EvalSymlinks(%q)=%q, want %q", a.path, p, a.dest)
if runtime.GOOS == "windows" {
for _, d := range EvalSymlinksAbsWindowsTests {
tests = append(tests, d)
}
}
testEvalSymlinks(t, tests)
}
// Test paths relative to $GOROOT/src