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

proposal: cmd/go: Add a build tag for tests #52600

Open
misha-ridge opened this issue Apr 28, 2022 · 3 comments
Open

proposal: cmd/go: Add a build tag for tests #52600

misha-ridge opened this issue Apr 28, 2022 · 3 comments
Labels
Projects
Milestone

Comments

@misha-ridge
Copy link

@misha-ridge misha-ridge commented Apr 28, 2022

This is a proposal to add a build tag during compilation of unit tests.

Background & motivation

Sometimes it is handy to have some code not available for unit testing, e.g. some volatile data that should not be relied upon in tests. Code reviews help to catch accidental or intentional misuses, but it's exhausting to check that no test code refers to this data directly or indirectly.

There are workarounds that allow distinguishing if the code is running tests, but they are inadequate:

  • Checking presence of "test.v" flag does not work for code running from global initializers or package-level init() clauses.
  • Opening currently running executable to match some string that is present only in internals of testing package may produce false positives, slow, and OS-dependent.
  • Manually specifying build tags for testing (likely via shell wrappers) forces every developer of the project to learn that go test is not the right tool to run tests, and creates toil for integrating with IDEs.

Proposal

Add a build tag that is available during compilation of unit tests. This will trivially allow marking some data/functions as "not available in unit tests".

@gopherbot gopherbot added this to the Proposal milestone Apr 28, 2022
@misha-ridge
Copy link
Author

@misha-ridge misha-ridge commented Apr 28, 2022

flag.Lookup("test.v") idiom is seen in the wild: https://github.com/search?q=flag.Lookup%28%22test.v%22%29&type=code

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals May 4, 2022
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 4, 2022

This may be a dup of #14668.

@misha-ridge
Copy link
Author

@misha-ridge misha-ridge commented May 5, 2022

It is. New information in this ticket:

  • a new legitimate use-case
  • survey of existing codebases that shows that inadequate workarounds do get applied

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

No branches or pull requests

3 participants