internal/testutil: get absolute filepath
@@ -21,6 +21,7 @@ import (
"log"
"os"
"path/filepath"
+ "strings"
"testing"
"golang.org/x/tools/go/packages"
@@ -89,11 +90,29 @@ func testContext() (Context, error) {
return tc, noProjectID
}
- pkgs, err := packages.Load(&packages.Config{Mode: packages.NeedName}, "github.com/GoogleCloudPlatform/golang-samples")
+ cfg := &packages.Config{
+ Mode: packages.NeedName | packages.NeedFiles,
+ Tests: true,
+ }
+ pkgs, err := packages.Load(cfg, "github.com/GoogleCloudPlatform/golang-samples")
if err != nil {
- return tc, fmt.Errorf("Could not find golang-samples: %v", err)
+ return tc, fmt.Errorf("could not find golang-samples: %v", err)
+ }
+ // packages.Load returns multiple values, some with files and some without.
+ // Some of the files are generated as part of the build and some are the
+ // normal Go source files we're looking for.
+ // We can probably assume the one we want is pkgs[2], but loop through
+ // looking for the one we want in case it ever changes.
+ for _, pkg := range pkgs {
+ if len(pkg.GoFiles) > 0 && strings.HasSuffix(pkg.GoFiles[0], ".go") {
+ // Use the directory of a file in the root package as the module
+ // root directory.
+ tc.Dir = filepath.Dir(pkg.GoFiles[0])
+ }
+ }
+ if tc.Dir == "" {
+ return tc, fmt.Errorf("could not find golang-samples directory")
}
- tc.Dir = pkgs[0].PkgPath
return tc, nil
}
