-
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
all: tests that change the working directory should use defer to restore it #45182
Comments
or t.Cleanup |
Sure, please send a patch. Thanks. |
@ericlagergren, thanks. Using |
By the way, in https://github.com/golang/go/blob/master/src/go/build/build_test.go#L543 there is a similar pattern when changing an environment variable defer os.Setenv("GO111MODULE", os.Getenv("GO111MODULE"))
os.Setenv("GO111MODULE", "off") I have also noted that that many tests calls Thanks. |
|
Ok, thanks. Probably moving to
|
Change https://golang.org/cl/306290 mentions this issue: |
I have noted that a similar pattern is also used in https://github.com/golang/go/blob/master/src/syscall/syscall_linux_test.go#L27, where a Maybe the support |
…ctory Updates #45182 Change-Id: Iaf3bdcc345c72fa9669fdc99908ada4e89904edd Reviewed-on: https://go-review.googlesource.com/c/go/+/306290 Trust: Emmanuel Odeke <[email protected]> Run-TryBot: Emmanuel Odeke <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
Change https://golang.org/cl/307189 mentions this issue: |
Revert CL 306290 changes to TestRemoveAllLongPath. This breaks the test on aix, illumos and solaris. We need to chdir out of startPath before attempting to remove it. Updates #45182 Change-Id: Ic14fa1962d6f2cc83238f6fc2c6932fd9a6e52a1 Reviewed-on: https://go-review.googlesource.com/c/go/+/307189 Trust: Tobias Klauser <[email protected]> Run-TryBot: Tobias Klauser <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]>
In Probably the old function should be removed and the new |
Change https://golang.org/cl/307651 mentions this issue: |
emmm.. This file does not have test function that manually |
For reference, here is a list with all tests using the std
cmd
Probably all these tests chdir to a temporary directory, so chtmpdir is the best API. Thanks to @ianwoolf for the change. |
As I have suggested in https://golang.org/cl/307651, the A possible name is |
As i wrote in https://golang.org/cl/307651, one depsRules of Because of the additional depsRules, it seems that |
It's fine to modify the go/build test when adding a new internal package. That in itself is not a reason to avoid adding a new package. I see that you've opened #45403 to add a function to the testing package. |
Would be really cool to have that feature as part of |
I thought the same thing and opened #62516 |
This change simplifies the test code for changing directories by utilizing t.TempDir() within chtmpdir function. It also replaces os.Exit(1) with t.Fatalf to handle errors more consistently with Go's testing practice. The cleanup process is now managed with t.Cleanup, ensuring that any created temporary directories are removed without the need for a deferred restore function. Updates golang#45182
Change https://go.dev/cl/546217 mentions this issue: |
This change simplifies the test code for changing directories by utilizing t.TempDir() within chtmpdir function. It also replaces os.Exit(1) with t.Fatalf to handle errors more consistently with Go's testing practice. The cleanup process is now managed with t.Cleanup, ensuring that any created temporary directories are removed without the need for a deferred restore function. Updates golang#45182
NOTE we have t.Chdir now, and this is the easiest way to fix the issue. |
What version of Go are you using (
go version
)?I have noted that the tests that need to change the current working directory use the following pattern:
os.Getwd
to get the current working directoryos.Chdir
to change the current working directoryos.Chdir
to restore the original working directoryAn example is:
https://github.com/golang/go/blob/master/src/os/removeall_test.go#L159
The code should probably use
defer
, using a support function like:The new pattern is:
defer chdir(dir)()
This is more readable and ensures that the working directory is restored in case of test failures.
The text was updated successfully, but these errors were encountered: