You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In short: if package is not imported in project, but present in "coverpkg" list, its "init" method will be called. It is not expected that execution behavior depends on coverage options.
package bar
import"fmt"funcinit() {
fmt.Println("bar. <-- here is issue")
}
funcBar() {
fmt.Println("bar Bar.")
}
Build test binary with command:
go test -c -o t main_test.go -coverprofile p.out -covermode atomic --coverpkg github.com/AZavorotnii/go-coverpkg-initissue/foo,github.com/AZavorotnii/go-coverpkg-initissue/bar
What did you expect to see?
Expect "init" calls only from imported packages, as without code coverage:
foo.
main.
main main.
foo Foo.
testing: warning: no tests to run
PASS
What did you see instead?
Init method of "bar" package is executed, even this package is not imported:
foo.
main.
bar. <-- here is issue
main main.
foo Foo.
testing: warning: no tests to run
PASS
coverage: 75.0% of statements in github.com/AZavorotnii/go-coverpkg-initissue/foo, github.com/AZavorotnii/go-coverpkg-initissue/bar
What version of Go are you using (go version)?
go version go1.8.3 darwin/amd64
What operating system and processor architecture are you using (go env)?
rsc
changed the title
x/tools/cmd/cover: method "init" should not be called for non-imported packages
cmd/cover: method "init" should not be called for non-imported packages
Nov 10, 2017
What did you do?
In short: if package is not imported in project, but present in "coverpkg" list, its "init" method will be called. It is not expected that execution behavior depends on coverage options.
In details
I put full example here:
https://github.com/AZavorotnii/go-coverpkg-initissue
Assuming we have file structure:
Where main_test.go imports only foo package:
Where foo.go:
Where bar.go:
Build test binary with command:
What did you expect to see?
Expect "init" calls only from imported packages, as without code coverage:
What did you see instead?
Init method of "bar" package is executed, even this package is not imported:
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?The text was updated successfully, but these errors were encountered: