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

[bug report] flag provided but not defined: -test.v #46869

Closed
fwhezfwhez opened this issue Jun 22, 2021 · 8 comments
Closed

[bug report] flag provided but not defined: -test.v #46869

fwhezfwhez opened this issue Jun 22, 2021 · 8 comments

Comments

@fwhezfwhez
Copy link

fwhezfwhez commented Jun 22, 2021

Go 1.13.15 has a bug for package flag. Code below can help reproduce it.

config/init.go

package config

import "flag"
var mode string
func init() {

	flag.StringVar(&mode, "mode", "local", "go run main.go -mode 'local'")
	flag.Parse()
}

config/init_test.go

package config

import (
	"fmt"
	"testing"
)

func TestDefault(t *testing.T) {
	fmt.Println(mode)
}

output
go 1.12.17 ok

=== RUN   TestDefault
local
--- PASS: TestDefault (0.00s)
PASS

go 1.13.15

flag provided but not defined: -test.v
Usage of C:\Users\DELL\AppData\Local\Temp\___TestDefault_in_libgamedataapisrv_config.exe:
  -mode string
    	go run main.go -profPort ':6060' (default "local")
@the-moment
Copy link

still hava this in 1.16.3

@davecheney
Copy link
Contributor

What is the exact command you ran?

@davecheney
Copy link
Contributor

Sorry, you cannot call flag.Parse in init because it conflicts with the flags that are passed to the test binary. Move flag.parse to your main function

@agnivade
Copy link
Contributor

What Dave said.

This is expected and documented: https://golang.org/doc/go1.13#testing, #31859.

Closing.

@the-moment
Copy link

What is the exact command you ran?

What Dave said.

This is expected and documented: https://golang.org/doc/go1.13#testing, #31859.

Closing.

I saw this question,but when will solve this problem.or should I notice that question in any program?

@the-moment
Copy link

What is the exact command you ran?

go test -v -run + myfun

@fwhezfwhez
Copy link
Author

@the-moment
Write flag.Parse in main.go, whatever main.go init Func or main.go beginning. It's ok. And don't write flag.Parse in any init Func of other package.

By the way, testing function will auto call flag.Parse(). Style below is ok

package config

import "flag"
var mode string
func init() {
	flag.StringVar(&mode, "mode", "local", "go run main.go -mode 'local'")
}
package config

import (
	"fmt"
	"testing"
)

func TestDefault(t *testing.T) {
	fmt.Println(mode)
}
go test -run ^TestDefault$ -mode hehe

Output

hehe

As it suggests, hehe is output correctly even if I didn't call flag.Parse yet. It, flag.Parse, seems just called inside.

@davecheney
Copy link
Contributor

Thats about it. Because flag.Parse operates on global state it must only be called once. The problem is both your package and, when compiled as a test, the testing package both define and parse flags, there is no way to make it work with flag.Parse called from init. Either:

  • flag.Parse is called from your package, in which case the flags defined in the testing package haven't been defined yet so yo get a weird message about flags you didn't define causing the test process to fail.
  • or, the testing package's flag.Parse is called and your flags aren't registered so your test blows up because the flags you were expecting aren't defined and cannot be used.

The root problem is the global flag.Parse singleton, but sadly that ship has long sailed. You can work around this by doing one or more of the following:

  • Defining your own flag set
  • calling flag.Parse in your main function, and no sooner.

ethitter added a commit to ethitter/eth-log-alerting that referenced this issue Feb 26, 2022
@golang golang locked and limited conversation to collaborators Jun 22, 2022
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

5 participants