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

Merged
merged 2 commits into from
Jan 6, 2023
Merged

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

merged 2 commits into from
Jan 6, 2023

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Jan 3, 2023

What problem does this PR solve?

Issue Number: close #xxx

Problem Summary

What is changed and how does it work?

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.

Reference: https://pkg.go.dev/testing#T.TempDir

func TestFoo(t *testing.T) {
	// before
	tmpDir, err := os.MkdirTemp("", "")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpDir)

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

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

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]>
Signed-off-by: Eng Zer Jun <[email protected]>
@Juneezee Juneezee requested a review from ifplusor January 6, 2023 10:40
Copy link
Collaborator

@ifplusor ifplusor left a comment

Choose a reason for hiding this comment

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

LGTM

@codecov
Copy link

codecov bot commented Jan 6, 2023

Codecov Report

Merging #381 (72f5c9b) into main (09f3748) will decrease coverage by 3.08%.
The diff coverage is 52.83%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #381      +/-   ##
==========================================
- Coverage   62.72%   59.63%   -3.09%     
==========================================
  Files         133      171      +38     
  Lines       12128    14165    +2037     
==========================================
+ Hits         7607     8447     +840     
- Misses       3977     5125    +1148     
- Partials      544      593      +49     
Impacted Files Coverage Δ
internal/controller/eventbus/block/block.go 52.63% <ø> (-3.47%) ⬇️
internal/controller/eventbus/server/instance.go 72.04% <ø> (ø)
internal/controller/trigger/storage/secret.go 48.48% <ø> (ø)
...nternal/controller/trigger/storage/subscription.go 54.71% <ø> (ø)
...ernal/controller/trigger/storage/trigger_worker.go 63.41% <ø> (ø)
internal/gateway/proxy/proxy.go 24.45% <0.00%> (ø)
...rnal/primitive/interceptor/errinterceptor/error.go 0.00% <0.00%> (ø)
.../primitive/interceptor/memberinterceptor/member.go 0.00% <0.00%> (ø)
internal/primitive/vanus/id.go 19.68% <0.00%> (-0.65%) ⬇️
internal/raft/log/recovery.go 52.27% <ø> (ø)
... and 160 more

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 7f990d3...72f5c9b. Read the comment docs.

@ifplusor ifplusor merged commit 58921e5 into vanus-labs:main Jan 6, 2023
wenfengwang pushed a commit that referenced this pull request Mar 23, 2023
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]>
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.

2 participants