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