sh_binary和sh_test中数据的重新排列文件夹结构?

发布于 2025-02-13 18:13:17 字数 700 浏览 0 评论 0原文

假设我有这样的源文件:

.
├── config.json
└── test.sh

我想使用这些文件创建sh_test

但是,当测试运行时,test.sh期望config.json处于不同的位置:

.
├── configs
│   └── prod.json    # config.json
└── test.sh

我不想重新安排我的源代码以满足> test.sh,我也不想添加一堆文件复制/移动到test.sh的开始。

是否可以告诉Bazel如何以任意方式安排test.sh的文件?

理想的语法就是这样:

sh_test(
  name = "test",
  srcs = [ "test.sh" ],
  data = {  
    "configs/prod.json": "config.json",
  },
)

Suppose I have source-files like this:

.
├── config.json
└── test.sh

I want to create an sh_test using these files.

However, when the test runs, test.sh expects config.json to be in a different place:

.
├── configs
│   └── prod.json    # config.json
└── test.sh

I do not want to rearrange my source-code to satisfy test.sh and I also don't want to add a bunch of file copying/moving to the start of test.sh.

Is it possible to tell Bazel how to arrange the files for test.sh in an arbitrary way?

Ideal syntax would be like this:

sh_test(
  name = "test",
  srcs = [ "test.sh" ],
  data = {  
    "configs/prod.json": "config.json",
  },
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

憧憬巴黎街头的黎明 2025-02-20 18:13:17

使用genrule

genrule(
  name = "prod_json",
  srcs = ["config.json"],
  outs = ["configs/prod.json"],
  cmd = "cp 
lt; $@",
)

sh_test(
    name = "test",
    srcs = ["test.sh"],
    data = ["configs/prod.json"],
)

或者,您可以编写与问题中的语法匹配的宏:

# BUILD
load(":sh_test.bzl", "sh_test")

sh_test(
    name = "test",
    srcs = ["test.sh"],
    data = {
      "configs/test.json": "config_test.json",
      "configs/staging.json": "config_staging.json",
      "configs/prod.json": "config.json",
    }
)
# sh_test.bzl

def sh_test(name, data = {}, **kwargs):
    datas = []
    for dest, src in data.items():
        genrule_name = dest.replace("/", "_")
        native.genrule(
            name = genrule_name,
            srcs = [src],
            outs = [dest],
            cmd = "cp 
lt; $@",
        )
        datas.append(genrule_name)

    native.sh_binary(
        name = name,
        data = datas,
        **kwargs
    )

Use a genrule:

genrule(
  name = "prod_json",
  srcs = ["config.json"],
  outs = ["configs/prod.json"],
  cmd = "cp 
lt; $@",
)

sh_test(
    name = "test",
    srcs = ["test.sh"],
    data = ["configs/prod.json"],
)

Alternatively, you could write a macro that matches the syntax in your question:

# BUILD
load(":sh_test.bzl", "sh_test")

sh_test(
    name = "test",
    srcs = ["test.sh"],
    data = {
      "configs/test.json": "config_test.json",
      "configs/staging.json": "config_staging.json",
      "configs/prod.json": "config.json",
    }
)
# sh_test.bzl

def sh_test(name, data = {}, **kwargs):
    datas = []
    for dest, src in data.items():
        genrule_name = dest.replace("/", "_")
        native.genrule(
            name = genrule_name,
            srcs = [src],
            outs = [dest],
            cmd = "cp 
lt; $@",
        )
        datas.append(genrule_name)

    native.sh_binary(
        name = name,
        data = datas,
        **kwargs
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文