Skip to content

Commit

Permalink
testdata: get test assets related to package root instead of GOPATH (#…
Browse files Browse the repository at this point in the history
…2272)

fixes #2270
  • Loading branch information
menghanl committed Aug 22, 2018
1 parent 9907ae0 commit e00d249
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions testdata/testdata.go
Expand Up @@ -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.
Expand All @@ -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)
}

0 comments on commit e00d249

Please sign in to comment.