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

Merged
merged 1 commit into from Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 2 additions & 20 deletions database/ql/ql_test.go
Expand Up @@ -3,8 +3,6 @@ package ql
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -15,15 +13,7 @@ import (
)

func Test(t *testing.T) {
dir, err := ioutil.TempDir("", "ql-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "ql.db"))
p := &Ql{}
addr := fmt.Sprintf("ql://%s", filepath.Join(dir, "ql.db"))
Expand All @@ -45,15 +35,7 @@ func Test(t *testing.T) {
}

func TestMigrate(t *testing.T) {
dir, err := ioutil.TempDir("", "ql-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "ql.db"))

db, err := sql.Open("ql", filepath.Join(dir, "ql.db"))
Expand Down
54 changes: 6 additions & 48 deletions database/sqlcipher/sqlcipher_test.go
Expand Up @@ -3,8 +3,6 @@ package sqlcipher
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -17,15 +15,7 @@ import (
)

func Test(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s", filepath.Join(dir, "sqlite3.db"))
Expand All @@ -37,15 +27,7 @@ func Test(t *testing.T) {
}

func TestMigrate(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))

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

func TestMigrationTable(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test-migration-table")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()

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

Expand Down Expand Up @@ -120,15 +94,7 @@ func TestMigrationTable(t *testing.T) {
}

func TestNoTxWrap(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=true", filepath.Join(dir, "sqlite3.db"))
Expand All @@ -142,19 +108,11 @@ func TestNoTxWrap(t *testing.T) {
}

func TestNoTxWrapInvalidValue(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=yeppers", filepath.Join(dir, "sqlite3.db"))
_, err = p.Open(addr)
_, err := p.Open(addr)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "x-no-tx-wrap")
assert.Contains(t, err.Error(), "invalid syntax")
Expand Down
64 changes: 7 additions & 57 deletions database/sqlite/sqlite_test.go
Expand Up @@ -3,8 +3,6 @@ package sqlite
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -17,15 +15,7 @@ import (
)

func Test(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite://%s", filepath.Join(dir, "sqlite.db"))
Expand All @@ -37,15 +27,7 @@ func Test(t *testing.T) {
}

func TestMigrate(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite.db"))

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

func TestMigrationTable(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite-driver-test-migration-table")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()

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

Expand Down Expand Up @@ -120,15 +94,7 @@ func TestMigrationTable(t *testing.T) {
}

func TestNoTxWrap(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite://%s?x-no-tx-wrap=true", filepath.Join(dir, "sqlite.db"))
Expand All @@ -142,35 +108,19 @@ func TestNoTxWrap(t *testing.T) {
}

func TestNoTxWrapInvalidValue(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite://%s?x-no-tx-wrap=yeppers", filepath.Join(dir, "sqlite.db"))
_, err = p.Open(addr)
_, err := p.Open(addr)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "x-no-tx-wrap")
assert.Contains(t, err.Error(), "invalid syntax")
}
}

func TestMigrateWithDirectoryNameContainsWhitespaces(t *testing.T) {
dir, err := ioutil.TempDir("", "directory name contains whitespaces")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
dbPath := filepath.Join(dir, "sqlite.db")
t.Logf("DB path : %s\n", dbPath)
p := &Sqlite{}
Expand Down
64 changes: 7 additions & 57 deletions database/sqlite3/sqlite3_test.go
Expand Up @@ -3,8 +3,6 @@ package sqlite3
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -17,15 +15,7 @@ import (
)

func Test(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s", filepath.Join(dir, "sqlite3.db"))
Expand All @@ -37,15 +27,7 @@ func Test(t *testing.T) {
}

func TestMigrate(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))

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

func TestMigrationTable(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test-migration-table")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()

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

Expand Down Expand Up @@ -120,15 +94,7 @@ func TestMigrationTable(t *testing.T) {
}

func TestNoTxWrap(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=true", filepath.Join(dir, "sqlite3.db"))
Expand All @@ -142,35 +108,19 @@ func TestNoTxWrap(t *testing.T) {
}

func TestNoTxWrapInvalidValue(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=yeppers", filepath.Join(dir, "sqlite3.db"))
_, err = p.Open(addr)
_, err := p.Open(addr)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "x-no-tx-wrap")
assert.Contains(t, err.Error(), "invalid syntax")
}
}

func TestMigrateWithDirectoryNameContainsWhitespaces(t *testing.T) {
dir, err := ioutil.TempDir("", "directory name contains whitespaces")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
dir := t.TempDir()
dbPath := filepath.Join(dir, "sqlite3.db")
t.Logf("DB path : %s\n", dbPath)
p := &Sqlite{}
Expand Down