github.com/dannyzhou2015/migrate/v4@v4.15.2/source/pkger/pkger_test.go (about) 1 package pkger 2 3 import ( 4 "errors" 5 "os" 6 "testing" 7 8 st "github.com/dannyzhou2015/migrate/v4/source/testing" 9 "github.com/gobuffalo/here" 10 "github.com/markbates/pkger" 11 "github.com/markbates/pkger/pkging" 12 "github.com/markbates/pkger/pkging/mem" 13 ) 14 15 func Test(t *testing.T) { 16 t.Run("WithInstance", func(t *testing.T) { 17 i := testInstance(t) 18 19 createPkgerFile(t, i, "/1_foobar.up.sql") 20 createPkgerFile(t, i, "/1_foobar.down.sql") 21 createPkgerFile(t, i, "/3_foobar.up.sql") 22 createPkgerFile(t, i, "/4_foobar.up.sql") 23 createPkgerFile(t, i, "/4_foobar.down.sql") 24 createPkgerFile(t, i, "/5_foobar.down.sql") 25 createPkgerFile(t, i, "/7_foobar.up.sql") 26 createPkgerFile(t, i, "/7_foobar.down.sql") 27 28 d, err := WithInstance(i, "/") 29 if err != nil { 30 t.Fatal(err) 31 } 32 33 st.Test(t, d) 34 }) 35 36 t.Run("Open", func(t *testing.T) { 37 i := testInstance(t) 38 39 createPkgerFile(t, i, "/1_foobar.up.sql") 40 createPkgerFile(t, i, "/1_foobar.down.sql") 41 createPkgerFile(t, i, "/3_foobar.up.sql") 42 createPkgerFile(t, i, "/4_foobar.up.sql") 43 createPkgerFile(t, i, "/4_foobar.down.sql") 44 createPkgerFile(t, i, "/5_foobar.down.sql") 45 createPkgerFile(t, i, "/7_foobar.up.sql") 46 createPkgerFile(t, i, "/7_foobar.down.sql") 47 48 registerPackageLevelInstance(t, i) 49 50 d, err := (&Pkger{}).Open("pkger:///") 51 if err != nil { 52 t.Fatal(err) 53 } 54 55 st.Test(t, d) 56 }) 57 58 } 59 60 func TestWithInstance(t *testing.T) { 61 t.Run("Subdir", func(t *testing.T) { 62 i := testInstance(t) 63 64 // Make sure the relative root exists so that httpfs.PartialDriver can 65 // initialize. 66 createPkgerSubdir(t, i, "/subdir") 67 68 _, err := WithInstance(i, "/subdir") 69 if err != nil { 70 t.Fatal("") 71 } 72 }) 73 74 t.Run("NilInstance", func(t *testing.T) { 75 _, err := WithInstance(nil, "") 76 if err == nil { 77 t.Fatal(err) 78 } 79 }) 80 81 t.Run("FailInit", func(t *testing.T) { 82 i := testInstance(t) 83 84 _, err := WithInstance(i, "/fail") 85 if err == nil { 86 t.Fatal(err) 87 } 88 }) 89 90 t.Run("FailWithoutMigrations", func(t *testing.T) { 91 i := testInstance(t) 92 93 createPkgerSubdir(t, i, "/") 94 95 d, err := WithInstance(i, "/") 96 if err != nil { 97 t.Fatal(err) 98 } 99 100 if _, err := d.First(); !errors.Is(err, os.ErrNotExist) { 101 t.Fatal(err) 102 } 103 104 }) 105 } 106 107 func TestOpen(t *testing.T) { 108 109 t.Run("InvalidURL", func(t *testing.T) { 110 _, err := (&Pkger{}).Open(":///") 111 if err == nil { 112 t.Fatal(err) 113 } 114 }) 115 116 t.Run("Root", func(t *testing.T) { 117 _, err := (&Pkger{}).Open("pkger:///") 118 if err != nil { 119 t.Fatal(err) 120 } 121 }) 122 123 t.Run("FailInit", func(t *testing.T) { 124 _, err := (&Pkger{}).Open("pkger:///subdir") 125 if err == nil { 126 t.Fatal(err) 127 } 128 }) 129 130 i := testInstance(t) 131 createPkgerSubdir(t, i, "/subdir") 132 133 // Note that this registers the instance globally so anything run after 134 // this will have access to everything container in the registered 135 // instance. 136 registerPackageLevelInstance(t, i) 137 138 t.Run("Subdir", func(t *testing.T) { 139 _, err := (&Pkger{}).Open("pkger:///subdir") 140 if err != nil { 141 t.Fatal(err) 142 } 143 }) 144 } 145 146 func TestClose(t *testing.T) { 147 d, err := (&Pkger{}).Open("pkger:///") 148 if err != nil { 149 t.Fatal(err) 150 } 151 if err := d.Close(); err != nil { 152 t.Fatal(err) 153 } 154 } 155 156 func registerPackageLevelInstance(t *testing.T, pkg pkging.Pkger) { 157 if err := pkger.Apply(pkg, nil); err != nil { 158 t.Fatalf("failed to register pkger instance: %v\n", err) 159 } 160 } 161 162 func testInstance(t *testing.T) pkging.Pkger { 163 pkg, err := inMemoryPkger() 164 if err != nil { 165 t.Fatalf("failed to create an pkging.Pkger instance: %v\n", err) 166 } 167 168 return pkg 169 } 170 171 func createPkgerSubdir(t *testing.T, pkg pkging.Pkger, subdir string) { 172 if err := pkg.MkdirAll(subdir, os.ModePerm); err != nil { 173 t.Fatalf("failed to create pkger subdir %q: %v\n", subdir, err) 174 } 175 } 176 177 func createPkgerFile(t *testing.T, pkg pkging.Pkger, name string) { 178 _, err := pkg.Create(name) 179 if err != nil { 180 t.Fatalf("failed to create pkger file %q: %v\n", name, err) 181 } 182 } 183 184 func inMemoryPkger() (*mem.Pkger, error) { 185 info, err := here.New().Current() 186 if err != nil { 187 return nil, err 188 } 189 190 pkg, err := mem.New(info) 191 if err != nil { 192 return nil, err 193 } 194 195 return pkg, nil 196 }