使用 GVM 和 Go 1.2 版本的覆盖工具。

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

Using Go 1.2 cover tool with GVM

问题

我正在使用GVM来管理我的Go安装和路径,一切似乎都正常工作 - 我可以运行测试并生成构建。我现在正在尝试生成一个代码覆盖文件,但遇到了一些困难。

我的包定义在/home/bill/dev/server/model中。

当我运行:

$ go test -cover -coverprofile cover.out

测试成功运行并生成了一个覆盖文件。然而,覆盖文件中的路径看起来像这样:

_/home/bill/dev/server/model/activity.go:19.34,21.2 1 1

当我尝试创建一个HTML覆盖文件时,我得到以下错误:

$ go tool cover -html=cover.out

cover: 找不到"activity.go":无法在以下任何位置找到包"_/home/bill/dev/server/model/":
    /home/bill/.gvm/gos/go1.2/src/pkg/_/home/bill/dev/server/model (来自$GOROOT)
    /home/bill/.gvm/pkgsets/go1.2/global/src/_/home/bill/dev/server/model (来自$GOPATH)

我该如何解决这个问题?

附加细节

~ gvm use go1.2
正在使用版本go1.2

~ echo $GOPATH
/home/bill/.gvm/pkgsets/go1.2/global

~ echo $GOROOT
/home/bill/.gvm/gos/go1.2

我尝试手动设置我的$GOPATH,但这并没有改变cover.out文件。我还尝试手动编辑cover.out文件,但我无法弄清楚它实际上需要什么路径。在上面显示的默认配置中,运行go test会按预期运行。

尝试修复GOPATH

~ export GOPATH=/home/bill/dev/
~ ln -s /home/bill/dev/server /home/bill/.gvm/gos/go1.2/src

~ go test
找不到包"code.google.com/p/go.crypto/pbkdf2":无法在以下任何位置找到:
/home/bill/.gvm/gos/go1.2/src/pkg/code.google.com/p/go.crypto/pbkdf2 (来自$GOROOT)
/home/bill/dev/src/code.google.com/p/go.crypto/pbkdf2 (来自$GOPATH)
../util/log.go:4:2: 找不到包"github.com/kr/pretty":无法在以下任何位置找到:
/home/bill/.gvm/gos/go1.2/src/pkg/github.com/kr/pretty (来自$GOROOT)
/home/bill/dev/src/github.com/kr/pretty (来自$GOPATH)

这些是我之前使用go get下载的额外依赖项。它们最终位于/home/bill/.gvm/pkgsets/go1.2/global/src,而$GOPATH以前指向该路径。所以我改变了GOPATH

~ export GOPATH=/home/bill/dev/:/home/bill/.gvm/pkgsets/go1.2/global

这样测试就能再次运行,但cover.out文件仍然包含相同的目录,并且仍然给我相同的错误。

英文:

I'm using GVM to manage my go installations and paths and everything seems to work just fine - I can run tests and produce builds. I'm now trying to produce a code coverage file and am having some difficulty.

My package is defined in /home/bill/dev/server/model.

When I run:

$ go test -cover -coverprofile cover.out

The tests run successfully and a coverage file is produced. However, the paths in the coverage file look like this:

_/home/bill/dev/server/model/activity.go:19.34,21.2 1 1

And I get the following error when I try to create an html cover file:

$ go tool cover -html=cover.out

cover: can't find "activity.go": cannot find package "_/home/bill/dev/server/model/" in any of:
    /home/bill/.gvm/gos/go1.2/src/pkg/_/home/bill/dev/server/model (from $GOROOT)
    /home/bill/.gvm/pkgsets/go1.2/global/src/_/home/bill/dev/server/model (from $GOPATH)

How do I fix this?

Additional details

~ gvm use go1.2
Now using version go1.2

~ echo $GOPATH
/home/bill/.gvm/pkgsets/go1.2/global

~ echo $GOROOT
/home/bill/.gvm/gos/go1.2

I tried manually setting my $GOPATH but that didn't change the cover.out file. I also tried manually editing the cover.out file but I can't figure out what paths it actually wants. In the default configuration shown above, running go test runs as expected.

Attempting to fix GOPATH

~ export GOPATH=/home/bill/dev/
~ ln -s /home/bill/dev/server /home/bill/.gvm/gos/go1.2/src

