-
Notifications
You must be signed in to change notification settings - Fork 123
/
common_test.go
482 lines (388 loc) · 9.5 KB
/
common_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package gitbase
import (
"context"
"io"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/src-d/go-borges"
"github.com/src-d/go-borges/libraries"
"github.com/src-d/go-borges/plain"
"github.com/src-d/go-borges/siva"
fixtures "github.com/src-d/go-git-fixtures"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/plan"
"github.com/stretchr/testify/require"
"gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-billy.v4/memfs"
"gopkg.in/src-d/go-billy.v4/osfs"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
)
type CleanupFunc func()
func setup(t *testing.T) (*sql.Context, string, CleanupFunc) {
require := require.New(t)
t.Helper()
fxs := []*fixtures.Fixture{fixtures.ByTag("worktree").One()}
ctx, paths, cleanup := buildSession(t, fxs)
require.Len(paths, 1)
return ctx, paths[0], cleanup
}
func setupRepos(t *testing.T) (*sql.Context, []string, CleanupFunc) {
t.Helper()
return buildSession(t, fixtures.ByTag("worktree"))
}
func buildSession(
t *testing.T,
repos fixtures.Fixtures,
) (ctx *sql.Context, paths []string, cleanup CleanupFunc) {
require := require.New(t)
t.Helper()
lib, err := newMultiLibrary()
require.NoError(err)
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
for _, fixture := range repos {
path := fixture.Worktree().Root()
ok, err := IsGitRepo(path)
require.NoError(err)
if ok {
name := strings.TrimLeft(path, string(os.PathSeparator))
if err := lib.AddPlain(name, path, nil); err == nil {
r, err := pool.GetRepo(name)
require.NoError(err)
paths = append(paths, pathToName(path))
require.NoError(r.Close())
}
}
}
cleanup = func() {
t.Helper()
require.NoError(fixtures.Clean())
}
session := NewSession(pool)
ctx = sql.NewContext(context.TODO(), sql.WithSession(session))
return ctx, paths, cleanup
}
func tableToRows(ctx *sql.Context, t sql.Table) ([]sql.Row, error) {
return sql.NodeToRows(ctx, plan.NewResolvedTable(t))
}
/*
The following code adds utilities to test that siva files are properly closed.
Instead of using normal setup you can use setupSivaCloseRepos, it returns
a context with a pool with all the sivas in "_testdata" directory. It also
tracks all siva filesystems opened. Its closed state can be checked with
closedLib.Check().
*/
type closedRepository struct {
borges.Repository
closed bool
}
func (c *closedRepository) Close() error {
c.closed = true
return c.Repository.Close()
}
type closedLibrary struct {
*multiLibrary
repos []*closedRepository
m sync.Mutex
}
func (c *closedLibrary) trackRepo(r borges.Repository) *closedRepository {
c.m.Lock()
defer c.m.Unlock()
closed := &closedRepository{Repository: r}
c.repos = append(c.repos, closed)
return closed
}
func (c *closedLibrary) Check() bool {
for _, r := range c.repos {
if !r.closed {
return false
}
}
return true
}
func (c *closedLibrary) Get(
r borges.RepositoryID,
m borges.Mode,
) (borges.Repository, error) {
repo, err := c.multiLibrary.Get(r, m)
if err != nil {
return nil, err
}
return c.trackRepo(repo), nil
}
func (c *closedLibrary) Repositories(m borges.Mode) (borges.RepositoryIterator, error) {
iter, err := c.multiLibrary.Repositories(m)
if err != nil {
return nil, err
}
return &closedIter{
RepositoryIterator: iter,
c: c,
}, nil
}
type closedIter struct {
borges.RepositoryIterator
c *closedLibrary
}
func (i *closedIter) Next() (borges.Repository, error) {
repo, err := i.RepositoryIterator.Next()
if err != nil {
return nil, err
}
return i.c.trackRepo(repo), nil
}
// setupSivaCloseRepos creates a pool with siva files that can be checked
// if they've been closed.
func setupSivaCloseRepos(t *testing.T, dir string) (*sql.Context, *closedLibrary) {
require := require.New(t)
t.Helper()
lib, err := newMultiLibrary()
require.NoError(err)
closedLib := &closedLibrary{multiLibrary: lib}
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), closedLib)
cwd, err := os.Getwd()
require.NoError(err)
cwdFS := osfs.New(cwd)
filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if IsSivaFile(path) {
require.NoError(lib.AddSiva(path, cwdFS))
}
return nil
},
)
session := NewSession(pool, WithSkipGitErrors(true))
ctx := sql.NewContext(context.TODO(), sql.WithSession(session))
return ctx, closedLib
}
func testTableIndexIterClosed(t *testing.T, table sql.IndexableTable) {
t.Helper()
require := require.New(t)
ctx, closed := setupSivaCloseRepos(t, "_testdata")
iter, err := table.IndexKeyValues(ctx, nil)
require.NoError(err)
for {
_, i, err := iter.Next()
if err != nil {
require.Equal(io.EOF, err)
break
}
i.Close()
}
iter.Close()
require.True(closed.Check())
}
func testTableIterators(t *testing.T, table sql.IndexableTable, columns []string) {
t.Helper()
require := require.New(t)
ctx, closed := setupSivaCloseRepos(t, "_testdata")
rows, _ := tableToRows(ctx, table)
expected := len(rows)
iter, err := table.IndexKeyValues(ctx, columns)
require.NoError(err)
actual := 0
for {
_, i, err := iter.Next()
if err != nil {
require.Equal(io.EOF, err)
break
}
for {
_, _, err := i.Next()
if err != nil {
require.Equal(io.EOF, err)
break
}
actual++
}
i.Close()
}
iter.Close()
require.True(closed.Check())
require.EqualValues(expected, actual)
}
func testTableIterClosed(t *testing.T, table sql.IndexableTable) {
t.Helper()
require := require.New(t)
ctx, closed := setupSivaCloseRepos(t, "_testdata")
_, err := tableToRows(ctx, table)
require.NoError(err)
require.True(closed.Check())
}
func poolFromCtx(t *testing.T, ctx *sql.Context) *RepositoryPool {
t.Helper()
session, err := getSession(ctx)
require.NoError(t, err)
return session.Pool
}
func pathToName(path string) string {
return strings.TrimLeft(path, string(os.PathSeparator))
}
type multiLibrary struct {
lib *libraries.Libraries
plain *plain.Library
plainFS billy.Filesystem
siva *siva.Library
sivaFS billy.Filesystem
rootFS billy.Filesystem
}
func newMultiPool() (*multiLibrary, *RepositoryPool, error) {
lib, err := newMultiLibrary()
if err != nil {
return nil, nil, err
}
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
return lib, pool, err
}
func newMultiLibrary() (*multiLibrary, error) {
plainFS := memfs.New()
plainLoc, err := plain.NewLocation("root", plainFS, nil)
if err != nil {
return nil, err
}
plainLib := plain.NewLibrary("plain", nil)
plainLib.AddLocation(plainLoc)
sivaFS := memfs.New()
sivaLib, err := siva.NewLibrary("", sivaFS, &siva.LibraryOptions{
RootedRepo: true,
})
if err != nil {
return nil, err
}
libs := libraries.New(nil)
err = libs.Add(plainLib)
if err != nil {
return nil, err
}
err = libs.Add(sivaLib)
if err != nil {
return nil, err
}
return &multiLibrary{
lib: libs,
plain: plainLib,
plainFS: plainFS,
siva: sivaLib,
sivaFS: sivaFS,
rootFS: osfs.New("/"),
}, nil
}
func (m *multiLibrary) AddPlain(
name string,
path string,
fs billy.Filesystem,
) error {
if fs == nil {
fs = m.rootFS
}
return recursiveCopy(name, m.plainFS, path, fs)
}
func (m *multiLibrary) AddSiva(
path string,
fs billy.Filesystem,
) error {
if fs == nil {
fs = m.rootFS
}
name := filepath.Base(path)
return recursiveCopy(name, m.sivaFS, path, fs)
}
func (m *multiLibrary) ID() borges.LibraryID {
return m.lib.ID()
}
func (m *multiLibrary) Init(r borges.RepositoryID) (borges.Repository, error) {
return m.lib.Init(r)
}
func (m *multiLibrary) Get(r borges.RepositoryID, mode borges.Mode) (borges.Repository, error) {
return m.lib.Get(r, mode)
}
func (m *multiLibrary) GetOrInit(r borges.RepositoryID) (borges.Repository, error) {
return m.lib.GetOrInit(r)
}
func (m *multiLibrary) Has(r borges.RepositoryID) (bool, borges.LibraryID, borges.LocationID, error) {
return m.lib.Has(r)
}
func (m *multiLibrary) Repositories(mode borges.Mode) (borges.RepositoryIterator, error) {
return m.lib.Repositories(mode)
}
func (m *multiLibrary) Location(r borges.LocationID) (borges.Location, error) {
return m.lib.Location(r)
}
func (m *multiLibrary) Locations() (borges.LocationIterator, error) {
return m.lib.Locations()
}
func (m *multiLibrary) Library(l borges.LibraryID) (borges.Library, error) {
return m.lib.Library(l)
}
func (m *multiLibrary) Libraries() (borges.LibraryIterator, error) {
return m.lib.Libraries()
}
func recursiveCopy(
dst string,
dstFS billy.Filesystem,
src string,
srcFS billy.Filesystem,
) error {
stat, err := srcFS.Stat(src)
if err != nil {
return err
}
if stat.IsDir() {
err = dstFS.MkdirAll(dst, stat.Mode())
if err != nil {
return err
}
files, err := srcFS.ReadDir(src)
if err != nil {
return err
}
for _, file := range files {
srcPath := filepath.Join(src, file.Name())
dstPath := filepath.Join(dst, file.Name())
err = recursiveCopy(dstPath, dstFS, srcPath, srcFS)
if err != nil {
return err
}
}
} else {
err = copyFile(dst, dstFS, src, srcFS, stat.Mode())
if err != nil {
return err
}
}
return nil
}
func copyFile(
dst string,
dstFS billy.Filesystem,
src string,
srcFS billy.Filesystem,
mode os.FileMode,
) error {
_, err := srcFS.Stat(src)
if err != nil {
return err
}
fo, err := srcFS.Open(src)
if err != nil {
return err
}
defer fo.Close()
fd, err := dstFS.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode)
if err != nil {
return err
}
defer fd.Close()
_, err = io.Copy(fd, fo)
if err != nil {
_ = fd.Close()
_ = dstFS.Remove(dst)
return err
}
return nil
}