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

Merged
merged 1 commit into from
Apr 11, 2022
Merged

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

merged 1 commit into from
Apr 11, 2022

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Apr 10, 2022

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.

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


This PR also introduces a minor change to the sqlite dialect. The file descriptor is now properly closed in CreateDB().

func (m *sqlite) CreateDB() error {
 	if err != nil {
 		return fmt.Errorf("could not create SQLite database '%s': %w", m.ConnectionDetails.Database, err)
 	}
-	_, err = os.Create(m.ConnectionDetails.Database)
+	f, err := os.Create(m.ConnectionDetails.Database)
 	if err != nil {
 		return fmt.Errorf("could not create SQLite database '%s': %w", m.ConnectionDetails.Database, err)
 	}
+	_ = f.Close()

This is required after we use t.TempDir to create the temporary directory in the test TestSqlite_CreateDB. Otherwise the test will fail on Windows.

--- FAIL: TestSqlite_CreateDB (0.00s)
    testing.go:958: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestSqlite_CreateDB3832827053\001\testdb.sqlite: The process cannot access the file because it is being used by another process.

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]>
Copy link
Member

@sio4 sio4 left a comment

Choose a reason for hiding this comment

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

LGTM. Could you please update the PR description with mentioning change on sqlite dialect? I hope the PR description would contain everything about what PR contains so we can easily check that in the future when we need to check the history.

@sio4 sio4 requested review from stanislas-m and a team April 10, 2022 12:31
@fasmat
Copy link
Member

fasmat commented Apr 11, 2022

Thanks for your contribution! Looks good to me, I'm always happy to get rid of *util packages from our imports.

@sio4 sio4 merged commit 2ff8b3f into gobuffalo:development Apr 11, 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