mirror of
https://github.com/golang/go
synced 2024-11-21 21:04:41 -07:00
path: add IsAbs
R=rsc, imkrasin, r CC=golang-dev https://golang.org/cl/1969042
This commit is contained in:
parent
e56c0555da
commit
dfb2af6099
@ -208,3 +208,9 @@ func Base(name string) string {
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// IsAbs returns true if the path is absolute.
|
||||
func IsAbs(path string) bool {
|
||||
// TODO: Add Windows support
|
||||
return strings.HasPrefix(path, "/")
|
||||
}
|
||||
|
@ -307,3 +307,27 @@ func TestBase(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type IsAbsTest struct {
|
||||
path string
|
||||
isAbs bool
|
||||
}
|
||||
|
||||
var isAbsTests = []IsAbsTest{
|
||||
IsAbsTest{"", false},
|
||||
IsAbsTest{"/", true},
|
||||
IsAbsTest{"/usr/bin/gcc", true},
|
||||
IsAbsTest{"..", false},
|
||||
IsAbsTest{"/a/../bb", true},
|
||||
IsAbsTest{".", false},
|
||||
IsAbsTest{"./", false},
|
||||
IsAbsTest{"lala", false},
|
||||
}
|
||||
|
||||
func TestIsAbs(t *testing.T) {
|
||||
for _, test := range isAbsTests {
|
||||
if r := IsAbs(test.path); r != test.isAbs {
|
||||
t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user