使用`go.work`文件在多个测试文件上运行`go test`命令。

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

run go test command on multiple testfiles with go.work file

问题

截至Go 1.18版本,可以使用"go.work"文件作为多模块工作区保存文件,相当于"go.mod"文件。

我想知道如何在"go test"命令中使用定义的"go.work"文件。

我尝试了多种方法。当我打印"go.work"环境变量时,一切似乎都正常。

如何在"go.work"文件上使用"go test"命令?

假设我有一个目录,其中包含两个子目录,每个子目录都有一个go.mod文件和两个.go源文件(file.go和file_test.go),而主目录只有go.work文件。

go test -v .\go.work --> 不可行

go test ".\..." --> 按照指定的方式不起作用,无法正确解析。

标志build.experimentalWorkspaceModule设置为false,因为它不再需要。只有go test all似乎可以工作,但我只想测试我的包,而不是安装的所有包。

我查阅了整个文档,但找不到示例,因为它似乎是新的,没有经常使用,pkg.go.dev也没有提供信息。

也没有在这里找到 https://go.googlesource.com/tools/+/refs/heads/master/gopls/doc/workspace.md

英文:

As of go 1.18 it is possible to use "go.work" files as multimodule workspace save file, equivalent to a "go.mod" file.

I want to know how to use the defined "go.work" file in the command go test.

I tried multiple approaches. When I print the go.work env variables everything seems to work.

How do I use the "go test" command on a "go.work" file?

lets assume i have a directory with two subdirectories with each a go.mod file and two .go source files (file.go and file_test.go) and the main directory with just the go.work file.

go test -v .\go.work --> not possible

go test ".\ ..." --> doesn't work as specified, it can't resolve it correctly.

The flag build.experimentalWorkspaceModule is set to false because it isn't needed anymore. Just go test all seems to work, but I just want to test my packages, not the whole packages installed.

I dug into the whole documentation, I cant find an example because it seems to be new and not used frequently, the pkg.go.dev doesn't supply info.

Also not here https://go.googlesource.com/tools/+/refs/heads/master/gopls/doc/workspace.md

答案1

得分: 0

我不认为go.work可以用于这个目的。工作区是模块系统的一部分,基本上用于使设置一个本地多模块仓库更容易(对你来说)。以下是来自提案的引用:

go.work文件指定了一个由本地模块组成的工作区。在工作区模式下调用go命令时,它将始终选择这些模块和一致的依赖关系集。

还有来自工作区教程的引用:

可以使用go.work代替添加replace指令以跨多个模块工作。

最后,来自官方文档的引用:

工作区是磁盘上的一组模块,用作运行最小版本选择(MVS)时的主要模块。

同时运行多个模块的测试套件与go.work的用例无关。对于这个,你仍然需要使用一个shell脚本(参见https://stackoverflow.com/questions/61163306/running-go-test-in-parent-directory-of-multiple-go-modules)或类似的方法。

英文:

I don't think go.work can be used to this end. Workspaces is a part of the modules system, and are basically used to make it easier (for you) to set up a local multi-module repository. The following quote is from the proposal:

> The go.work file specifies a set of local modules that comprise a workspace. When invoked in workspace mode, the go command will always select these modules and a consistent set of dependencies.

And this one from the workspace tutorial:

> go.work can be used instead of adding replace directives to work across multiple modules.

And finally this one from the official docs:

> A workspace is a collection of modules on disk that are used as the main modules when running minimal version selection (MVS).

Running the test suite of several modules at once is orthogonal to go.work's use case. For that, you still have to use a shell script (see https://stackoverflow.com/questions/61163306/running-go-test-in-parent-directory-of-multiple-go-modules) or similar.

huangapple
  • 本文由 发表于 2022年10月25日 03:21:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/74185840.html
匿名

发表评论

匿名网友

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

确定