Bazel:如何获取我在Bazel测试中构建的二进制代码的路径 [英] Bazel: how to get the path of a binary that I built along a bazel test

查看:0
本文介绍了Bazel:如何获取我在Bazel测试中构建的二进制代码的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和Bazel一起写围棋测试。该测试将首先构建二进制文件,并将该二进制文件挂载到停靠容器中。所以我需要知道我构建的二进制文件的路径。

文件系统结构如下:

目录结构如下所示:

    some/of/my/path
    ├── BUILD.bazel
    ├── my_test.go
    └── my_tool
        ├── BUILD.bazel
        └── my_tool.go

我要在其中使用my_tool目录中的GO文件构建GO二进制文件。

my previous question's answer我了解到可以使用go_test中的data条目来构建二进制文件:

go_test(
    name = "my_test",
    srcs = ["my_test.go"],
    data = glob(["testdata/**"]) + [
        "//some/of/my/path/my_tool:my_tool",
    ],
    gotags = ["my_test"],
    deps = [
        "//pkg/util/contextutil",
        "//pkg/util/log",
    ...
    ],
)

它为my_tool构建了GO二进制文件。但现在的问题是,我如何知道的构建二进制文件my_toolmy_test.go中的位置?

my_test.go中,我需要编写如下内容:

package my_lucky_test

...

pathForMyTool := some_path
hostConfig := container.HostConfig{
Binds := []string {""%s/my_tool-bin:/workding-dir/my_tool-bin", pathForMyTool"}
}

我的问题是我如何才能获得";my_Tool-bin";的绝对路径?

推荐答案

只需运行;

bazel build //some/of/my/path:my_test

它应将二进制文件的路径打印到标准输出。

作为您当前方法的替代方法,我建议您使用bazelbuild/rules_docker#go_image,这是一种支持Bazel的容器构建方法。

编辑:OP已经澄清了问题,但我保留了上面的原始答案,因为其他人可能会觉得有用。

当您将二进制/测试与一些数据捆绑在一起时,data属性下的文件将变成一组"runfile"。每种语言都有不同的库用于解析运行文件。对于Golang,您需要使用Rules_Go中的bazel pkg来解析您的运行文件。例如

go_test(
    name = "my_test",
    srcs = ["my_test.go"],
    data = glob(["testdata/**"]) + [
        "//some/of/my/path/my_tool:my_tool",
    ],
    gotags = ["my_test"],
    deps = [
        "//pkg/util/contextutil",
        "//pkg/util/log",
        # NEW dep
        "@rules_go//rules_go/go/tools/bazel",
    ...
    ],
)

您的my_test.go

package my_lucky_test

# NEW
import(
    "github.com/bazelbuild/rules_go/go/tools/bazel"
)

#...

pathForMyTool, ok := bazel.FindBinary("some/of/my/path/my_tool", "my_tool")
if !ok {
    # Error handling
}
hostConfig := container.HostConfig{
    Binds := []string {""%s/my_tool-bin:/workding-dir/my_tool-bin", pathForMyTool}
}

这篇关于Bazel:如何获取我在Bazel测试中构建的二进制代码的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