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

Closed
wants to merge 3 commits into from

Conversation

sfc-gh-igarish
Copy link
Collaborator

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

Description

Please explain the changes you made here.

Checklist

  • Code compiles correctly
  • Run make fmt to fix inconsistent formats
  • Run make lint to get lint errors and fix all of them
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary

@sfc-gh-igarish sfc-gh-igarish requested a review from a team as a code owner June 8, 2023 18:00
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 <[email protected]>
This commit fixes the 3 failing tests on Windows [1]
	1. TestEncryptDecryptFilePadding
	2. TestEncryptDecryptLargeFile
	3. TestPutGetFile

They all failed with the same reason:
  testing.go:1097: TempDir RemoveAll cleanup: remove ... The process cannot access the file because it is being used by another process.

Windows handle open files differently [2] so we need to close all open
file descriptors explicitly.

[1]: https://github.com/snowflakedb/gosnowflake/actions/runs/5214296899/jobs/9410395563?pr=647
[2]: golang/go#50510 (comment)

Signed-off-by: Eng Zer Jun <[email protected]>
@codecov
Copy link

codecov bot commented Oct 16, 2023

Codecov Report

Merging #817 (62fc5c0) into master (dfb1c18) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #817   +/-   ##
=======================================
  Coverage   83.23%   83.24%           
=======================================
  Files          49       49           
  Lines        9287     9290    +3     
=======================================
+ Hits         7730     7733    +3     
  Misses       1557     1557           
Files Coverage Δ
encrypt_util.go 73.73% <100.00%> (+0.36%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@sfc-gh-pfus
Copy link
Collaborator

Closing as merged original PR.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 16, 2023
@sfc-gh-pfus sfc-gh-pfus deleted the test_pr_647 branch October 16, 2023 07:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants