如何告诉Gazelle一个go文件是用于go_default_test而不是go_default_library?

huangapple go评论88阅读模式
英文:

How to tell gazelle that a go file is meant for go_default_test and not go_default_library?

问题

我有一个名为embed_testdata.go的文件,用于测试,但它本身没有测试(所以我不想在文件名后面加上_test.go)。我该如何告诉gazelle这是测试源码而不是生产源码呢?

顺便说一下,只是将它添加到go_default_test并从go_default_library中移除并不起作用,因为gazelle会撤销手动编辑的更改。

英文:

I have a file, embed_testdata.go, meant to be used in tests but does not itself have tests (so I don't want to suffix it with _test.go). How do I tell gazelle that it's really test source and not prod source?

FYI, simply adding it to go_default_test and removing it from go_default_library doesn't work since gazelle undoes that manual edit.

答案1

得分: 0

https://github.com/bazelbuild/bazel-gazelle#directives 是所有有效的gazelle指令。

没有特定的指令告诉它将非_test.go文件视为测试文件,但有两个指令可以用于实现这个效果,即excludekeep

# gazelle:exclude embed_testdata.go
…
go_test(
    name = "go_default_test",
    srcs = [
        "embed_testdata.go",  # keep
…

在嵌入文件系统时,还需要类似以下的内容:

go_test(
    name = "go_default_test",
…
    embedsrcs = glob(["testdata/**"]), # keep
…
英文:

https://github.com/bazelbuild/bazel-gazelle#directives are all the valid gazelle directives.

There's no specific directive that tells it to treat a non-_test.go file as a test but there are two that can be used to that effect, exclude and keep:

# gazelle:exclude embed_testdata.go
…
go_test(
    name = "go_default_test",
    srcs = [
        "embed_testdata.go",  # keep
…

When embedding a file system, something like the following is also needed:

go_test(
    name = "go_default_test",
…
    embedsrcs = glob(["testdata/**"]), # keep
…

huangapple
  • 本文由 发表于 2023年3月17日 01:06:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75759430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定