testdata: get test assets related to package root instead of GOPATH (#2272)
fixes #2270
This commit is contained in:
Родитель
9907ae0e57
Коммит
e00d24941b
|
@ -18,11 +18,18 @@
|
|||
package testdata
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// basepath is the root directory of this package.
|
||||
var basepath string
|
||||
|
||||
func init() {
|
||||
_, currentFile, _, _ := runtime.Caller(0)
|
||||
basepath = filepath.Dir(currentFile)
|
||||
}
|
||||
|
||||
// Path returns the absolute path the given relative file or directory path,
|
||||
// relative to the google.golang.org/grpc/testdata directory in the user's GOPATH.
|
||||
// If rel is already absolute, it is returned unmodified.
|
||||
|
@ -31,33 +38,5 @@ func Path(rel string) string {
|
|||
return rel
|
||||
}
|
||||
|
||||
v, err := goPackagePath("google.golang.org/grpc/testdata")
|
||||
if err != nil {
|
||||
log.Fatalf("Error finding google.golang.org/grpc/testdata directory: %v", err)
|
||||
}
|
||||
|
||||
return filepath.Join(v, rel)
|
||||
}
|
||||
|
||||
func goPackagePath(pkg string) (path string, err error) {
|
||||
gp := os.Getenv("GOPATH")
|
||||
if gp == "" {
|
||||
return path, os.ErrNotExist
|
||||
}
|
||||
|
||||
for _, p := range filepath.SplitList(gp) {
|
||||
dir := filepath.Join(p, "src", filepath.FromSlash(pkg))
|
||||
fi, err := os.Stat(dir)
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !fi.IsDir() {
|
||||
continue
|
||||
}
|
||||
return dir, nil
|
||||
}
|
||||
return path, os.ErrNotExist
|
||||
return filepath.Join(basepath, rel)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче