Skip to content

Commit 9721a3d

Browse files
Prometheus2677FPiety0521
authored and
FPiety0521
committed
test: use T.TempDir to create temporary test 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 Signed-off-by: Eng Zer Jun <[email protected]>
1 parent bd5e554 commit 9721a3d

File tree

5 files changed

+30
-233
lines changed

5 files changed

+30
-233
lines changed

Diff for: database/ql/ql_test.go

+2-20
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package ql
33
import (
44
"database/sql"
55
"fmt"
6-
"io/ioutil"
7-
"os"
86
"path/filepath"
97
"testing"
108

@@ -15,15 +13,7 @@ import (
1513
)
1614

1715
func Test(t *testing.T) {
18-
dir, err := ioutil.TempDir("", "ql-driver-test")
19-
if err != nil {
20-
return
21-
}
22-
defer func() {
23-
if err := os.RemoveAll(dir); err != nil {
24-
t.Fatal(err)
25-
}
26-
}()
16+
dir := t.TempDir()
2717
t.Logf("DB path : %s\n", filepath.Join(dir, "ql.db"))
2818
p := &Ql{}
2919
addr := fmt.Sprintf("ql://%s", filepath.Join(dir, "ql.db"))
@@ -45,15 +35,7 @@ func Test(t *testing.T) {
4535
}
4636

4737
func TestMigrate(t *testing.T) {
48-
dir, err := ioutil.TempDir("", "ql-driver-test")
49-
if err != nil {
50-
return
51-
}
52-
defer func() {
53-
if err := os.RemoveAll(dir); err != nil {
54-
t.Error(err)
55-
}
56-
}()
38+
dir := t.TempDir()
5739
t.Logf("DB path : %s\n", filepath.Join(dir, "ql.db"))
5840

5941
db, err := sql.Open("ql", filepath.Join(dir, "ql.db"))

Diff for: database/sqlcipher/sqlcipher_test.go

+6-48
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package sqlcipher
33
import (
44
"database/sql"
55
"fmt"
6-
"io/ioutil"
7-
"os"
86
"path/filepath"
97
"testing"
108

@@ -17,15 +15,7 @@ import (
1715
)
1816

1917
func Test(t *testing.T) {
20-
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
21-
if err != nil {
22-
return
23-
}
24-
defer func() {
25-
if err := os.RemoveAll(dir); err != nil {
26-
t.Error(err)
27-
}
28-
}()
18+
dir := t.TempDir()
2919
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
3020
p := &Sqlite{}
3121
addr := fmt.Sprintf("sqlite3://%s", filepath.Join(dir, "sqlite3.db"))
@@ -37,15 +27,7 @@ func Test(t *testing.T) {
3727
}
3828

3929
func TestMigrate(t *testing.T) {
40-
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
41-
if err != nil {
42-
return
43-
}
44-
defer func() {
45-
if err := os.RemoveAll(dir); err != nil {
46-
t.Error(err)
47-
}
48-
}()
30+
dir := t.TempDir()
4931
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
5032

5133
db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db"))
@@ -72,15 +54,7 @@ func TestMigrate(t *testing.T) {
7254
}
7355

7456
func TestMigrationTable(t *testing.T) {
75-
dir, err := ioutil.TempDir("", "sqlite3-driver-test-migration-table")
76-
if err != nil {
77-
return
78-
}
79-
defer func() {
80-
if err := os.RemoveAll(dir); err != nil {
81-
t.Error(err)
82-
}
83-
}()
57+
dir := t.TempDir()
8458

8559
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
8660

@@ -120,15 +94,7 @@ func TestMigrationTable(t *testing.T) {
12094
}
12195

12296
func TestNoTxWrap(t *testing.T) {
123-
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
124-
if err != nil {
125-
return
126-
}
127-
defer func() {
128-
if err := os.RemoveAll(dir); err != nil {
129-
t.Error(err)
130-
}
131-
}()
97+
dir := t.TempDir()
13298
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
13399
p := &Sqlite{}
134100
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=true", filepath.Join(dir, "sqlite3.db"))
@@ -142,19 +108,11 @@ func TestNoTxWrap(t *testing.T) {
142108
}
143109

144110
func TestNoTxWrapInvalidValue(t *testing.T) {
145-
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
146-
if err != nil {
147-
return
148-
}
149-
defer func() {
150-
if err := os.RemoveAll(dir); err != nil {
151-
t.Error(err)
152-
}
153-
}()
111+
dir := t.TempDir()
154112
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
155113
p := &Sqlite{}
156114
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=yeppers", filepath.Join(dir, "sqlite3.db"))
157-
_, err = p.Open(addr)
115+
_, err := p.Open(addr)
158116
if assert.Error(t, err) {
159117
assert.Contains(t, err.Error(), "x-no-tx-wrap")
160118
assert.Contains(t, err.Error(), "invalid syntax")

Diff for: database/sqlite/sqlite_test.go

+7-57
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package sqlite
33
import (
44
"database/sql"
55
"fmt"
6-
"io/ioutil"
7-
"os"
86
"path/filepath"
97
"testing"
108

@@ -17,15 +15,7 @@ import (
1715
)
1816

1917
func Test(t *testing.T) {
20-
dir, err := ioutil.TempDir("", "sqlite-driver-test")
21-
if err != nil {
22-
return
23-
}
24-
defer func() {
25-
if err := os.RemoveAll(dir); err != nil {
26-
t.Error(err)
27-
}
28-
}()
18+
dir := t.TempDir()
2919
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite.db"))
3020
p := &Sqlite{}
3121
addr := fmt.Sprintf("sqlite://%s", filepath.Join(dir, "sqlite.db"))
@@ -37,15 +27,7 @@ func Test(t *testing.T) {
3727
}
3828

3929
func TestMigrate(t *testing.T) {
40-
dir, err := ioutil.TempDir("", "sqlite-driver-test")
41-
if err != nil {
42-
return
43-
}
44-
defer func() {
45-
if err := os.RemoveAll(dir); err != nil {
46-
t.Error(err)
47-
}
48-
}()
30+
dir := t.TempDir()
4931
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite.db"))
5032

5133
db, err := sql.Open("sqlite", filepath.Join(dir, "sqlite.db"))
@@ -72,15 +54,7 @@ func TestMigrate(t *testing.T) {
7254
}
7355

7456
func TestMigrationTable(t *testing.T) {
75-
dir, err := ioutil.TempDir("", "sqlite-driver-test-migration-table")
76-
if err != nil {
77-
return
78-
}
79-
defer func() {
80-
if err := os.RemoveAll(dir); err != nil {
81-
t.Error(err)
82-
}
83-
}()
57+
dir := t.TempDir()
8458

8559
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite.db"))
8660

@@ -120,15 +94,7 @@ func TestMigrationTable(t *testing.T) {
12094
}
12195

12296
func TestNoTxWrap(t *testing.T) {
123-
dir, err := ioutil.TempDir("", "sqlite-driver-test")
124-
if err != nil {
125-
return
126-
}
127-
defer func() {
128-
if err := os.RemoveAll(dir); err != nil {
129-
t.Error(err)
130-
}
131-
}()
97+
dir := t.TempDir()
13298
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite.db"))
13399
p := &Sqlite{}
134100
addr := fmt.Sprintf("sqlite://%s?x-no-tx-wrap=true", filepath.Join(dir, "sqlite.db"))
@@ -142,35 +108,19 @@ func TestNoTxWrap(t *testing.T) {
142108
}
143109

144110
func TestNoTxWrapInvalidValue(t *testing.T) {
145-
dir, err := ioutil.TempDir("", "sqlite-driver-test")
146-
if err != nil {
147-
return
148-
}
149-
defer func() {
150-
if err := os.RemoveAll(dir); err != nil {
151-
t.Error(err)
152-
}
153-
}()
111+
dir := t.TempDir()
154112
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite.db"))
155113
p := &Sqlite{}
156114
addr := fmt.Sprintf("sqlite://%s?x-no-tx-wrap=yeppers", filepath.Join(dir, "sqlite.db"))
157-
_, err = p.Open(addr)
115+
_, err := p.Open(addr)
158116
if assert.Error(t, err) {
159117
assert.Contains(t, err.Error(), "x-no-tx-wrap")
160118
assert.Contains(t, err.Error(), "invalid syntax")
161119
}
162120
}
163121

164122
func TestMigrateWithDirectoryNameContainsWhitespaces(t *testing.T) {
165-
dir, err := ioutil.TempDir("", "directory name contains whitespaces")
166-
if err != nil {
167-
return
168-
}
169-
defer func() {
170-
if err := os.RemoveAll(dir); err != nil {
171-
t.Error(err)
172-
}
173-
}()
123+
dir := t.TempDir()
174124
dbPath := filepath.Join(dir, "sqlite.db")
175125
t.Logf("DB path : %s\n", dbPath)
176126
p := &Sqlite{}

Diff for: database/sqlite3/sqlite3_test.go

+7-57
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package sqlite3
33
import (
44
"database/sql"
55
"fmt"
6-
"io/ioutil"
7-
"os"
86
"path/filepath"
97
"testing"
108

@@ -17,15 +15,7 @@ import (
1715
)
1816

1917
func Test(t *testing.T) {
20-
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
21-
if err != nil {
22-
return
23-
}
24-
defer func() {
25-
if err := os.RemoveAll(dir); err != nil {
26-
t.Error(err)
27-
}
28-
}()
18+
dir := t.TempDir()
2919
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
3020
p := &Sqlite{}
3121
addr := fmt.Sprintf("sqlite3://%s", filepath.Join(dir, "sqlite3.db"))
@@ -37,15 +27,7 @@ func Test(t *testing.T) {
3727
}
3828

3929
func TestMigrate(t *testing.T) {
40-
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
41-
if err != nil {
42-
return
43-
}
44-
defer func() {
45-
if err := os.RemoveAll(dir); err != nil {
46-
t.Error(err)
47-
}
48-
}()
30+
dir := t.TempDir()
4931
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
5032

5133
db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db"))
@@ -72,15 +54,7 @@ func TestMigrate(t *testing.T) {
7254
}
7355

7456
func TestMigrationTable(t *testing.T) {
75-
dir, err := ioutil.TempDir("", "sqlite3-driver-test-migration-table")
76-
if err != nil {
77-
return
78-
}
79-
defer func() {
80-
if err := os.RemoveAll(dir); err != nil {
81-
t.Error(err)
82-
}
83-
}()
57+
dir := t.TempDir()
8458

8559
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
8660

@@ -120,15 +94,7 @@ func TestMigrationTable(t *testing.T) {
12094
}
12195

12296
func TestNoTxWrap(t *testing.T) {
123-
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
124-
if err != nil {
125-
return
126-
}
127-
defer func() {
128-
if err := os.RemoveAll(dir); err != nil {
129-
t.Error(err)
130-
}
131-
}()
97+
dir := t.TempDir()
13298
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
13399
p := &Sqlite{}
134100
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=true", filepath.Join(dir, "sqlite3.db"))
@@ -142,35 +108,19 @@ func TestNoTxWrap(t *testing.T) {
142108
}
143109

144110
func TestNoTxWrapInvalidValue(t *testing.T) {
145-
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
146-
if err != nil {
147-
return
148-
}
149-
defer func() {
150-
if err := os.RemoveAll(dir); err != nil {
151-
t.Error(err)
152-
}
153-
}()
111+
dir := t.TempDir()
154112
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
155113
p := &Sqlite{}
156114
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=yeppers", filepath.Join(dir, "sqlite3.db"))
157-
_, err = p.Open(addr)
115+
_, err := p.Open(addr)
158116
if assert.Error(t, err) {
159117
assert.Contains(t, err.Error(), "x-no-tx-wrap")
160118
assert.Contains(t, err.Error(), "invalid syntax")
161119
}
162120
}
163121

164122
func TestMigrateWithDirectoryNameContainsWhitespaces(t *testing.T) {
165-
dir, err := ioutil.TempDir("", "directory name contains whitespaces")
166-
if err != nil {
167-
return
168-
}
169-
defer func() {
170-
if err := os.RemoveAll(dir); err != nil {
171-
t.Error(err)
172-
}
173-
}()
123+
dir := t.TempDir()
174124
dbPath := filepath.Join(dir, "sqlite3.db")
175125
t.Logf("DB path : %s\n", dbPath)
176126
p := &Sqlite{}

0 commit comments

Comments
 (0)