1package ref
2
3import (
4	"testing"
5
6	"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper/testassert"
7	"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
8	"google.golang.org/protobuf/types/known/timestamppb"
9)
10
11func TestBuildLocalBranch(t *testing.T) {
12	testCases := []struct {
13		desc string
14		in   *gitalypb.GitCommit
15		out  *gitalypb.FindLocalBranchResponse
16	}{
17		{
18			desc: "all required fields present",
19			in: &gitalypb.GitCommit{
20				Id:      "b83d6e391c22777fca1ed3012fce84f633d7fed0",
21				Subject: []byte("Merge branch 'branch-merged' into 'master'"),
22				Body:    []byte("Merge branch 'branch-merged' into 'master'\r\n\r\nadds bar folder and branch-test text file to check Repository merged_to_root_ref method\r\n\r\n\r\n\r\nSee merge request !12"),
23				Author: &gitalypb.CommitAuthor{
24					Name:     []byte("Job van der Voort"),
25					Email:    []byte("job@gitlab.com"),
26					Date:     &timestamppb.Timestamp{Seconds: 1474987066},
27					Timezone: []byte("+0200"),
28				},
29				Committer: &gitalypb.CommitAuthor{
30					Name:     []byte("Job van der Voort"),
31					Email:    []byte("job@gitlab.com"),
32					Date:     &timestamppb.Timestamp{Seconds: 1474987066},
33					Timezone: []byte("+0200"),
34				},
35				ParentIds: []string{
36					"1b12f15a11fc6e62177bef08f47bc7b5ce50b141",
37					"498214de67004b1da3d820901307bed2a68a8ef6",
38				},
39				BodySize: 162,
40			},
41			out: &gitalypb.FindLocalBranchResponse{
42				Name:          []byte("my-branch"),
43				CommitId:      "b83d6e391c22777fca1ed3012fce84f633d7fed0",
44				CommitSubject: []byte("Merge branch 'branch-merged' into 'master'"),
45				CommitAuthor: &gitalypb.FindLocalBranchCommitAuthor{
46					Name:     []byte("Job van der Voort"),
47					Email:    []byte("job@gitlab.com"),
48					Date:     &timestamppb.Timestamp{Seconds: 1474987066},
49					Timezone: []byte("+0200"),
50				},
51				CommitCommitter: &gitalypb.FindLocalBranchCommitAuthor{
52					Name:     []byte("Job van der Voort"),
53					Email:    []byte("job@gitlab.com"),
54					Date:     &timestamppb.Timestamp{Seconds: 1474987066},
55					Timezone: []byte("+0200"),
56				},
57				Commit: &gitalypb.GitCommit{
58					Id:      "b83d6e391c22777fca1ed3012fce84f633d7fed0",
59					Subject: []byte("Merge branch 'branch-merged' into 'master'"),
60					Body:    []byte("Merge branch 'branch-merged' into 'master'\r\n\r\nadds bar folder and branch-test text file to check Repository merged_to_root_ref method\r\n\r\n\r\n\r\nSee merge request !12"),
61					Author: &gitalypb.CommitAuthor{
62						Name:     []byte("Job van der Voort"),
63						Email:    []byte("job@gitlab.com"),
64						Date:     &timestamppb.Timestamp{Seconds: 1474987066},
65						Timezone: []byte("+0200"),
66					},
67					Committer: &gitalypb.CommitAuthor{
68						Name:     []byte("Job van der Voort"),
69						Email:    []byte("job@gitlab.com"),
70						Date:     &timestamppb.Timestamp{Seconds: 1474987066},
71						Timezone: []byte("+0200"),
72					},
73					ParentIds: []string{"1b12f15a11fc6e62177bef08f47bc7b5ce50b141", "498214de67004b1da3d820901307bed2a68a8ef6"},
74					BodySize:  162,
75				},
76			},
77		},
78		{
79			desc: "missing author",
80			in: &gitalypb.GitCommit{
81				Id:      "b83d6e391c22777fca1ed3012fce84f633d7fed0",
82				Subject: []byte("Merge branch 'branch-merged' into 'master'"),
83				Body:    []byte("Merge branch 'branch-merged' into 'master'\r\n\r\nadds bar folder and branch-test text file to check Repository merged_to_root_ref method\r\n\r\n\r\n\r\nSee merge request !12"),
84				Committer: &gitalypb.CommitAuthor{
85					Name:     []byte("Job van der Voort"),
86					Email:    []byte("job@gitlab.com"),
87					Date:     &timestamppb.Timestamp{Seconds: 1474987066},
88					Timezone: []byte("+0200"),
89				},
90				ParentIds: []string{
91					"1b12f15a11fc6e62177bef08f47bc7b5ce50b141",
92					"498214de67004b1da3d820901307bed2a68a8ef6",
93				},
94				BodySize: 162,
95			},
96			out: &gitalypb.FindLocalBranchResponse{
97				Name:          []byte("my-branch"),
98				CommitId:      "b83d6e391c22777fca1ed3012fce84f633d7fed0",
99				CommitSubject: []byte("Merge branch 'branch-merged' into 'master'"),
100				CommitAuthor:  nil,
101				CommitCommitter: &gitalypb.FindLocalBranchCommitAuthor{
102					Name:     []byte("Job van der Voort"),
103					Email:    []byte("job@gitlab.com"),
104					Date:     &timestamppb.Timestamp{Seconds: 1474987066},
105					Timezone: []byte("+0200"),
106				},
107				Commit: &gitalypb.GitCommit{
108					Id:      "b83d6e391c22777fca1ed3012fce84f633d7fed0",
109					Subject: []byte("Merge branch 'branch-merged' into 'master'"),
110					Body:    []byte("Merge branch 'branch-merged' into 'master'\r\n\r\nadds bar folder and branch-test text file to check Repository merged_to_root_ref method\r\n\r\n\r\n\r\nSee merge request !12"),
111					Committer: &gitalypb.CommitAuthor{
112						Name:     []byte("Job van der Voort"),
113						Email:    []byte("job@gitlab.com"),
114						Date:     &timestamppb.Timestamp{Seconds: 1474987066},
115						Timezone: []byte("+0200"),
116					},
117					ParentIds: []string{"1b12f15a11fc6e62177bef08f47bc7b5ce50b141", "498214de67004b1da3d820901307bed2a68a8ef6"},
118					BodySize:  162,
119				},
120			},
121		},
122		{
123			desc: "missing committer",
124			in: &gitalypb.GitCommit{
125				Id:      "b83d6e391c22777fca1ed3012fce84f633d7fed0",
126				Subject: []byte("Merge branch 'branch-merged' into 'master'"),
127				Body:    []byte("Merge branch 'branch-merged' into 'master'\r\n\r\nadds bar folder and branch-test text file to check Repository merged_to_root_ref method\r\n\r\n\r\n\r\nSee merge request !12"),
128				Author: &gitalypb.CommitAuthor{
129					Name:     []byte("Job van der Voort"),
130					Email:    []byte("job@gitlab.com"),
131					Date:     &timestamppb.Timestamp{Seconds: 1474987066},
132					Timezone: []byte("+0200"),
133				},
134				ParentIds: []string{
135					"1b12f15a11fc6e62177bef08f47bc7b5ce50b141",
136					"498214de67004b1da3d820901307bed2a68a8ef6",
137				},
138				BodySize: 162,
139			},
140			out: &gitalypb.FindLocalBranchResponse{
141				Name:          []byte("my-branch"),
142				CommitId:      "b83d6e391c22777fca1ed3012fce84f633d7fed0",
143				CommitSubject: []byte("Merge branch 'branch-merged' into 'master'"),
144				CommitAuthor: &gitalypb.FindLocalBranchCommitAuthor{
145					Name:     []byte("Job van der Voort"),
146					Email:    []byte("job@gitlab.com"),
147					Date:     &timestamppb.Timestamp{Seconds: 1474987066},
148					Timezone: []byte("+0200"),
149				},
150				CommitCommitter: nil,
151				Commit: &gitalypb.GitCommit{
152					Id:      "b83d6e391c22777fca1ed3012fce84f633d7fed0",
153					Subject: []byte("Merge branch 'branch-merged' into 'master'"),
154					Body:    []byte("Merge branch 'branch-merged' into 'master'\r\n\r\nadds bar folder and branch-test text file to check Repository merged_to_root_ref method\r\n\r\n\r\n\r\nSee merge request !12"),
155					Author: &gitalypb.CommitAuthor{
156						Name:     []byte("Job van der Voort"),
157						Email:    []byte("job@gitlab.com"),
158						Date:     &timestamppb.Timestamp{Seconds: 1474987066},
159						Timezone: []byte("+0200"),
160					},
161					ParentIds: []string{"1b12f15a11fc6e62177bef08f47bc7b5ce50b141", "498214de67004b1da3d820901307bed2a68a8ef6"},
162					BodySize:  162,
163				},
164			},
165		},
166		{
167			desc: "nil commit",
168			in:   nil,
169			out: &gitalypb.FindLocalBranchResponse{
170				Name: []byte("my-branch"),
171			},
172		},
173	}
174
175	for _, tc := range testCases {
176		t.Run(tc.desc, func(t *testing.T) {
177			testassert.ProtoEqual(t, tc.out, buildLocalBranch([]byte("my-branch"), tc.in))
178		})
179	}
180}
181