diff --git a/commit_files_test.go b/commit_files_test.go index 9c879ddda..cd23c35a4 100644 --- a/commit_files_test.go +++ b/commit_files_test.go @@ -2,6 +2,7 @@ package gitbase import ( "io" + "strconv" "testing" "github.com/src-d/go-mysql-server/sql" @@ -146,3 +147,60 @@ func TestPartitionRowsWithIndex(t *testing.T) { require.ElementsMatch(expected, result) } + +func TestCommitFilesIndexIter(t *testing.T) { + require := require.New(t) + + ctx, _, cleanup := setupRepos(t) + defer cleanup() + + key := &commitFileIndexKey{ + Repository: "zero", + Packfile: plumbing.ZeroHash.String(), + Hash: plumbing.ZeroHash.String(), + Offset: 0, + Name: "two", + Mode: 5, + Tree: plumbing.ZeroHash.String(), + Commit: plumbing.ZeroHash.String(), + } + limit := 10 + it := newCommitFilesIndexIter(testIndexValueIter{key, int64(limit)}, poolFromCtx(t, ctx)) + for off := 0; off < limit; off++ { + row, err := it.Next() + require.NoError(err) + + require.Equal(key.Repository, row[0]) + require.Equal(strconv.Itoa(off), row[2]) + } + _, err := it.Next() + require.EqualError(err, io.EOF.Error()) +} + +type testIndexValueIter struct { + key *commitFileIndexKey + limit int64 +} + +func (it testIndexValueIter) Next() ([]byte, error) { + if it.key.Offset >= it.limit { + return nil, io.EOF + } + + it.key.Name = strconv.Itoa(int(it.key.Offset)) + val, err := it.key.encode() + if err != nil { + return nil, err + } + val, err = encoder.encode(val) + if err != nil { + return nil, err + } + + it.key.Offset++ + return val, nil +} + +func (it testIndexValueIter) Close() error { + return nil +} diff --git a/common_test.go b/common_test.go index e2b13a887..3e819d93a 100644 --- a/common_test.go +++ b/common_test.go @@ -57,9 +57,10 @@ func buildSession( if ok { name := strings.TrimLeft(path, string(os.PathSeparator)) if err := lib.AddPlain(name, path, nil); err == nil { - _, err := pool.GetRepo(name) + r, err := pool.GetRepo(name) require.NoError(err) paths = append(paths, pathToName(path)) + require.NoError(r.Close()) } } } diff --git a/packfiles_test.go b/packfiles_test.go index a9d5968a6..c9c255b87 100644 --- a/packfiles_test.go +++ b/packfiles_test.go @@ -5,8 +5,10 @@ import ( "path/filepath" "testing" + fixtures "github.com/src-d/go-git-fixtures" "github.com/stretchr/testify/require" "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/cache" ) var ( @@ -43,6 +45,40 @@ func TestRepositoryPackfiles(t *testing.T) { require.NotNil(fs) } +func TestRepositoryPackfilesNoBare(t *testing.T) { + require := require.New(t) + + fs := fixtures.ByTag("worktree").One().Worktree() + + dotgit, packfiles, err := repositoryPackfiles(fs) + require.NoError(err) + require.Equal([]plumbing.Hash{ + plumbing.NewHash("323a4b6b5de684f9966953a043bc800154e5dbfa"), + }, packfiles) + + require.NoError(dotgit.Close()) +} + +func TestGetUnpackedObject(t *testing.T) { + require := require.New(t) + + fs := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().Worktree() + path := fs.Root() + + lib, err := newMultiLibrary() + require.NoError(err) + pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib) + require.NoError(lib.AddPlain(path, path, nil)) + + r, err := pool.GetRepo(path) + require.NoError(err) + + obj, err := getUnpackedObject(r, plumbing.NewHash("3bf5d30ad4f23cf517676fee232e3bcb8537c1d0")) + require.NoError(err) + require.NotNil(obj) + require.NoError(r.Close()) +} + func TestRepositoryIndex(t *testing.T) { lib, pool, err := newMultiPool() require.NoError(t, err) diff --git a/repository_pool.go b/repository_pool.go index 22294fe79..b18d8d934 100644 --- a/repository_pool.go +++ b/repository_pool.go @@ -59,12 +59,13 @@ func (r *Repository) Cache() cache.Object { return r.cache } -func (r *Repository) Close() { +func (r *Repository) Close() error { if r != nil && r.repo != nil { if closer, ok := r.repo.(io.Closer); ok { - closer.Close() + return closer.Close() } } + return nil } // RepositoryPool holds a pool git repository paths and diff --git a/squash_iterator_test.go b/squash_iterator_test.go index b7462d0ce..384c32415 100644 --- a/squash_iterator_test.go +++ b/squash_iterator_test.go @@ -4,7 +4,9 @@ import ( "context" "os" "path/filepath" + "strings" "testing" + "unicode" "github.com/src-d/go-borges" "github.com/src-d/go-borges/plain" @@ -23,6 +25,38 @@ func TestAllReposIter(t *testing.T) { require.Len(chainableIterRows(t, ctx, NewAllReposIter(nil)), 2) } +func TestSquashedTableString(t *testing.T) { + require := require.New(t) + ctx, cleanup := setupIter(t) + defer cleanup() + + const expected = `SquashedTable(test)├─Columns│└─Column(repository_id,TEXT,nullable=false)└─Filters└─NOT(1)` + + notTrue := expression.NewNot( + expression.NewLiteral(1, sql.Int64), + ) + + st := &SquashedTable{ + iter: NewAllReposIter(notTrue), + tables: []string{"test"}, + schemaMappings: nil, + filters: []sql.Expression{notTrue}, + indexedTables: nil, + } + + str := strings.Map(func(r rune) rune { + if unicode.IsSpace(r) { + return -1 + } + return r + }, st.String()) + require.EqualValues(expected, str) + + rows, err := tableToRows(ctx, st) + require.NoError(err) + require.Empty(rows) +} + func TestSquashContextCancelled(t *testing.T) { require := require.New(t) ctx, cleanup := setupIter(t)