github.com/dannyzhou2015/migrate/v4@v4.15.2/source/file/file_test.go (about) 1 package file 2 3 import ( 4 "errors" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path" 9 "path/filepath" 10 "testing" 11 12 st "github.com/dannyzhou2015/migrate/v4/source/testing" 13 ) 14 15 func Test(t *testing.T) { 16 tmpDir := t.TempDir() 17 18 // write files that meet driver test requirements 19 mustWriteFile(t, tmpDir, "1_foobar.up.sql", "1 up") 20 mustWriteFile(t, tmpDir, "1_foobar.down.sql", "1 down") 21 22 mustWriteFile(t, tmpDir, "3_foobar.up.sql", "3 up") 23 24 mustWriteFile(t, tmpDir, "4_foobar.up.sql", "4 up") 25 mustWriteFile(t, tmpDir, "4_foobar.down.sql", "4 down") 26 27 mustWriteFile(t, tmpDir, "5_foobar.down.sql", "5 down") 28 29 mustWriteFile(t, tmpDir, "7_foobar.up.sql", "7 up") 30 mustWriteFile(t, tmpDir, "7_foobar.down.sql", "7 down") 31 32 f := &File{} 33 d, err := f.Open("file://" + tmpDir) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 st.Test(t, d) 39 } 40 41 func TestOpen(t *testing.T) { 42 tmpDir := t.TempDir() 43 44 mustWriteFile(t, tmpDir, "1_foobar.up.sql", "") 45 mustWriteFile(t, tmpDir, "1_foobar.down.sql", "") 46 47 if !filepath.IsAbs(tmpDir) { 48 t.Fatal("expected tmpDir to be absolute path") 49 } 50 51 f := &File{} 52 _, err := f.Open("file://" + tmpDir) // absolute path 53 if err != nil { 54 t.Fatal(err) 55 } 56 } 57 58 func TestOpenWithRelativePath(t *testing.T) { 59 tmpDir := t.TempDir() 60 61 wd, err := os.Getwd() 62 if err != nil { 63 t.Fatal(err) 64 } 65 defer func() { 66 // rescue working dir after we are done 67 if err := os.Chdir(wd); err != nil { 68 t.Log(err) 69 } 70 }() 71 72 if err := os.Chdir(tmpDir); err != nil { 73 t.Fatal(err) 74 } 75 76 if err := os.Mkdir(filepath.Join(tmpDir, "foo"), os.ModePerm); err != nil { 77 t.Fatal(err) 78 } 79 80 mustWriteFile(t, filepath.Join(tmpDir, "foo"), "1_foobar.up.sql", "") 81 82 f := &File{} 83 84 // dir: foo 85 d, err := f.Open("file://foo") 86 if err != nil { 87 t.Fatal(err) 88 } 89 _, err = d.First() 90 if err != nil { 91 t.Fatalf("expected first file in working dir %v for foo", tmpDir) 92 } 93 94 // dir: ./foo 95 d, err = f.Open("file://./foo") 96 if err != nil { 97 t.Fatal(err) 98 } 99 _, err = d.First() 100 if err != nil { 101 t.Fatalf("expected first file in working dir %v for ./foo", tmpDir) 102 } 103 } 104 105 func TestOpenDefaultsToCurrentDirectory(t *testing.T) { 106 wd, err := os.Getwd() 107 if err != nil { 108 t.Fatal(err) 109 } 110 111 f := &File{} 112 d, err := f.Open("file://") 113 if err != nil { 114 t.Fatal(err) 115 } 116 117 if d.(*File).path != wd { 118 t.Fatal("expected driver to default to current directory") 119 } 120 } 121 122 func TestOpenWithDuplicateVersion(t *testing.T) { 123 tmpDir := t.TempDir() 124 125 mustWriteFile(t, tmpDir, "1_foo.up.sql", "") // 1 up 126 mustWriteFile(t, tmpDir, "1_bar.up.sql", "") // 1 up 127 128 f := &File{} 129 _, err := f.Open("file://" + tmpDir) 130 if err == nil { 131 t.Fatal("expected err") 132 } 133 } 134 135 func TestClose(t *testing.T) { 136 tmpDir := t.TempDir() 137 138 f := &File{} 139 d, err := f.Open("file://" + tmpDir) 140 if err != nil { 141 t.Fatal(err) 142 } 143 144 if d.Close() != nil { 145 t.Fatal("expected nil") 146 } 147 } 148 149 func mustWriteFile(t testing.TB, dir, file string, body string) { 150 if err := ioutil.WriteFile(path.Join(dir, file), []byte(body), 06444); err != nil { 151 t.Fatal(err) 152 } 153 } 154 155 func mustCreateBenchmarkDir(t *testing.B) (dir string) { 156 tmpDir := t.TempDir() 157 158 for i := 0; i < 1000; i++ { 159 mustWriteFile(t, tmpDir, fmt.Sprintf("%v_foobar.up.sql", i), "") 160 mustWriteFile(t, tmpDir, fmt.Sprintf("%v_foobar.down.sql", i), "") 161 } 162 163 return tmpDir 164 } 165 166 func BenchmarkOpen(b *testing.B) { 167 dir := mustCreateBenchmarkDir(b) 168 defer func() { 169 if err := os.RemoveAll(dir); err != nil { 170 b.Error(err) 171 } 172 }() 173 b.ResetTimer() 174 for n := 0; n < b.N; n++ { 175 f := &File{} 176 _, err := f.Open("file://" + dir) 177 if err != nil { 178 b.Error(err) 179 } 180 } 181 b.StopTimer() 182 } 183 184 func BenchmarkNext(b *testing.B) { 185 dir := mustCreateBenchmarkDir(b) 186 defer func() { 187 if err := os.RemoveAll(dir); err != nil { 188 b.Error(err) 189 } 190 }() 191 f := &File{} 192 d, _ := f.Open("file://" + dir) 193 b.ResetTimer() 194 v, err := d.First() 195 for n := 0; n < b.N; n++ { 196 for !errors.Is(err, os.ErrNotExist) { 197 v, err = d.Next(v) 198 } 199 } 200 b.StopTimer() 201 }