...

Source file src/gotest.tools/v3/internal/source/bazel.go

Documentation: gotest.tools/v3/internal/source

     1  package source
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  )
     8  
     9  // These Bazel env vars are documented here:
    10  // https://bazel.build/reference/test-encyclopedia
    11  
    12  // Signifies test executable is being driven by `bazel test`.
    13  //
    14  // Due to Bazel's compilation and sandboxing strategy,
    15  // some care is required to handle resolving the original *.go source file.
    16  var inBazelTest = os.Getenv("BAZEL_TEST") == "1"
    17  
    18  // The name of the target being tested (ex: //some_package:some_package_test)
    19  var bazelTestTarget = os.Getenv("TEST_TARGET")
    20  
    21  // Absolute path to the base of the runfiles tree
    22  var bazelTestSrcdir = os.Getenv("TEST_SRCDIR")
    23  
    24  // The local repository's workspace name (ex: __main__)
    25  var bazelTestWorkspace = os.Getenv("TEST_WORKSPACE")
    26  
    27  func bazelSourcePath(filename string) (string, error) {
    28  	// Use the env vars to resolve the test source files,
    29  	// which must be provided as test data in the respective go_test target.
    30  	filename = filepath.Join(bazelTestSrcdir, bazelTestWorkspace, filename)
    31  
    32  	_, err := os.Stat(filename)
    33  	if os.IsNotExist(err) {
    34  		return "", fmt.Errorf(bazelMissingSourceMsg, filename, bazelTestTarget)
    35  	}
    36  	return filename, nil
    37  }
    38  
    39  var bazelMissingSourceMsg = `
    40  the test source file does not exist: %s
    41  It appears that you are running this test under Bazel (target: %s).
    42  Check that your test source files are added as test data in your go_test targets.
    43  
    44  Example:
    45      go_test(
    46          name = "your_package_test",
    47          srcs = ["your_test.go"],
    48          deps = ["@tools_gotest_v3//assert"],
    49          data = glob(["*_test.go"])
    50      )"
    51  `
    52  

View as plain text