[英]How to add input files to bazel run in TeamCity
我對bazel非常陌生,所以很多理解可能是錯誤的。
我有一個具有以下結構的本地目錄:
/my/certain/path
|- my_test.go
|- my_utils.go
|- BUILD.bazel
|- mydir
|- input1
|- input2
我在 TeamCity 中使用bazel run
運行 go 測試,其中my_test.go
(一些敏感信息被替換為xxx
或...
):
bazel run \
//my/certain/path:my_test \
--config=crosslinux --config=test \
--test_timeout=1800
在/my/certain/path/BUILD.bazel
我有
go_library(
name = "path",
srcs = ["my_utils.go"],
importpath = "/my/certain/path",
visibility = ["//visibility:public"],
deps = [
"@com_github_docker_docker//api/types",
...
"@org_golang_x_net//context",
],
)
go_test(
name = "my_test",
size = "enormous",
srcs = ["my_test.go"],
embed = [":xxx"],
deps = [
"//pkg/util/contextutil",
"//pkg/util/log",
"@com_github_docker_docker//client",
"@org_golang_x_net//context",
],
)
my_test.go
實際上會在運行時使用mydir
中的文件作為輸入。
但是在 teamcity 上運行時,我注意到mydir
沒有安裝在發生此 bazel 測試的臨時目錄中,這導致my_test.go
失敗。
我試圖通過添加這些行來打印出當前目錄和my_test.go
中的所有文件來解決它:
fmt.Println("==== listing all files in go====")
err := filepath.Walk(".",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
fmt.Println(path, info.Size())
return nil
})
if err != nil {
t.Fatal(err)
}
fmt.Println("==== end listing all files in go====")
ctx := context.Background()
pwd, err := os.Getwd()
if err != nil {
t.Fatal(fmt.Errorf("cannot get pwd: %v", err))
}
fmt.Println("-----pwd:", pwd)
它表明,
==== listing all files in go====
17:26:17 . 4096
17:26:17 my_test_ 4096
17:26:17 my_test_/my_test 152
17:26:17 ==== end listing all files in go====
pwd: /home/agent/.cache/bazel/_bazel_agent/xxxxx/execroot/myname/bazel-out/k8-fastbuild/bin/my/path/my_test_/my_test.runfiles/my/certain/path
所以我的目標是將mydir
掛載到這個工作目錄( /home/agent/.cache/bazel/_bazel_agent/xxxxx/execroot/myname/bazel-out/k8-fastbuild/bin/my/path/my_test_/ /home/agent/.cache/bazel/_bazel_agent/xxxxx/execroot/myname/bazel-out/k8-fastbuild/bin/my/path/my_test_/my_test.runfiles/my/certain/path
)。
我不確定這是 TeamCity 的特定問題,還是 bazel 有辦法指定它。
我通過在BUILD.bazel
中添加data
條目來解決它:
go_test(
name = "my_test",
size = "enormous",
srcs = ["my_test.go"],
embed = [":xxx"],
data = glob([
"mydir/**",
]),
deps = [
"//pkg/util/contextutil",
"//pkg/util/log",
"@com_github_docker_docker//client",
"@org_golang_x_net//context",
],
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.