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
|
package gittest
import (
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/internal/helper/text"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
)
// TestRepository tests an implementation of Repository.
func TestRepository(t *testing.T, cfg config.Cfg, getRepository func(testing.TB, *gitalypb.Repository) git.Repository) {
for _, tc := range []struct {
desc string
test func(*testing.T, config.Cfg, func(testing.TB, *gitalypb.Repository) git.Repository)
}{
{
desc: "ResolveRevision",
test: testRepositoryResolveRevision,
},
{
desc: "HasBranches",
test: testRepositoryHasBranches,
},
} {
t.Run(tc.desc, func(t *testing.T) {
tc.test(t, cfg, getRepository)
})
}
}
func testRepositoryResolveRevision(t *testing.T, cfg config.Cfg, getRepository func(testing.TB, *gitalypb.Repository) git.Repository) {
ctx, cancel := testhelper.Context()
defer cancel()
pbRepo, _, _ := CloneRepoAtStorage(t, cfg, cfg.Storages[0], t.Name())
for _, tc := range []struct {
desc string
revision string
expected git.ObjectID
}{
{
desc: "unqualified master branch",
revision: "master",
expected: "1e292f8fedd741b75372e19097c76d327140c312",
},
{
desc: "fully qualified master branch",
revision: "refs/heads/master",
expected: "1e292f8fedd741b75372e19097c76d327140c312",
},
{
desc: "typed commit",
revision: "refs/heads/master^{commit}",
expected: "1e292f8fedd741b75372e19097c76d327140c312",
},
{
desc: "extended SHA notation",
revision: "refs/heads/master^2",
expected: "c1c67abbaf91f624347bb3ae96eabe3a1b742478",
},
{
desc: "nonexistent branch",
revision: "refs/heads/foobar",
},
{
desc: "SHA notation gone wrong",
revision: "refs/heads/master^3",
},
} {
t.Run(tc.desc, func(t *testing.T) {
oid, err := getRepository(t, pbRepo).ResolveRevision(ctx, git.Revision(tc.revision))
if tc.expected == "" {
require.Equal(t, err, git.ErrReferenceNotFound)
return
}
require.NoError(t, err)
require.Equal(t, tc.expected, oid)
})
}
}
func testRepositoryHasBranches(t *testing.T, cfg config.Cfg, getRepository func(testing.TB, *gitalypb.Repository) git.Repository) {
ctx, cancel := testhelper.Context()
defer cancel()
pbRepo, repoPath, cleanup := InitBareRepoAt(t, cfg, cfg.Storages[0])
defer cleanup()
repo := getRepository(t, pbRepo)
emptyCommit := text.ChompBytes(Exec(t, cfg, "-C", repoPath, "commit-tree", git.EmptyTreeOID.String()))
Exec(t, cfg, "-C", repoPath, "update-ref", "refs/headsbranch", emptyCommit)
hasBranches, err := repo.HasBranches(ctx)
require.NoError(t, err)
require.False(t, hasBranches)
Exec(t, cfg, "-C", repoPath, "update-ref", "refs/heads/branch", emptyCommit)
hasBranches, err = repo.HasBranches(ctx)
require.NoError(t, err)
require.True(t, hasBranches)
}
|