Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions go/tools/bazel_testing/bazel_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ func TestMain(m *testing.M, args Args) {
code = m.Run()
}

// RunBazel invokes a bazel command with a list of arguments.
//
// If the command starts but exits with a non-zero status, a *StderrExitError
// will be returned which wraps the original *exec.ExitError.
func RunBazel(args ...string) error {
// BazelCmd prepares a bazel command for execution. It chooses the correct
// bazel binary based on the environment and sanitizes the environment to
// hide that this code is executing inside a bazel test.
func BazelCmd(args ...string) *exec.Cmd {
cmd := exec.Command("bazel", args...)
for _, e := range os.Environ() {
// Filter environment variables set by the bazel test wrapper script.
Expand All @@ -126,6 +125,15 @@ func RunBazel(args ...string) error {
}
cmd.Env = append(cmd.Env, e)
}
return cmd
}

// RunBazel invokes a bazel command with a list of arguments.
//
// If the command starts but exits with a non-zero status, a *StderrExitError
// will be returned which wraps the original *exec.ExitError.
func RunBazel(args ...string) error {
cmd := BazelCmd(args...)

buf := &bytes.Buffer{}
cmd.Stderr = buf
Expand All @@ -137,6 +145,25 @@ func RunBazel(args ...string) error {
return err
}

// BazelOutput invokes a bazel command with a list of arguments and returns
// the content of stdout.
//
// If the command starts but exits with a non-zero status, a *StderrExitError
// will be returned which wraps the original *exec.ExitError.
func BazelOutput(args ...string) ([]byte, error) {
cmd := BazelCmd(args...)
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stdout = stdout
cmd.Stderr = stderr
err := cmd.Run()
if eErr, ok := err.(*exec.ExitError); ok {
eErr.Stderr = stderr.Bytes()
err = &StderrExitError{Err: eErr}
}
return stdout.Bytes(), err
}

// StderrExitError wraps *exec.ExitError and prints the complete stderr output
// from a command.
type StderrExitError struct {
Expand Down