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
|
package commit
import (
"context"
"fmt"
"io/ioutil"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/streamio"
"google.golang.org/grpc/codes"
)
func TestSuccessfulRawBlameRequest(t *testing.T) {
stop, serverSocketPath := startTestServices(t)
defer stop()
client, conn := newCommitServiceClient(t, serverSocketPath)
defer conn.Close()
testRepo, _, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
testCases := []struct {
revision, path, data []byte
}{
{
revision: []byte("e63f41fe459e62e1228fcef60d7189127aeba95a"),
path: []byte("files/ruby/popen.rb"),
data: testhelper.MustReadFile(t, "testdata/files-ruby-popen-e63f41f-blame.txt"),
},
{
revision: []byte("e63f41fe459e62e1228fcef60d7189127aeba95a"),
path: []byte("files/ruby/../ruby/popen.rb"),
data: testhelper.MustReadFile(t, "testdata/files-ruby-popen-e63f41f-blame.txt"),
},
{
revision: []byte("93dcf076a236c837dd47d61f86d95a6b3d71b586"),
path: []byte("gitaly/empty-file"),
data: []byte{},
},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("test case: revision=%q path=%q", testCase.revision, testCase.path), func(t *testing.T) {
request := &gitalypb.RawBlameRequest{
Repository: testRepo,
Revision: testCase.revision,
Path: testCase.path,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := client.RawBlame(ctx, request)
if err != nil {
t.Fatal(err)
}
sr := streamio.NewReader(func() ([]byte, error) {
response, err := c.Recv()
return response.GetData(), err
})
blame, err := ioutil.ReadAll(sr)
if err != nil {
t.Fatal(err)
}
require.Equal(t, testCase.data, blame, "blame data mismatched")
})
}
}
func TestFailedRawBlameRequest(t *testing.T) {
stop, serverSocketPath := startTestServices(t)
defer stop()
client, conn := newCommitServiceClient(t, serverSocketPath)
defer conn.Close()
testRepo, _, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
invalidRepo := &gitalypb.Repository{StorageName: "fake", RelativePath: "path"}
testCases := []struct {
description string
repo *gitalypb.Repository
revision, path []byte
code codes.Code
}{
{
description: "Invalid repo",
repo: invalidRepo,
revision: []byte("master"),
path: []byte("a/b/c"),
code: codes.InvalidArgument,
},
{
description: "Empty revision",
repo: testRepo,
revision: []byte(""),
path: []byte("a/b/c"),
code: codes.InvalidArgument,
},
{
description: "Empty path",
repo: testRepo,
revision: []byte("abcdef"),
path: []byte(""),
code: codes.InvalidArgument,
},
{
description: "Invalid revision",
repo: testRepo,
revision: []byte("--output=/meow"),
path: []byte("a/b/c"),
code: codes.InvalidArgument,
},
}
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
request := gitalypb.RawBlameRequest{
Repository: testCase.repo,
Revision: testCase.revision,
Path: testCase.path,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := client.RawBlame(ctx, &request)
if err != nil {
t.Fatal(err)
}
testhelper.RequireGrpcError(t, drainRawBlameResponse(c), testCase.code)
})
}
}
func drainRawBlameResponse(c gitalypb.CommitService_RawBlameClient) error {
var err error
for err == nil {
_, err = c.Recv()
}
return err
}
|