From 5342aedeed54606fa1dcad2f039744fabfcc7544 Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Tue, 17 May 2011 12:33:36 +1000 Subject: [PATCH] filepath: make EvalSymlinks work on Windows Fixes #1830. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/4526060 --- src/pkg/path/filepath/path.go | 9 +++++ src/pkg/path/filepath/path_test.go | 56 +++++++++++++++++++----------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go index 541a233066a..6917218dbef 100644 --- a/src/pkg/path/filepath/path.go +++ b/src/pkg/path/filepath/path.go @@ -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, diff --git a/src/pkg/path/filepath/path_test.go b/src/pkg/path/filepath/path_test.go index b3b6eb5abaf..b147349836a 100644 --- a/src/pkg/path/filepath/path_test.go +++ b/src/pkg/path/filepath/path_test.go @@ -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 { - err = os.Symlink(d.dest, d.path) + if runtime.GOOS != "windows" { + err = os.Symlink(d.dest, d.path) + } } if err != nil { t.Fatal(err) } } - // relative - 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) + var tests []EvalSymlinksTest + if runtime.GOOS == "windows" { + for _, d := range EvalSymlinksTests { + 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), - } - 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) + 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) } } + testEvalSymlinks(t, tests) } // Test paths relative to $GOROOT/src