...
1 package source
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 )
8
9
10
11
12
13
14
15
16 var inBazelTest = os.Getenv("BAZEL_TEST") == "1"
17
18
19 var bazelTestTarget = os.Getenv("TEST_TARGET")
20
21
22 var bazelTestSrcdir = os.Getenv("TEST_SRCDIR")
23
24
25 var bazelTestWorkspace = os.Getenv("TEST_WORKSPACE")
26
27 func bazelSourcePath(filename string) (string, error) {
28
29
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