[go-nuts] Re: how do I use go test for code that needs a flag defined in flag package

2023-05-07 Thread Brian Candler
Since the CLI parsing sets global variables which affect how your code operates, you can just set those variables directly in your test functions. func TestWithDots(t *testing.T) { noDotsFlag = false ... } func TestWithNoDots(t *testing.T) { noDotsFlag = true ... } On Saturday,

[go-nuts] Re: how do I use go test for code that needs a flag defined in flag package

2023-05-06 Thread Robert Solomon
thank you On Saturday, May 6, 2023 at 4:32:57 PM UTC-4 Jason Phillips wrote: > The testing package documentation mentions this case explicitly: > https://pkg.go.dev/testing#hdr-Main > > In short, you can define a main function (called TestMain) for your test > package and call flag.Parse from

[go-nuts] Re: how do I use go test for code that needs a flag defined in flag package

2023-05-06 Thread Jason Phillips
The testing package documentation mentions this case explicitly: https://pkg.go.dev/testing#hdr-Main In short, you can define a main function (called TestMain) for your test package and call flag.Parse from there. On Saturday, May 6, 2023 at 4:03:59 PM UTC-4 Robert Solomon wrote: > A related

[go-nuts] Re: how do I use go test for code that needs a flag defined in flag package

2023-05-06 Thread Robert Solomon
A related issue: Now that I can run go test and it's running, how do I define the flag. Currently, I define it in main(), and also the flag.Parse() is in main(). But main() is not run in go test, so I tried putting flag.BoolVar() and flag.Parse() in an init() function, but that doesn''t work

[go-nuts] Re: how do I use go test for code that needs a flag defined in flag package

2023-05-06 Thread Robert Solomon
Looks like I didn't copy everything I have to the playground. But I do have the correct imports and the top statement, package main. Thank you for answering, that's working. --rob solomon On Saturday, May 6, 2023 at 11:24:11 AM UTC-4 Brian Candler wrote: > Your code is incomplete (it's

[go-nuts] Re: how do I use go test for code that needs a flag defined in flag package

2023-05-06 Thread Brian Candler
Your code is incomplete (it's missing all the imports), and "go test" doesn't run your main() function anyway, so it's not going to parse the flags. But otherwise I think it would be something like this: (single dash, -args not --args) go test . -v -args - -dots