~ go test
cannot find package "code.google.com/p/go.crypto/pbkdf2" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/code.google.com/p/go.crypto/pbkdf2 (from $GOROOT)
/home/bill/dev/src/code.google.com/p/go.crypto/pbkdf2 (from $GOPATH)
../util/log.go:4:2: cannot find package "github.com/kr/pretty" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/github.com/kr/pretty (from $GOROOT)
/home/bill/dev/src/github.com/kr/pretty (from $GOPATH)

These are additional dependencies that I previously downloaded using go get. They end up in /home/bill/.gvm/pkgsets/go1.2/global/src which the $GOPATH used to point to. So I changed GOPATH

~ export GOPATH=/home/bill/dev/:/home/bill/.gvm/pkgsets/go1.2/global

So that the tests run again, but the cover.out file still has the same directories in it and still gives me the same error.

答案1

得分: 12

这是获取GVM所有优势的方法,而不会破坏你理想的Go开发环境,如这里所描述,并且不需要使用笨拙的特殊符号链接技巧。

假设我已经按照标准设置了我的开发环境在~/go目录下(所以我的github上的包foo将在~/go/github.com/gepoch/foo中)。

首先,我们将创建一个特殊的pkgset,它将愉快地引用我们的开发环境。只需运行:

$ gvm pkgset create dev

这将添加pkgset。接下来,我们可以对它放置go路径的位置进行一些自定义。运行:

$ gvm pkgenv dev

你应该看到你喜欢的文本编辑器弹出,并显示一堆环境变量定义。只需更改GOPATH条目以包含你的开发根目录!例如,我将这个:

export GOPATH; GOPATH="$GVM_ROOT/pkgsets/go1.2/dev"

更改为:

export GOPATH; GOPATH="$GVM_ROOT/pkgsets/go1.2/dev:$HOME/go"

此外,通过更改以下内容,让gvm正确设置你的路径:

export PATH; PATH="${GVM_ROOT}/pkgsets/go1.2/global/bin:${GVM_ROOT}/gos/go1.2/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}"

更改为:

export PATH; PATH="${GVM_ROOT}/pkgsets/go1.2/global/bin:${GVM_ROOT}/gos/go1.2/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}:$HOME/go/bin"

重新启动终端,就完成了!每当你运行$ gvm pkgset use dev,你都可以轻松访问你的开发环境。

这意味着(除了其他很多功能)以下命令将按预期工作:

$ go test -coverprofile=coverage.out github.com/gepoch/foo
$ go tool cover -html=coverage.out

你可以将此添加到任何你想要的pkgset环境中,以便轻松访问开发树。

英文:

Here's the way to get all of the advantages of GVM without having to ruin your ideal go development environment as described here, and without having to resort to clunky special-case symlink hacks.

Suppose I've set all of my development up according to the standard in ~/go (so package foo in my github would be in ~/go/github.com/gepoch/foo)

First of all, we're going to make a special-use pkgset that will happily reference our development environment. Simply run:

$ gvm pkgset create dev

This will add the pkgset. Next, we can do some customization on where exactly it puts the go path. Run:

$ gvm pkgenv dev

You should see your favorite text editor pop open with a bunch of environment variable definitions. Simply change the GOPATH entry to include your dev root! For example, I change this:

export GOPATH; GOPATH="$GVM_ROOT/pkgsets/go1.2/dev"

Into this:

export GOPATH; GOPATH="$GVM_ROOT/pkgsets/go1.2/dev:$HOME/go"

Additionally, have gvm set up your path correctly by changing this:

export PATH; PATH="${GVM_ROOT}/pkgsets/go1.2/global/bin:${GVM_ROOT}/gos/go1.2/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}"

into this:

export PATH; PATH="${GVM_ROOT}/pkgsets/go1.2/global/bin:${GVM_ROOT}/gos/go1.2/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}:$HOME/go/bin"

Restart your terminal, and that's it! Whenever you run $ gvm pkgset use dev you'll have easy access to your dev environment.

That means (among many other things) that this works as intended:

$ go test -coverprofile=coverage.out github.com/gepoch/foo
$ go tool cover -html=coverage.out

You can add this to any pkgset environment that you wish, for easy access to the dev tree.

答案2

得分: 10

我一个月前遇到了同样的问题,通过以下步骤解决了它。

