Skip to content
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 #4256

Merged
merged 1 commit into from
Jul 14, 2022
Merged

test: use T.TempDir to create temporary test directory #4256

merged 1 commit into from
Jul 14, 2022

Conversation

Juneezee
Copy link
Contributor

Summary

A testing cleanup.

This pull request replaces ioutil.TempDir with t.TempDir. We can use the T.TempDir function from the testing package to create temporary directory. The directory created by T.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

func TestFoo(t *testing.T) {
	// before
	tmpDir, err := ioutil.TempDir("", "")
	require.NoError(t, err)
	defer os.RemoveAll(tmpDir)

	// now
	tmpDir := t.TempDir()
}

Test Plan

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>
@CLAassistant
Copy link

CLAassistant commented Jul 13, 2022

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Jul 13, 2022

Codecov Report

Merging #4256 (fac34e5) into master (1335f16) will increase coverage by 0.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #4256      +/-   ##
==========================================
+ Coverage   55.13%   55.14%   +0.01%     
==========================================
  Files         395      395              
  Lines       50117    50117              
==========================================
+ Hits        27630    27638       +8     
+ Misses      20111    20098      -13     
- Partials     2376     2381       +5     
Impacted Files Coverage Δ
ledger/tracker.go 77.82% <0.00%> (-1.31%) ⬇️
ledger/acctonline.go 77.83% <0.00%> (-1.11%) ⬇️
catchup/service.go 69.38% <0.00%> (-0.25%) ⬇️
network/wsPeer.go 68.76% <0.00%> (+0.54%) ⬆️
catchup/peerSelector.go 100.00% <0.00%> (+1.04%) ⬆️
node/node.go 24.31% <0.00%> (+1.93%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1335f16...fac34e5. Read the comment docs.

@Juneezee Juneezee marked this pull request as ready for review July 13, 2022 05:11
@jannotti jannotti self-requested a review July 13, 2022 13:00
Copy link
Contributor

@jannotti jannotti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants