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

cmd/go: a program that does not invoke testing package features has test flags added into CLI flags #21141

Closed
HouzuoGuo opened this issue Jul 24, 2017 · 3 comments

Comments

@HouzuoGuo
Copy link

HouzuoGuo commented Jul 24, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.8.3

What operating system and processor architecture are you using (go env)?

Linux on x86-64

Code sample

package main

import (
	"testing"
	"flag"
)

func main() {
	println("hi")
	flag.Parse()
}

func a(t *testing.T) {
}

Observe that, once compiled, test flags are added into the executable:

> go build -o main
> ./main -help   
hi                        
Usage of ./main:     
  -test.bench regexp      
        run only benchmarks matching regexp
  -test.benchmem
        print memory allocations for benchmarks
......
......

What did you expect to see?

The function a uses testing package, however, since the function is dead code to the entire program, the testing package features are never utilised and test flags should never have been compiled into the executable.

@cznic
Copy link
Contributor

cznic commented Jul 24, 2017

The init function in package testing is not dead code. Working as intended.

@mvdan
Copy link
Member

mvdan commented Jul 24, 2017

See @cznic's answer.

@mvdan mvdan closed this as completed Jul 24, 2017
@HouzuoGuo
Copy link
Author

For future readers: you can work around this issue by making a stub interface definition that works just like a testing.T: https://github.com/HouzuoGuo/laitos/blob/master/testingstub/testingstub.go

@golang golang locked and limited conversation to collaborators Jul 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants