-
Notifications
You must be signed in to change notification settings - Fork 616
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
test: use T.TempDir
to create temporary test directory
#3159
Conversation
Hi @Juneezee, thanks a lot for submitting this change. I see that the target branch is "master", could you please open this PR against "dev"? |
Hi @Juneezee -- again sorry to have taken so long to get around to this. I see that the windows integration tests failed in the last test run. Unfortunately we'll need you to push an update to rebase against the latest dev to retrigger the tests. Would you be able to do this. If the tests continue to fail, I can share the failure logs with you and help in the debugging effort. |
Hi @fierlion, thanks for looking into this PR. I've rebased onto |
Rebased onto |
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `os.MkdirTemp` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <[email protected]>
Summary
A testing cleanup. We can use the
T.TempDir
function from thetesting
package to create temporary directory. The directory created byT.TempDir
is automatically removed when the test and all its subtests complete.This PR also refactor several cleanup logic by using
t.Cleanup
instead ofdefer
.Reference: https://pkg.go.dev/testing#T.TempDir
Reference: https://pkg.go.dev/testing#T.Cleanup
Implementation details
ioutil.TempDir
witht.TempDir
newTestDataClient(t *testing.T)
no longer returns acleanup func()
. It now usest.Cleanup
to handle the cleanup when the test finishes.Testing
make test
andmake run-integ-tests
on Linux machine.New tests cover the changes: yes
Description for the changelog
Enhancement - Use
T.TempDir
to create temporary test directoryLicensing
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.