| // Copyright 2020 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package gitiles |
| |
| import ( |
| "context" |
| "testing" |
| |
| "github.com/golang/mock/gomock" |
| "go.chromium.org/luci/common/proto/git" |
| gitilespb "go.chromium.org/luci/common/proto/gitiles" |
| "go.chromium.org/luci/common/proto/gitiles/mock_gitiles" |
| |
| "github.com/google/go-cmp/cmp" |
| ) |
| |
| func TestLatestCommit(t *testing.T) { |
| t.Parallel() |
| tests := []struct { |
| project string |
| expected string |
| }{ |
| { |
| project: "test-project", |
| expected: "foobar", |
| }, |
| } |
| for _, test := range tests { |
| ctrl := gomock.NewController(t) |
| defer ctrl.Finish() |
| mockGitilesClient := mock_gitiles.NewMockGitilesClient(ctrl) |
| req := gitilespb.LogRequest{ |
| Project: test.project, |
| Committish: "refs/heads/main", |
| PageSize: 1, |
| } |
| // Client returns a successful Log response. |
| resp := gitilespb.LogResponse{ |
| Log: []*git.Commit{{Id: "foobar"}}, |
| } |
| mockGitilesClient.EXPECT().Log(gomock.Any(), &req).Return(&resp, nil) |
| mockGitilesClientWrapper := Client{ |
| client: mockGitilesClient, |
| project: test.project, |
| } |
| ctx := context.Background() |
| commit, err := mockGitilesClientWrapper.LatestCommit(ctx, "refs/heads/main") |
| if err != nil { |
| t.Fatal(err) |
| } |
| if test.expected != commit { |
| t.Fatalf("expected: %s\nactual: %s\n", test.expected, commit) |
| } |
| } |
| } |
| |
| func TestLog(t *testing.T) { |
| t.Parallel() |
| tests := []struct { |
| project string |
| expected []*git.Commit |
| }{ |
| { |
| project: "test-project", |
| expected: []*git.Commit{ |
| { |
| Id: "c", |
| }, |
| { |
| Id: "b", |
| }, |
| { |
| Id: "a", |
| }, |
| }, |
| }, |
| } |
| for _, test := range tests { |
| ctrl := gomock.NewController(t) |
| defer ctrl.Finish() |
| mockGitilesClient := mock_gitiles.NewMockGitilesClient(ctrl) |
| req := gitilespb.LogRequest{ |
| Project: test.project, |
| Committish: test.expected[0].Id, |
| PageSize: int32(len(test.expected)), |
| } |
| // Client returns a successful Log response. |
| resp := gitilespb.LogResponse{ |
| Log: test.expected, |
| } |
| mockGitilesClient.EXPECT().Log(gomock.Any(), &req).Return(&resp, nil) |
| mockGitilesClientWrapper := Client{ |
| client: mockGitilesClient, |
| project: test.project, |
| } |
| ctx := context.Background() |
| log, err := mockGitilesClientWrapper.Log(ctx, test.expected[0].Id, int32(len(test.expected))) |
| if err != nil { |
| t.Fatal(err) |
| } |
| if diff := cmp.Diff(test.expected, log, cmp.Comparer(func(x, y *git.Commit) bool { |
| return x.Id == y.Id |
| })); diff != "" { |
| t.Fatalf("different (-want +got):\n%s", diff) |
| } |
| } |
| } |
| |
| func TestDownloadFile(t *testing.T) { |
| t.Parallel() |
| tests := []struct { |
| project string |
| path string |
| ref string |
| expected string |
| }{ |
| { |
| project: "test-project", |
| path: "a/b/c.txt", |
| ref: "foo", |
| expected: "file contents", |
| }, |
| } |
| for _, test := range tests { |
| ctrl := gomock.NewController(t) |
| defer ctrl.Finish() |
| mockGitilesClient := mock_gitiles.NewMockGitilesClient(ctrl) |
| req := gitilespb.DownloadFileRequest{ |
| Project: test.project, |
| Committish: test.ref, |
| Path: test.path, |
| } |
| // Client returns a successful DownloadFile response. |
| resp := gitilespb.DownloadFileResponse{ |
| Contents: test.expected, |
| } |
| mockGitilesClient.EXPECT().DownloadFile(gomock.Any(), &req).Return(&resp, nil) |
| mockGitilesClientWrapper := Client{ |
| client: mockGitilesClient, |
| project: test.project, |
| } |
| ctx := context.Background() |
| contents, err := mockGitilesClientWrapper.DownloadFile(ctx, test.path, test.ref) |
| if err != nil { |
| t.Fatal(err) |
| } |
| if contents != test.expected { |
| t.Fatalf("expected: %s\nactual: %s\n", test.expected, contents) |
| } |
| } |
| } |