...

Package bazel_testing

import "github.com/bazelbuild/rules_go/go/tools/bazel_testing"
Overview
Index

Overview ▾

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.

Constants

const (
    // Standard Bazel exit codes.
    // A subset of codes in https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/util/ExitCode.java.
    SUCCESS                    = 0
    BUILD_FAILURE              = 1
    COMMAND_LINE_ERROR         = 2
    TESTS_FAILED               = 3
    NO_TESTS_FOUND             = 4
    RUN_FAILURE                = 6
    ANALYSIS_FAILURE           = 7
    INTERRUPTED                = 8
    LOCK_HELD_NOBLOCK_FOR_LOCK = 9
)

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 BazelOutputWithInput

func BazelOutputWithInput(stdin io.Reader, args ...string) ([]byte, []byte, error)

BazelOutputWithInput invokes a bazel command with a list of arguments and an input stream 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

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 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

    // NogoIncludes is the list of targets to include for Nogo linting.
    NogoIncludes []string

    // NogoExcludes is the list of targets to include for Nogo linting.
    NogoExcludes []string

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

    // ModuleFileSuffix is a string that should be appended to the end of a
    // default generated MODULE.bazel file. If this is empty, no such file is
    // generated.
    ModuleFileSuffix 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
}

type StderrExitError

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

type StderrExitError struct {
    Err *exec.ExitError
}

func (*StderrExitError) Error

func (e *StderrExitError) Error() string

func (*StderrExitError) Unwrap

func (e *StderrExitError) Unwrap() error