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

Merged
merged 1 commit into from
Apr 29, 2022
Merged

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

merged 1 commit into from
Apr 29, 2022

Conversation

Juneezee
Copy link
Contributor

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.

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(tmp)

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

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 <engzerjun@gmail.com>
@hiddeco hiddeco added area/ci CI related issues and pull requests area/testing Testing related issues and pull requests and removed area/ci CI related issues and pull requests labels Apr 29, 2022
Copy link
Member

@hiddeco hiddeco left a comment

Choose a reason for hiding this comment

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

HAAAA, wanted to do this since I was reminded of it being introduced in Go 1.15, and started using it consistently in newer test but hadn't gotten around changing it yet. Thanks a lot 💯

@hiddeco hiddeco merged commit 89a4e52 into fluxcd:main Apr 29, 2022
@hiddeco
Copy link
Member

hiddeco commented Apr 29, 2022

Feel free to also do this for all other (Flux v2) repositories by the way, if you are bored :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/testing Testing related issues and pull requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants