test local go module which is imported using replace directive and use vendor directory from main folder

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

test local go module which is imported using replace directive and use vendor directory from main folder

问题

我的Exporter是我本地的模块,我使用replace将其导入到主模块中。我想在MyExporter中使用位于主项目根目录下的vendor文件夹来运行测试。

我的项目目录结构如下:

bitbucket.com
|__agent
     |__Exporter
     |     |__MyExporter
     |            |__factory.go
     |               factory_test.go
     |               go.mod
     |__Runner
     |     |__main.go
     |        main_test.go
     |__Vendor
     |     |__bitbucket.com/agent/exporter/myexporter
     |                   |__factory.go
     |__go.mod

MyExporter的go.mod文件内容如下:

module bitbucket.com/agent/exporter/myexporter

Agent的go.mod文件内容如下:

module bitbucket.com/agent

require (
    bitbucket.com/agent/exporter/myexporter
)

replace bitbucket.com/agent/exporter/myexporter => ./exporter/myexporter

我的问题是:

  1. 如果我运行"go test -mod=vendor ./...",它只会运行main_test中的测试。
  2. 如果我使用"go test -mod=vendor exporter/myexporter",我会得到"package exporter/myexporter is not in GOROOT"的错误。
  3. 如果我运行"go test -mod=vendor bitbucket.com/agent/exporter/myexporter",则没有测试文件。
  4. 如果我切换到exporter/myexporter目录,然后使用"go test ./..."运行测试,它可以正常工作,但是"go test -mod=vendor ./..."不行,因为vendor目录位于agent的根目录下。
  5. 如果我运行"go test -mod=vendor ./exporter/myexporter/",我会得到"main module (bitbucket.com/agent) does not contain package bitbucket.com/agent/exporter/myexporter"的错误。

构建过程无法访问go包,它依赖于vendor文件夹,测试也是如此。我不想在myexporter中再添加一个vendor文件夹来存放依赖项的多个副本。有没有办法可以在agent/vendor文件夹中运行MyExporter内部的测试?

英文:

MyExporter is my local module that I import in to main module using replace. I want to run tests in MyExporter using vendor folder at root of main project.

My directory structure for project is like this

bitbucket.com
|__agent
     |__Exporter
     |     |__MyExporter
     |            |__factory.go
     |               factory_test.go
     |               go.mod
     |__Runner
     |     |__main.go
     |        main_test.go
     |__Vendor
     |     |__bitbucket.com/agent/exporter/myexporter
     |                   |__factory.go
     |__go.mod

MyExporter go.mod file

module bitbucket.com/agent/exporter/myexporter

Agent go.mod file has

module bitbucket.com/agent

require (
    bitbucket.com/agent/exporter/myexporter
)

replace bitbucket.com/agent/exporter/myexporter => ./exporter/myexporter

My problem is

  1. If I run "go test -mod=vendor ./..." it only tests in main_test run
  2. If I use "go test -mod=vendor exporter/myexporter" I get "package exporter/myexporter is not in GOROOT"
  3. If I run "go test -mod=vendor bitbucket.com/agent/exporter/myexporter" there are no test files
  4. If I switch to exporter/myexporter directory then run using "go test ./..." it works but not "go test -mod=vendor ./..." as vendor directory is at root of agent
  5. If I run "go test -mod=vendor ./exporter/myexporter/" I get "main module (bitbucket.com/agent) does not contain package bitbucket.com/agent/exporter/myexporter"

The build doesn't have access to go packages, it relies on vendor folder, so do tests. I don't want multiple copies of dependencies in one more vendor folder inside myexporter. Is there anything can be done in order to run tests inside MyExporter using agent/vendor folder?

答案1

得分: 1

有没有办法在MyExporter内部使用agent/vendor文件夹来运行测试?

没有。

您必须在MyExporter所在的位置测试您的myexporter。一旦它被vendored,只有实际的代码被vendored。

唯一可能的事情是合并这些模块。

实际上,问题归结为:如果您需要以并行/协同的方式测试这两个模块,那么这些实际上不是具有不同生命周期的两个不同模块。如果这些是具有不同生命周期的独立事物,您可以测试第一个,确保它正常工作,并将其vendored到第二个。然后测试第二个,包括其对第一个的使用,但不再运行第一个的测试,因为这不再需要。

英文:

> Is there anything can be done in order to run tests inside MyExporter using agent/vendor folder?

No.

You have to test your myexporter where it lives. Once it is vendored only the actual code is vendored.

The only thing possible is combine the modules.

It really boils down to: If you somehow need to test these two modules in parallel / in unisono then these are not really two distinct modules with different livecycles. And if these are standalone things with different livecylcles you can test the first, make sure it works and vendor it to the second. Then test the second, including its use of the first but without running the firsts tests as this is not needed anymore.

huangapple
  • 本文由 发表于 2023年7月1日 04:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76592104.html
匿名

发表评论

匿名网友

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

确定