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
|
package commit
import (
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
"google.golang.org/grpc/codes"
)
func TestSuccessfulLastCommitForPathRequest(t *testing.T) {
_, repo, _, client := setupCommitServiceWithRepo(t, true)
commit := testhelper.GitLabTestCommit("570e7b2abdd848b95f2f578043fc23bd6f6fd24d")
testCases := []struct {
desc string
revision string
path []byte
commit *gitalypb.GitCommit
}{
{
desc: "path present",
revision: "e63f41fe459e62e1228fcef60d7189127aeba95a",
path: []byte("files/ruby/regex.rb"),
commit: commit,
},
{
desc: "path empty",
revision: "570e7b2abdd848b95f2f578043fc23bd6f6fd24d",
commit: commit,
},
{
desc: "path is '/'",
revision: "570e7b2abdd848b95f2f578043fc23bd6f6fd24d",
commit: commit,
path: []byte("/"),
},
{
desc: "path is '*'",
revision: "570e7b2abdd848b95f2f578043fc23bd6f6fd24d",
commit: commit,
path: []byte("*"),
},
{
desc: "file does not exist in this commit",
revision: "570e7b2abdd848b95f2f578043fc23bd6f6fd24d",
path: []byte("files/lfs/lfs_object.iso"),
},
}
for _, testCase := range testCases {
t.Run(testCase.desc, func(t *testing.T) {
request := &gitalypb.LastCommitForPathRequest{
Repository: repo,
Revision: []byte(testCase.revision),
Path: testCase.path,
}
ctx, cancel := testhelper.Context()
defer cancel()
response, err := client.LastCommitForPath(ctx, request)
require.NoError(t, err)
testhelper.ProtoEqual(t, testCase.commit, response.GetCommit())
})
}
}
func TestFailedLastCommitForPathRequest(t *testing.T) {
_, repo, _, client := setupCommitServiceWithRepo(t, true)
invalidRepo := &gitalypb.Repository{StorageName: "fake", RelativePath: "path"}
testCases := []struct {
desc string
request *gitalypb.LastCommitForPathRequest
code codes.Code
}{
{
desc: "Invalid repository",
request: &gitalypb.LastCommitForPathRequest{Repository: invalidRepo},
code: codes.InvalidArgument,
},
{
desc: "Repository is nil",
request: &gitalypb.LastCommitForPathRequest{Revision: []byte("some-branch")},
code: codes.InvalidArgument,
},
{
desc: "Revision is missing",
request: &gitalypb.LastCommitForPathRequest{Repository: repo, Path: []byte("foo/bar")},
code: codes.InvalidArgument,
},
{
desc: "Revision is invalid",
request: &gitalypb.LastCommitForPathRequest{
Repository: repo, Path: []byte("foo/bar"), Revision: []byte("--output=/meow"),
},
code: codes.InvalidArgument,
},
}
for _, testCase := range testCases {
t.Run(testCase.desc, func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
_, err := client.LastCommitForPath(ctx, testCase.request)
testhelper.RequireGrpcError(t, err, testCase.code)
})
}
}
func TestSuccessfulLastCommitWithGlobCharacters(t *testing.T) {
cfg, repo, repoPath, client := setupCommitServiceWithRepo(t, true)
// This is an arbitrary blob known to exist in the test repository
const blobID = "c60514b6d3d6bf4bec1030f70026e34dfbd69ad5"
path := ":wq"
commitID := gittest.WriteCommit(t, cfg, repoPath,
gittest.WithTreeEntries(gittest.TreeEntry{
Mode: "100644", Path: path, OID: git.ObjectID(blobID),
}),
)
request := &gitalypb.LastCommitForPathRequest{
Repository: repo,
Revision: []byte(commitID),
Path: []byte(path),
LiteralPathspec: true,
}
ctx, cancel := testhelper.Context()
defer cancel()
response, err := client.LastCommitForPath(ctx, request)
require.NoError(t, err)
require.NotNil(t, response.GetCommit())
require.Equal(t, commitID.String(), response.GetCommit().Id)
request.LiteralPathspec = false
response, err = client.LastCommitForPath(ctx, request)
require.NoError(t, err)
require.Nil(t, response.GetCommit())
}
|