1
0
mirror of https://github.com/golang/go synced 2024-11-22 09:24: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 ( import (
"bytes" "bytes"
"os" "os"
"runtime"
"sort" "sort"
"strings" "strings"
) )
@ -178,6 +179,14 @@ func Ext(path string) string {
// links. // links.
// If path is relative it will be evaluated relative to the current directory. // If path is relative it will be evaluated relative to the current directory.
func EvalSymlinks(path string) (string, os.Error) { 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 const maxIter = 255
originalPath := path originalPath := path
// consume path by taking each frontmost path element, // consume path by taking each frontmost path element,

View File

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