-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: testing: add more support to automatically cleanup temporary resources or temporary changes to the global environment #45403
Comments
As an alternative these functions can be added to an internal package. In #45182 I have proposed Thanks. |
I noted only now that Setenv was already added to the testing package a month ago. Is it possible to send now changes to use the new API or it is better to wait after go 1.17 has been released? |
Most API additions to the |
Yes, but as I wrote |
Adding |
Now that t.Setenv exists, it is OK to send CLs to the Go repo to use t.Setenv if that cleans up a test. |
Given that Setenv and TempDir already exist, it doesn't seem like we need to add ChTempDir (that's just Chdir), nor t.TempFile (that can use os.TempFile with the t.TempDir result). We could consider adding t.Chdir instead of t.ChTempDir, but any test that uses Chdir at all is unparallelizable. Adding t.Chdir to the API may suggest writing tests that don't scale well. I also don't understand why tests would really need to Chdir. It's almost always better to pass the directory to the function that needs it (like setting cmd.Dir in os/exec). So probably t.Chdir is below the bar, especially since you can already use t.Cleanup to un-Chdir. |
This proposal has been added to the active column of the proposals project |
@rsc I agree that functions like ChTempDir or Chdir are not suitable for the testing package. However adding them to an internal package should be fine. The problem is that that package may become something like As for why to chdir in a test, I'm not sure. For some other test it may necessary, as an example: https://github.com/golang/go/blob/master/src/runtime/syscall_windows_test.go#L1046 |
It sounds like the only thing left is t.Chdir and we agree it's not a good idea. |
Fine with me, thanks. However if possible I would like to have all these functions in a custom, unique file, e.g. Currently we have |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
In go 1.15, the
testing
package supports theT.TempDir
function.Currently the method is not used in the tests of the go distribution, but
os.MkdirTemp
is used 164 times.Here is a list of additional API to manage temporary resources or temporary changes to the global environment in tests:
T.ChTempDir
Currently all: tests that change the working directory should use defer to restore it #45182 has introduced the
chdir
function, but there are several tests that use it.os.Chdir
is used 53 times (including inchdir
andchtmpdir
), the newchdir
is used 8 times and the oldchtmpdir
is used 22 times.T.TempFile
.Currently
os.CreateTemp
is used 33 times in tests.T.SetTempEnv
Currently
os.Setenv
is used 108 times.The usage number has been computed with a simple:
grep -F "fname(" **/*_test.go | wc -l
The text was updated successfully, but these errors were encountered: