Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory
Browse files Browse the repository at this point in the history
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 <engzerjun@gmail.com>
  • Loading branch information
Juneezee authored and sio4 committed Apr 11, 2022
1 parent 32f6994 commit 2ff8b3f
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 24 deletions.
3 changes: 2 additions & 1 deletion dialect_sqlite.go
Expand Up @@ -169,10 +169,11 @@ 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()

log(logging.Info, "created database '%s'", m.ConnectionDetails.Database)
return nil
Expand Down
10 changes: 2 additions & 8 deletions dialect_sqlite_test.go
Expand Up @@ -4,8 +4,6 @@ package pop

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -133,10 +131,8 @@ func Test_ConnectionDetails_Finalize_SQLite_OverrideOptions_Synonym_Path(t *test

func Test_ConnectionDetails_FinalizeOSPath(t *testing.T) {
r := require.New(t)
d, err := ioutil.TempDir("", "")
r.NoError(err)
d := t.TempDir()
p := filepath.Join(d, "testdb.sqlite")
defer os.RemoveAll(p)
cd := &ConnectionDetails{
Dialect: "sqlite",
Database: p,
Expand All @@ -148,10 +144,8 @@ func Test_ConnectionDetails_FinalizeOSPath(t *testing.T) {

func TestSqlite_CreateDB(t *testing.T) {
r := require.New(t)
dir, err := ioutil.TempDir("", "")
r.NoError(err)
dir := t.TempDir()
p := filepath.Join(dir, "testdb.sqlite")
defer os.RemoveAll(p)
cd := &ConnectionDetails{
Dialect: "sqlite",
Database: p,
Expand Down
5 changes: 1 addition & 4 deletions soda/cmd/generate/config_cmd_test.go
@@ -1,7 +1,6 @@
package generate

import (
"io/ioutil"
"os"
"testing"

Expand All @@ -13,9 +12,7 @@ func Test_ConfigCmd_NoArg(t *testing.T) {
c := ConfigCmd
c.SetArgs([]string{})

tdir, err := ioutil.TempDir("", "testapp")
r.NoError(err)
defer os.RemoveAll(tdir)
tdir := t.TempDir()

pwd, err := os.Getwd()
r.NoError(err)
Expand Down
5 changes: 1 addition & 4 deletions soda/cmd/generate/fizz_cmd_test.go
@@ -1,7 +1,6 @@
package generate

import (
"io/ioutil"
"os"
"testing"

Expand All @@ -13,9 +12,7 @@ func Test_FizzCmd_NoArg(t *testing.T) {
c := FizzCmd
c.SetArgs([]string{})

tdir, err := ioutil.TempDir("", "testapp")
r.NoError(err)
defer os.RemoveAll(tdir)
tdir := t.TempDir()

pwd, err := os.Getwd()
r.NoError(err)
Expand Down
9 changes: 2 additions & 7 deletions soda/cmd/generate/model_cmd_test.go
@@ -1,7 +1,6 @@
package generate

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -14,9 +13,7 @@ func Test_ModelCmd_NoArg(t *testing.T) {
c := ModelCmd
c.SetArgs([]string{})

tdir, err := ioutil.TempDir("", "testapp")
r.NoError(err)
defer os.RemoveAll(tdir)
tdir := t.TempDir()

pwd, err := os.Getwd()
r.NoError(err)
Expand All @@ -32,9 +29,7 @@ func Test_ModelCmd_NameOnly(t *testing.T) {
c := ModelCmd
c.SetArgs([]string{"users"})

tdir, err := ioutil.TempDir("", "testapp")
r.NoError(err)
defer os.RemoveAll(tdir)
tdir := t.TempDir()

pwd, err := os.Getwd()
r.NoError(err)
Expand Down

0 comments on commit 2ff8b3f

Please sign in to comment.