Version: v0.24.3 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2020 License: Apache-2.0 Imports: 18 Imported by: 0



Package bazel_testing provides an integration testing framework for testing rules_go with Bazel.

Tests may be written by declaring a go_bazel_test target instead of a go_test (go_bazel_test is defined in def.bzl here), then calling TestMain. Tests are run in a synthetic test workspace. Tests may run bazel commands with RunBazel.



View Source
const (
	// Standard Bazel exit codes.
	// A subset of codes in
	SUCCESS                    = 0
	BUILD_FAILURE              = 1
	TESTS_FAILED               = 3
	NO_TESTS_FOUND             = 4
	RUN_FAILURE                = 6
	ANALYSIS_FAILURE           = 7
	INTERRUPTED                = 8


This section is empty.


func BazelCmd

func BazelCmd(args ...string) *exec.Cmd

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 BazelOutput

func BazelOutput(args ...string) ([]byte, error)

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 RunBazel

func RunBazel(args ...string) error

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 TestMain

func TestMain(m *testing.M, args Args)

TestMain should be called by tests using this framework from a function named "TestMain". For example:

func TestMain(m *testing.M) {
  os.Exit(bazel_testing.TestMain(m, bazel_testing.Args{...}))

TestMain constructs a set of workspaces and changes the working directory to the main workspace.


type Args

type Args struct {
	// Main is a text archive containing files in the main workspace.
	// The text archive format is parsed by
	// //go/tools/internal/txtar:go_default_library, which is copied from
	// cmd/go/internal/txtar. If this archive does not contain a WORKSPACE file,
	// a default file will be synthesized.
	Main string

	// Nogo is the nogo target to pass to go_register_toolchains. By default,
	// nogo is not used.
	Nogo string

	// WorkspaceSuffix is a string that should be appended to the end
	// of the default generated WORKSPACE file.
	WorkspaceSuffix string

	// SetUp is a function that is executed inside the context of the testing
	// workspace. It is executed once and only once before the beginning of
	// all tests. If SetUp returns a non-nil error, execution is halted and
	// tests cases are not executed.
	SetUp func() error

Args is a list of arguments to TestMain. It's defined as a struct so that new optional arguments may be added without breaking compatibility.

type StderrExitError

type StderrExitError struct {
	Err *exec.ExitError

StderrExitError wraps *exec.ExitError and prints the complete stderr output from a command.

func (*StderrExitError) Error

func (e *StderrExitError) Error() string

func (*StderrExitError) Unwrap

func (e *StderrExitError) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL