-
Notifications
You must be signed in to change notification settings - Fork 343
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
#151
Conversation
This commit replaces `ioutil.TempDir` 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 `ioutil.TempDir` 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 <engzerjun@gmail.com>
Codecov Report
@@ Coverage Diff @@
## main #151 +/- ##
==========================================
+ Coverage 56.89% 56.90% +0.01%
==========================================
Files 86 86
Lines 7887 7882 -5
==========================================
- Hits 4487 4485 -2
+ Misses 3006 3004 -2
+ Partials 394 393 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a very good suggestion!
/lgtm
/approve
@@ -805,8 +804,7 @@ func TestCgroupResourceReconcile_calculateResources(t *testing.T) { | |||
assert.NoError(t, err) | |||
defer func() { stop <- struct{}{} }() | |||
|
|||
helper := system.NewFileTestUtil(t) | |||
defer helper.Cleanup() | |||
_ = system.NewFileTestUtil(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we remove the equation?
|
||
helper := system.NewFileTestUtil(t) | ||
defer helper.Cleanup() | ||
_ = system.NewFileTestUtil(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we remove the equation
func NewFileTestUtil(t *testing.T) *FileTestUtil { | ||
tempDir, err := ioutil.TempDir("/tmp", "koordlet_test") | ||
tempDir := t.TempDir() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems (*FileTestUtil).Cleanup()
is useless with this patch and its references are removed.
Can we remove the Cleanup()
since its work would be done implicitly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
NewFileTestUtil now uses t.TempDir() to create temporary directory so manual cleanup is no longer necessary. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: hormes, jasonliu747, saintube The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
@Juneezee Hi, when I ran Did it ever happen to you? |
@jasonliu747 No, I don't get this error on my Linux machine. This is probably a Mac specific issue, see https://unix.stackexchange.com/a/367012/376279 |
Could you try to fix this? I believe majority of programmer are using Mac for development. |
@jasonliu747 PR #156 fixes the issue. I think the only way to tell Another way is to use |
Ⅰ. Describe what this PR does
A testing cleanup.
This pull request replaces
ioutil.TempDir
witht.TempDir
. We can use theT.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 saves us at least 2 lines (error check, and cleanup) on every instance, or in some cases adds cleanup that we forgot.
Reference: https://pkg.go.dev/testing#T.TempDir
Ⅱ. Does this pull request fix one issue?
NONE
Ⅲ. Describe how to verify it
All tests should continue to pass. No leftover temporary directories when the tests finish.
Ⅳ. Special notes for reviews