2017-02-08 06:01:29 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2017-03-03 20:02:32 +00:00
|
|
|
"path/filepath"
|
2017-02-08 06:01:29 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-10-10 22:11:48 +00:00
|
|
|
st "github.com/golang-migrate/migrate/v4/source/testing"
|
2017-02-08 06:01:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-04-26 22:47:16 +00:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-02-08 06:01:29 +00:00
|
|
|
|
|
|
|
// write files that meet driver test requirements
|
|
|
|
mustWriteFile(t, tmpDir, "1_foobar.up.sql", "1 up")
|
|
|
|
mustWriteFile(t, tmpDir, "1_foobar.down.sql", "1 down")
|
|
|
|
|
|
|
|
mustWriteFile(t, tmpDir, "3_foobar.up.sql", "3 up")
|
|
|
|
|
|
|
|
mustWriteFile(t, tmpDir, "4_foobar.up.sql", "4 up")
|
|
|
|
mustWriteFile(t, tmpDir, "4_foobar.down.sql", "4 down")
|
|
|
|
|
|
|
|
mustWriteFile(t, tmpDir, "5_foobar.down.sql", "5 down")
|
|
|
|
|
|
|
|
mustWriteFile(t, tmpDir, "7_foobar.up.sql", "7 up")
|
|
|
|
mustWriteFile(t, tmpDir, "7_foobar.down.sql", "7 down")
|
|
|
|
|
|
|
|
f := &File{}
|
|
|
|
d, err := f.Open("file://" + tmpDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
st.Test(t, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpen(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "TestOpen")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-04-26 22:47:16 +00:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-02-08 06:01:29 +00:00
|
|
|
|
|
|
|
mustWriteFile(t, tmpDir, "1_foobar.up.sql", "")
|
|
|
|
mustWriteFile(t, tmpDir, "1_foobar.down.sql", "")
|
|
|
|
|
2017-03-03 20:02:32 +00:00
|
|
|
if !filepath.IsAbs(tmpDir) {
|
|
|
|
t.Fatal("expected tmpDir to be absolute path")
|
|
|
|
}
|
|
|
|
|
2017-02-08 06:01:29 +00:00
|
|
|
f := &File{}
|
2017-03-03 20:02:32 +00:00
|
|
|
_, err = f.Open("file://" + tmpDir) // absolute path
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenWithRelativePath(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "TestOpen")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-04-26 22:47:16 +00:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-03-03 20:02:32 +00:00
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-04-26 22:47:16 +00:00
|
|
|
defer func() {
|
|
|
|
// rescue working dir after we are done
|
|
|
|
if err := os.Chdir(wd); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-03-03 20:02:32 +00:00
|
|
|
|
|
|
|
if err := os.Chdir(tmpDir); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Mkdir(filepath.Join(tmpDir, "foo"), os.ModePerm); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mustWriteFile(t, filepath.Join(tmpDir, "foo"), "1_foobar.up.sql", "")
|
|
|
|
|
|
|
|
f := &File{}
|
|
|
|
|
|
|
|
// dir: foo
|
|
|
|
d, err := f.Open("file://foo")
|
2017-02-08 06:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-03 20:02:32 +00:00
|
|
|
_, err = d.First()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected first file in working dir %v for foo", tmpDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dir: ./foo
|
|
|
|
d, err = f.Open("file://./foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = d.First()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected first file in working dir %v for ./foo", tmpDir)
|
|
|
|
}
|
2017-02-08 06:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenDefaultsToCurrentDirectory(t *testing.T) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f := &File{}
|
|
|
|
d, err := f.Open("file://")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.(*File).path != wd {
|
|
|
|
t.Fatal("expected driver to default to current directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenWithDuplicateVersion(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "TestOpenWithDuplicateVersion")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-04-26 22:47:16 +00:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-02-08 06:01:29 +00:00
|
|
|
|
|
|
|
mustWriteFile(t, tmpDir, "1_foo.up.sql", "") // 1 up
|
|
|
|
mustWriteFile(t, tmpDir, "1_bar.up.sql", "") // 1 up
|
|
|
|
|
|
|
|
f := &File{}
|
|
|
|
_, err = f.Open("file://" + tmpDir)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected err")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "TestOpen")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-04-26 22:47:16 +00:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-02-08 06:01:29 +00:00
|
|
|
|
|
|
|
f := &File{}
|
|
|
|
d, err := f.Open("file://" + tmpDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Close() != nil {
|
|
|
|
t.Fatal("expected nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustWriteFile(t testing.TB, dir, file string, body string) {
|
|
|
|
if err := ioutil.WriteFile(path.Join(dir, file), []byte(body), 06444); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustCreateBenchmarkDir(t *testing.B) (dir string) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "Benchmark")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
mustWriteFile(t, tmpDir, fmt.Sprintf("%v_foobar.up.sql", i), "")
|
|
|
|
mustWriteFile(t, tmpDir, fmt.Sprintf("%v_foobar.down.sql", i), "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkOpen(b *testing.B) {
|
|
|
|
dir := mustCreateBenchmarkDir(b)
|
2019-04-26 22:47:16 +00:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-02-08 06:01:29 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
f := &File{}
|
2019-04-26 22:47:16 +00:00
|
|
|
_, err := f.Open("file://" + dir)
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
2017-02-08 06:01:29 +00:00
|
|
|
}
|
|
|
|
b.StopTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkNext(b *testing.B) {
|
|
|
|
dir := mustCreateBenchmarkDir(b)
|
2019-04-26 22:47:16 +00:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-02-08 06:01:29 +00:00
|
|
|
f := &File{}
|
|
|
|
d, _ := f.Open("file://" + dir)
|
|
|
|
b.ResetTimer()
|
|
|
|
v, err := d.First()
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
for !os.IsNotExist(err) {
|
|
|
|
v, err = d.Next(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.StopTimer()
|
|
|
|
}
|