Getting the expected pwd instead of the generated pwd
Closed this issue · 7 comments
In my testing code, I use relative paths to pull in fixtures (non-go files). This however doesn't work because there is a new, temporary path being generated such as:
/var/folders/qy/zmrxj1t14mnbxsxrz9gtknxc0000gn/T/gocov672324733/src/pkg/github.com/imosquera/uploadthis/conf
for gocov to work. Whats the best way to get the original file path?
I would suggest using the go/build package to locate the package, and read sources from there. Something like:
import "go/build"
...
pkg, err := build.Import("github.com/imosquera/uploadthis", "", build.FindOnly)
...
Then you can use pkg.Dir
to construct your paths. Will this work for your tests?
Nope, I still get the same error:
Panic: can't open config fileopen /var/folders/qy/zmrxj1t14mnbxsxrz9gtknxc0000gn/T/withmock126237784/path/src/github.com/imosquera/uploadthis/fixtures/sample-config.yaml: no such file or directory (PC=0x14CA1)
I tried by using the GOPATH but I realized that had changed as well during the gocov testing.
Perhaps when gocov moves the gopath it should move ALL files in the package wether they're .go files or not since the code may depend on them.
What do you think?
Or perhaps just an environment variable or an exported variable somewhere that gives me the original GOPATH gocov changes it.
Ah, okay, I see the problem.
Gocov purposely does not copy subdirectories. If it did, then that directory would get picked up and mask the one in the original $GOPATH. In your case it doesn't matter, but in others it does. You could just copy all the originals over, but that's expensive.
Try this: pkg, err := build.Import("github.com/imosquera/uploadthis/fixtures", "", build.FindOnly)
Since you're passing "FindOnly", it doesn't care that there's no Go files in it.
Ok that worked! thanks again.
One more question. This might be out of your control, but thought I'd ask incase you can quickly help:
I'm using gocov with withmock (http://godoc.org/github.com/qur/withmock) and it comes back with this error:
cannot find package "github.com/imosquera/uploadthis/fixtures" in any of:
/var/folders/qy/zmrxj1t14mnbxsxrz9gtknxc0000gn/T/gocov761928724/src/pkg/github.com/imosquera/uploadthis/fixtures (from $GOROOT)
/var/folders/qy/zmrxj1t14mnbxsxrz9gtknxc0000gn/T/withmock190339487/path/src/github.com/imosquera/uploadthis/fixtures (from $GOPATH)
any quick fixes?
Ok that worked! thanks again.
Cool. You're welcome. I'll close this now that the original issue is resolved.
I'm using gocov with withmock (http://godoc.org/github.com/qur/withmock) and it comes back with this error:
...
any quick fixes?
Sorry, I have never used withmock, so I'm not sure what that error is about.