godoc/vfs/zipfs: join paths to get correct RootType
The logic was incorrectly written from vfs/os.go. zipfs filesystem passes actual paths during dirTree building. So we need to join the paths to determine whether they are under GOROOT or GOPATH. Updates golang/go#27205 Change-Id: Ic4330fce02c6ebfc44ae21122e2412c33f0cd45a Reviewed-on: https://go-review.googlesource.com/138435 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
73ed285d4c
commit
ae8529dc16
|
@ -86,7 +86,7 @@ func (fs *zipFS) String() string {
|
||||||
func (fs *zipFS) RootType(abspath string) vfs.RootType {
|
func (fs *zipFS) RootType(abspath string) vfs.RootType {
|
||||||
var t vfs.RootType
|
var t vfs.RootType
|
||||||
switch {
|
switch {
|
||||||
case abspath == vfs.GOROOT:
|
case exists(path.Join(vfs.GOROOT, abspath)):
|
||||||
t = vfs.RootTypeGoRoot
|
t = vfs.RootTypeGoRoot
|
||||||
case isGoPath(abspath):
|
case isGoPath(abspath):
|
||||||
t = vfs.RootTypeGoPath
|
t = vfs.RootTypeGoPath
|
||||||
|
@ -94,15 +94,20 @@ func (fs *zipFS) RootType(abspath string) vfs.RootType {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func isGoPath(path string) bool {
|
func isGoPath(abspath string) bool {
|
||||||
for _, p := range filepath.SplitList(build.Default.GOPATH) {
|
for _, p := range filepath.SplitList(build.Default.GOPATH) {
|
||||||
if p == path {
|
if exists(path.Join(p, abspath)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func exists(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
func (fs *zipFS) Close() error {
|
func (fs *zipFS) Close() error {
|
||||||
fs.list = nil
|
fs.list = nil
|
||||||
return fs.ReadCloser.Close()
|
return fs.ReadCloser.Close()
|
||||||
|
|
|
@ -187,3 +187,20 @@ func TestZipFSOpenSeek(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRootType(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
fsType vfs.RootType
|
||||||
|
}{
|
||||||
|
{"/src/net/http", vfs.RootTypeGoRoot},
|
||||||
|
{"/src/badpath", ""},
|
||||||
|
{"/", vfs.RootTypeGoRoot},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range tests {
|
||||||
|
if fs.RootType(item.path) != item.fsType {
|
||||||
|
t.Errorf("unexpected fsType. Expected- %v, Got- %v", item.fsType, fs.RootType(item.path))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue