Skip to content

Commit

Permalink
fixup fixture logic
Browse files Browse the repository at this point in the history
There was a bug with init() but it was resolved in
bazel-contrib/rules_go#2696

Changed to match other fixture methods.

Change-Id: I882b8535e5c5c117fb10c41d34c8eed1ccdb74bb
  • Loading branch information
mikedanese committed Jan 7, 2021
1 parent b624868 commit 7726137
Showing 1 changed file with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package fixtures

import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)

var (
Expand All @@ -38,28 +38,43 @@ var (
)

func init() {
_, thisFile, _, ok := runtime.Caller(0)
fixturesDir, err := pkgPath()
if err != nil {
panic(fmt.Sprintf("Cannot get path to the fixtures: %s", err))
}

CaCertPath = filepath.Join(fixturesDir, "ca.pem")
ServerCertPath = filepath.Join(fixturesDir, "server.pem")
ServerKeyPath = filepath.Join(fixturesDir, "server.key")
InvalidCertPath = filepath.Join(fixturesDir, "invalid.pem")
}

// pkgPath returns the absolute file path to this package's directory. With go
// test, we can just look at the runtime call stack. However, bazel compiles go
// binaries with the -trimpath option so the simple approach fails however we
// can consult environment variables to derive the path.
//
// The approach taken here works for both go test and bazel on the assumption
// that if and only if trimpath is passed, we are running under bazel.
func pkgPath() (string, error) {
_, thisFile, _, ok := runtime.Caller(1)
if !ok {
panic("Cannot get path to the fixtures")
return "", fmt.Errorf("failed to get current file")
}

fixturesDir := filepath.Dir(thisFile)
pkgPath := filepath.Dir(thisFile)

cwd, err := os.Getwd()
if err != nil {
panic("Cannot get CWD: " + err.Error())
// If we find bazel env variables, then -trimpath was passed so we need to
// construct the path from the environment.
if testSrcdir, testWorkspace := os.Getenv("TEST_SRCDIR"), os.Getenv("TEST_WORKSPACE"); testSrcdir != "" && testWorkspace != "" {
pkgPath = filepath.Join(testSrcdir, testWorkspace, pkgPath)
}

// When tests run in a bazel sandbox `runtime.Caller()`
// returns a relative path, when run with plain `go test` the path
// returned is absolute. To make those fixtures work in both those cases,
// we prepend the CWD iff the CWD is not yet part of the path to the fixtures.
if !strings.HasPrefix(fixturesDir, cwd) {
fixturesDir = filepath.Join(cwd, fixturesDir)
// If the path is still not absolute, something other than bazel compiled
// with -trimpath.
if !filepath.IsAbs(pkgPath) {
return "", fmt.Errorf("can't construct an absolute path from %q", pkgPath)
}

CaCertPath = filepath.Join(fixturesDir, "ca.pem")
ServerCertPath = filepath.Join(fixturesDir, "server.pem")
ServerKeyPath = filepath.Join(fixturesDir, "server.key")
InvalidCertPath = filepath.Join(fixturesDir, "invalid.pem")
return pkgPath, nil
}

0 comments on commit 7726137

Please sign in to comment.