我的包名叫做 alpaca

我的工作目录(代码)是 /home/pksunkara/coding/alpaca

$ gvm use go1.2
现在使用的是 go1.2 版本

$ echo $GOPATH
/usr/local/lib/gvm/pkgsets/go1.2/global

$ echo $GOROOT
/usr/local/lib/gvm/gos/go1.2

为了解决这个问题,我做了以下操作:

$ mkdir -p $GOPATH/src/github.com/pksunkara
$ ln -s /home/pksunkara/coding/alpaca $GOPATH/src/github.com/pksunkara/alpaca

基本上,我需要将当前工作文件夹链接到 $GOPATH/src 文件夹中,alpaca 的结果包路径变为 github.com/pksunkara/alpaca

现在,go test 和 cover 的工作方式如下:

$ go test -coverprofile=coverage.out github.com/pksunkara/alpaca
$ go tool cover -html=coverage.out

这很重要

我为了解决这个问题尝试了很多次。我尝试了包括你尝试过的所有方法。通过阅读关于 golang 代码组织的文章,我理解了这个问题,这对于每个使用 go 的人来说都是必读的。

在 golang 中,代码组织非常重要。

包路径对于 golang 来说很重要。在 golang 中,永远不要使用本地路径进行导入。虽然它们可以工作,但不推荐这样做。

假设你的包名是 model。你可以简单地将 model 目录链接到 $GOPATH/src/model,然后你将得到一个名为 model 的包路径,可以使用 import "model" 进行导入。但为了避免冲突,go 建议使用更长的包路径名。

我建议你将其链接到 $GOPATH/src/bill.com/server/model,并使用 import "bill.com/server/model" 进行导入。对于 ./query./util,你也可以类似地操作。

如果你还有疑问,请提出来。我会尽力解释更多。

英文:

I had the same problem a month ago. I solved it by using the following steps.

My package name is called alpaca

My working directory (code) is /home/pksunkara/coding/alpaca

$ gvm use go1.2
Now using version go1.2

$ echo $GOPATH
/usr/local/lib/gvm/pkgsets/go1.2/global

$ echo $GOROOT
/usr/local/lib/gvm/gos/go1.2

To fix the issue, I did this

$ mkdir -p $GOPATH/src/github.com/pksunkara
$ ln -s /home/pksunkara/coding/alpaca $GOPATH/src/github.com/pksunkara/alpaca

Basically I have to link the current working folder into the $GOPATH/src folder and the resultant package path for alpaca became github.com/pksunkara/alpaca.

Now, the go test & cover works as following

$ go test -coverprofile=coverage.out github.com/pksunkara/alpaca
$ go tool cover -html=coverage.out

THIS IS IMPORTANT

I stumbled a lot to fix this. I have attempted all kind of things including the ones you attempted. I understood the problem by reading about code organization in golang which should be a must read for everyone working with go.

The code organization mentioned here is very important to work with golang.

Package paths are important for golang. And you should never use local path when importing in golang. They will work but it is not recommended.

Let's assume your package name is model. You can simply link the model directory to $GOPATH/src/model and then you will have a package path named model which you can import using import "model". But to avoid collisions, go recommends using a bigger package path name.

I would recommend you to link it to $GOPATH/src/bill.com/server/model and import it as import "bill.com/server/model". Similarily with ./query and ./util you have.

If you still have doubts, please ask. I will try to explain more.

答案3

得分: 2

你是否尝试在gvm的开发者网站上提交了一个问题?https://github.com/moovweb/gvm(我不确定这是否是主要的网站)

英文:

Have you try to put a issue in gvm's developer site? https://github.com/moovweb/gvm (I'm not sure is this the major site)

答案4

得分: 0

请检查由gvm设置的$GOPATH的值。

我建议您手动设置$GOPATH进行测试,将其设置为/home/bill(使用符号链接src->dev),只是为了查看go test -cover是否会生成包含正确文件路径的cover.out文件。

英文:

Double check the value of $GOPATH, as set by gvm.

I would try setting $GOPATH manually just for testing to /home/bill (with a symlink src->dev), just to see if a go test -cover produces files a cover.out with the correct file path in it.

huangapple
  • 本文由 发表于 2014年1月17日 07:06:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/21174766.html
匿名

发表评论

匿名网友

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

确定