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 #3500

Merged
merged 3 commits into from
Jul 6, 2022
Merged

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

merged 3 commits into from
Jul 6, 2022

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Jul 5, 2022

Description

A testing cleanup.

This pull request replaces os.MkdirTemp 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 := os.MkdirTemp("", "")
	require.NoError(err)
	defer os.RemoveAll(tmpDir)

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

Type of change

Please delete options that are not relevant.

  • Code refactor or improvement

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version: Linux 5.18.9
  • Hardware: Lenovo ThinkPad L14
  • Toolchain: Go 1.18.3
  • SDK:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

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]>
@codecov
Copy link

codecov bot commented Jul 5, 2022

Codecov Report

Merging #3500 (6c290db) into master (25b402c) will increase coverage by 0.00%.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #3500   +/-   ##
=======================================
  Coverage   75.31%   75.32%           
=======================================
  Files         245      245           
  Lines       22715    22715           
=======================================
+ Hits        17107    17109    +2     
+ Misses       4683     4682    -1     
+ Partials      925      924    -1     
Impacted Files Coverage Δ
db/trie/mptrie/extensionnode.go 92.98% <0.00%> (+1.75%) ⬆️

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 25b402c...6c290db. Read the comment docs.

@Juneezee Juneezee marked this pull request as ready for review July 5, 2022 15:41
@Juneezee Juneezee requested a review from a team as a code owner July 5, 2022 15:41
@Liuhaai
Copy link
Member

Liuhaai commented Jul 5, 2022

good refactoring. thx for contribution

@huof6829
Copy link
Contributor

huof6829 commented Jul 6, 2022

@Juneezee Thanks for your task! 👍👍

@huof6829 huof6829 merged commit 99f0663 into iotexproject:master Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants