Go绝对导入和Travis CI

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

Go absolute imports and Travis CI

问题

我在http://github.com/NeilGarb/budget上设置了一个公共的golang项目,其中包含core和api两个子包。我将项目保存在$GOPATH/src/github.com/NeilGarb/budget目录下。

我想使用Travis CI来运行core的ginkgo测试,但是我的测试包中使用了绝对导入,Travis会报找不到这些包的错误。

例如,我的测试包包含以下内容:

package user_test

import (
    . "github.com/NeilGarb/budget/core"
)

在我的本地机器上运行ant命令没有问题,但是当Travis尝试运行测试时,我得到了以下错误:

core-test:
[exec] Failed to compile core:
[exec] 
[exec] can't load package: package github.com/NeilGarb/budget/core: cannot find package "github.com/NeilGarb/budget/core" in any of:
[exec] 	/home/travis/.gvm/gos/go1.2/src/pkg/github.com/NeilGarb/budget/core (from $GOROOT)
[exec] 	/home/travis/.gvm/pkgsets/go1.2/global/src/github.com/NeilGarb/budget/core (from $GOPATH)
[exec] 
[exec] Ginkgo ran in 714.967041ms
[exec] Test Suite Failed

我尝试在我的测试中使用相对导入(例如. "../core"),这样可以工作,但是我的覆盖率始终显示为0%。

该怎么办? Go绝对导入和Travis CI

英文:

I have a public golang project set up at http://github.com/NeilGarb/budget, with subpackages core and api. I keep the project in $GOPATH/src/github.com/NeilGarb/budget.

I would like to run core's ginkgo tests using Travis CI, but I'm using absolute imports in my test packages and Travis complains it can't find said packages.

For example, my test includes:

package user_test

import (
    . "github.com/NeilGarb/budget/core"
)

Running ant works fine on my local machine, but when travis tries to run the test I get:

core-test:
[exec] Failed to compile core:
[exec] 
[exec] can't load package: package github.com/NeilGarb/budget/core: cannot find package "github.com/NeilGarb/budget/core" in any of:
[exec] 	/home/travis/.gvm/gos/go1.2/src/pkg/github.com/NeilGarb/budget/core (from $GOROOT)
[exec] 	/home/travis/.gvm/pkgsets/go1.2/global/src/github.com/NeilGarb/budget/core (from $GOPATH)
[exec] 
[exec] Ginkgo ran in 714.967041ms
[exec] Test Suite Failed

I've tried to use relative imports in my tests (i.e. . "../core"), which works, but then my coverage always shows 0% covered.

What to do? Go绝对导入和Travis CI

答案1

得分: 2

我通过在$GOPATH/src/github.com/NeilGarb/budget和$TRAVIS_BUILD_DIR之间创建一个符号链接来解决了这个问题。

英文:

I've solved this problem by constructing a symlink from $GOPATH/src/github.com/NeilGarb/budget to $TRAVIS_BUILD_DIR.

答案2

得分: 0

github.com/NeilGarb/budget/core 需要将该包作为依赖进行打包,以便代码可用。请参阅 https://golang.org/cmd/go/#hdr-Vendor_Directories 获取文档。

英文:

github.com/NeilGarb/budget/core would need to vendor that package for the code to be available. See https://golang.org/cmd/go/#hdr-Vendor_Directories for documentation.

huangapple
  • 本文由 发表于 2014年3月21日 21:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/22560778.html
匿名

发表评论

匿名网友

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

确定