Get reliable tempfile names from ioutil.TempFile

Also makes them easier to spot (the tests tend to litter /tmp).
This commit is contained in:
Lars Buitinck 2015-06-05 16:02:14 +02:00
parent 542ae647f8
commit cebbf42ff6
2 changed files with 29 additions and 28 deletions

View File

@ -12,7 +12,7 @@ import (
) )
func TestFTS3(t *testing.T) { func TestFTS3(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)

View File

@ -6,15 +6,13 @@
package sqlite3 package sqlite3
import ( import (
"crypto/rand"
"database/sql" "database/sql"
"database/sql/driver" "database/sql/driver"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net/url" "net/url"
"os" "os"
"path/filepath"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -22,15 +20,18 @@ import (
"github.com/mattn/go-sqlite3/sqlite3_test" "github.com/mattn/go-sqlite3/sqlite3_test"
) )
func TempFilename() string { func TempFilename(t *testing.T) string {
randBytes := make([]byte, 16) f, err := ioutil.TempFile("", "go-sqlite3-test-")
rand.Read(randBytes) if err != nil {
return filepath.Join(os.TempDir(), "foo"+hex.EncodeToString(randBytes)+".db") t.Fatal(err)
}
f.Close()
return f.Name()
} }
func doTestOpen(t *testing.T, option string) (string, error) { func doTestOpen(t *testing.T, option string) (string, error) {
var url string var url string
tempFilename := TempFilename() tempFilename := TempFilename(t)
if option != "" { if option != "" {
url = tempFilename + option url = tempFilename + option
} else { } else {
@ -82,7 +83,7 @@ func TestOpen(t *testing.T) {
} }
func TestClose(t *testing.T) { func TestClose(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -108,7 +109,7 @@ func TestClose(t *testing.T) {
} }
func TestInsert(t *testing.T) { func TestInsert(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -147,7 +148,7 @@ func TestInsert(t *testing.T) {
} }
func TestUpdate(t *testing.T) { func TestUpdate(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -212,7 +213,7 @@ func TestUpdate(t *testing.T) {
} }
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -273,7 +274,7 @@ func TestDelete(t *testing.T) {
} }
func TestBooleanRoundtrip(t *testing.T) { func TestBooleanRoundtrip(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -322,7 +323,7 @@ func TestBooleanRoundtrip(t *testing.T) {
} }
func TestTimestamp(t *testing.T) { func TestTimestamp(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -405,7 +406,7 @@ func TestTimestamp(t *testing.T) {
} }
func TestBoolean(t *testing.T) { func TestBoolean(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -497,7 +498,7 @@ func TestBoolean(t *testing.T) {
} }
func TestFloat32(t *testing.T) { func TestFloat32(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -535,7 +536,7 @@ func TestFloat32(t *testing.T) {
} }
func TestNull(t *testing.T) { func TestNull(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -567,7 +568,7 @@ func TestNull(t *testing.T) {
} }
func TestTransaction(t *testing.T) { func TestTransaction(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -627,7 +628,7 @@ func TestTransaction(t *testing.T) {
} }
func TestWAL(t *testing.T) { func TestWAL(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -675,7 +676,7 @@ func TestWAL(t *testing.T) {
func TestTimezoneConversion(t *testing.T) { func TestTimezoneConversion(t *testing.T) {
zones := []string{"UTC", "US/Central", "US/Pacific", "Local"} zones := []string{"UTC", "US/Central", "US/Pacific", "Local"}
for _, tz := range zones { for _, tz := range zones {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename+"?_loc="+url.QueryEscape(tz)) db, err := sql.Open("sqlite3", tempFilename+"?_loc="+url.QueryEscape(tz))
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -781,7 +782,7 @@ func TestSuite(t *testing.T) {
// TODO: Execer & Queryer currently disabled // TODO: Execer & Queryer currently disabled
// https://github.com/mattn/go-sqlite3/issues/82 // https://github.com/mattn/go-sqlite3/issues/82
func TestExecer(t *testing.T) { func TestExecer(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -801,7 +802,7 @@ func TestExecer(t *testing.T) {
} }
func TestQueryer(t *testing.T) { func TestQueryer(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -842,7 +843,7 @@ func TestQueryer(t *testing.T) {
} }
func TestStress(t *testing.T) { func TestStress(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -880,7 +881,7 @@ func TestStress(t *testing.T) {
func TestDateTimeLocal(t *testing.T) { func TestDateTimeLocal(t *testing.T) {
zone := "Asia/Tokyo" zone := "Asia/Tokyo"
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename+"?_loc="+zone) db, err := sql.Open("sqlite3", tempFilename+"?_loc="+zone)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -947,7 +948,7 @@ func TestVersion(t *testing.T) {
} }
func TestNumberNamedParams(t *testing.T) { func TestNumberNamedParams(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -983,7 +984,7 @@ func TestNumberNamedParams(t *testing.T) {
} }
func TestStringContainingZero(t *testing.T) { func TestStringContainingZero(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -1043,7 +1044,7 @@ func (t TimeStamp) Value() (driver.Value, error) {
} }
func TestDateTimeNow(t *testing.T) { func TestDateTimeNow(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)