Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: Reuse of exported var name when using . import panics #10942

Closed
anacrolix opened this issue May 24, 2015 · 2 comments

Comments

Projects
None yet
3 participants
@anacrolix
Copy link
Contributor

commented May 24, 2015

In examples_test.go:

package torrent_test
import torrent "."

In the the rest of the go files in the directory:

package torrent

anacrolix@Matts-MBP:~/gopath/src/github.com/anacrolix/torrent$ go test .
2015/05/24 21:27:41 expvar.go:254: Reuse of exported var name: unusedDownloadedChunksCount
panic: Reuse of exported var name: unusedDownloadedChunksCount

goroutine 1 [running]:
log.Panicln(0xc20804bee8, 0x2, 0x2)
/Users/anacrolix/src/go/src/log/log.go:334 +0xc4
expvar.Publish(0x46139d0, 0x1b, 0x4979770, 0xc20800b4b0)
/Users/anacrolix/src/go/src/expvar/expvar.go:254 +0x236
expvar.NewInt(0x46139d0, 0x1b, 0x4831ec8)
/Users/anacrolix/src/go/src/expvar/expvar.go:272 +0x78
_/Users/anacrolix/gopath/src/github.com/anacrolix/torrent.init()
/Users/anacrolix/gopath/src/github.com/anacrolix/torrent/client.go:46 +0x112
github.com/anacrolix/torrent_test.init()
/Users/anacrolix/gopath/src/github.com/anacrolix/torrent/example_test.go:18 +0x47
main.init()
github.com/anacrolix/torrent/_test/_testmain.go:102 +0x56

The "." import seems to cause expvar to init those variables twice?

@minux minux closed this May 24, 2015

@minux

This comment has been minimized.

Copy link
Member

commented May 24, 2015

It's working as intended.

The generated main package will import "torrent",
but the test file imports the same package under
a different import path ("."), as Go uses import
paths to differentiate packages, one package imported
under two import paths will be treated as two different
packages and thus it's init will be called twice.

That's also the reason why symlinks are not allowed
in GOPATH.

Do not use "." to import a package in a test.

A simple example:
// t.go
package t

var A int

func init() {
println("init called")
}

// t_test.go
package t_test

import (
t "."
"testing"
)

var _ = t.A

func TestA(t *testing.T) {}

$ go test -v
init called
init called
=== RUN TestA
--- PASS: TestA (0.00s)
PASS
ok t 0.002s

@mikioh mikioh changed the title Reuse of exported var name when using . import panics testing: Reuse of exported var name when using . import panics May 26, 2015

@anacrolix

This comment has been minimized.

@golang golang locked and limited conversation to collaborators Jun 25, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.