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

Ignore variables in test packages/files #11

Closed
kaskavalci opened this issue Mar 27, 2019 · 10 comments
Closed

Ignore variables in test packages/files #11

kaskavalci opened this issue Mar 27, 2019 · 10 comments

Comments

@kaskavalci
Copy link

It is possible to initialize test variables by TestMain. However global variables are the only way to share data with TestMain and tests. Can we ignore global variables in _test packages or _test.go files in general?

@leighmcculloch
Copy link
Owner

That can cause side effects to leak from one test to the next though. Could you share an example of when a global variable is required for tests instead of a function that returns a setup to be shared across tests?

@GregoryVds
Copy link

Facing the same issue here. My setup is expensive to prepare (loading a BPF program into the kernel). I want to perform this setup step only once and then reuse it across several tests. So I basically get a file descriptor in TestMain that I want to share with my test cases.

@leonklingele
Copy link
Contributor

gochecknoglobals lints tests only when a -t flag is provided. Why not run it without the flag?
Otherwise (*testing.T).Run might be of help.

@GregoryVds
Copy link

GregoryVds commented May 30, 2019

Thanks for the suggestion leon, I didn't know about this flag. I am actually using golangci-lint to run gochecknoglobals and it looks like it does not expose the -t flag. Anyway, I ended up using an exclude-rule to turn off gochecknoglobals on the single test file that was problematic, so all good now.

@bombsimon
Copy link
Contributor

A bit late to the party but golangci-lint has it's own flag for this but defaults to true. Have a look at --tests=false or --skip-files=.*_tests

@leighmcculloch
Copy link
Owner

It's also worth noting golangci-lint ports this linter into its code, so it does not actually call this gochecknoglobals implementation and it's implementation is based on an older version of this tool. There's an issue discussing that here golangci/golangci-lint#1393.

@leighmcculloch
Copy link
Owner

I think the two recommendations above not using the -t flag, or using golangci-lint's --tests flag, solve the issue posted, so I am closing. Please reopen if this issue is not addressed or I am missing something.

@bbkane
Copy link

bbkane commented Jan 2, 2023

I'm a bit late to this party, but I'm using the "golden files" pattern ( https://speakerdeck.com/mitchellh/advanced-testing-with-go?slide=19 ) for my tests (see my actual code here). I really want to add gochecknoglobals for all code (tests included) in golangci-lint, but I'm not sure how to restructure my code to "unglobal" the update flag or have gochecknoglobals ignore this one specific flag in just this one test file. Any ideas? From this discussion it looks my options are either to:

  • stop linting this file completely
  • refactor my code (but I can't seem to see a way how)

Any ideas? And thanks for this tool, I love this idea!

@leighmcculloch
Copy link
Owner

leighmcculloch commented Jan 2, 2023

@bbkane There's a couple options I'm aware of. Others might chime in with additional ideas. Here's what I know:

You can use //nolint:gochecknoglobals. (I haven't used this myself, so I think I have the syntax right, but you might need to fiddle. I've seen it before here: https://golangci-lint.run/usage/false-positives/).

Another option is to put that var update = into its own file, and then stop linting that one file. You only need to put the definition into its own file, not the uses of the variable.

@bbkane
Copy link

bbkane commented Jan 2, 2023

That seems to work, thank you!

//nolint:gochecknoglobals  // https://github.com/leighmcculloch/gochecknoglobals/issues/11#issuecomment-1368578131
var update = stdlibflag.Bool("update", false, "update golden files")

Although if you (or anyone) hear about any techniques to write an update testing flag without using this package level variable trick, please update this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants