Commit e6f0b746 authored by Alex Brainman's avatar Alex Brainman

path/filepath: make Abs handle paths like c:a.txt properly

Fixes #8145.

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/126440043
parent 67812a7c
...@@ -231,6 +231,10 @@ func EvalSymlinks(path string) (string, error) { ...@@ -231,6 +231,10 @@ func EvalSymlinks(path string) (string, error) {
// working directory to turn it into an absolute path. The absolute // working directory to turn it into an absolute path. The absolute
// path name for a given file is not guaranteed to be unique. // path name for a given file is not guaranteed to be unique.
func Abs(path string) (string, error) { func Abs(path string) (string, error) {
return abs(path)
}
func unixAbs(path string) (string, error) {
if IsAbs(path) { if IsAbs(path) {
return Clean(path), nil return Clean(path), nil
} }
......
...@@ -28,3 +28,7 @@ func splitList(path string) []string { ...@@ -28,3 +28,7 @@ func splitList(path string) []string {
} }
return strings.Split(path, string(ListSeparator)) return strings.Split(path, string(ListSeparator))
} }
func abs(path string) (string, error) {
return unixAbs(path)
}
...@@ -628,6 +628,8 @@ var winisabstests = []IsAbsTest{ ...@@ -628,6 +628,8 @@ var winisabstests = []IsAbsTest{
{`\`, false}, {`\`, false},
{`\Windows`, false}, {`\Windows`, false},
{`c:a\b`, false}, {`c:a\b`, false},
{`c:\a\b`, true},
{`c:/a/b`, true},
{`\\host\share\foo`, true}, {`\\host\share\foo`, true},
{`//host/share/foo/bar`, true}, {`//host/share/foo/bar`, true},
} }
...@@ -807,6 +809,19 @@ func TestAbs(t *testing.T) { ...@@ -807,6 +809,19 @@ func TestAbs(t *testing.T) {
} }
} }
if runtime.GOOS == "windows" {
vol := filepath.VolumeName(root)
var extra []string
for _, path := range absTests {
if strings.Index(path, "$") != -1 {
continue
}
path = vol + path
extra = append(extra, path)
}
absTests = append(absTests, extra...)
}
err = os.Chdir(absTestDirs[0]) err = os.Chdir(absTestDirs[0])
if err != nil { if err != nil {
t.Fatal("chdir failed: ", err) t.Fatal("chdir failed: ", err)
......
...@@ -30,3 +30,7 @@ func splitList(path string) []string { ...@@ -30,3 +30,7 @@ func splitList(path string) []string {
} }
return strings.Split(path, string(ListSeparator)) return strings.Split(path, string(ListSeparator))
} }
func abs(path string) (string, error) {
return unixAbs(path)
}
...@@ -6,6 +6,7 @@ package filepath ...@@ -6,6 +6,7 @@ package filepath
import ( import (
"strings" "strings"
"syscall"
) )
func isSlash(c uint8) bool { func isSlash(c uint8) bool {
...@@ -103,3 +104,7 @@ func splitList(path string) []string { ...@@ -103,3 +104,7 @@ func splitList(path string) []string {
return list return list
} }
func abs(path string) (string, error) {
return syscall.FullPath(path)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment