test: use T.TempDir to create temporary test directory#151
test: use T.TempDir to create temporary test directory#151koordinator-bot[bot] merged 2 commits intokoordinator-sh:mainfrom Juneezee:test/t.TempDir
T.TempDir to create temporary test directory#151Conversation
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.
|
jasonliu747
left a comment
There was a problem hiding this comment.
This is actually a very good suggestion!
/lgtm
/approve
|
|
||
| helper := system.NewFileTestUtil(t) | ||
| defer helper.Cleanup() | ||
| _ = system.NewFileTestUtil(t) |
|
|
||
| helper := system.NewFileTestUtil(t) | ||
| defer helper.Cleanup() | ||
| _ = system.NewFileTestUtil(t) |
| // NewFileTestUtil creates a new test util for the specified subsystem | ||
| func NewFileTestUtil(t *testing.T) *FileTestUtil { | ||
| tempDir, err := ioutil.TempDir("/tmp", "koordlet_test") | ||
| tempDir := t.TempDir() |
There was a problem hiding this comment.
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?
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>
|
/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 DetailsNeeds 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.TempDirwitht.TempDir. We can use theT.TempDirfunction from thetestingpackage to create temporary directory. The directory created byT.TempDiris 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