From 4baae671e6ecbb3ebe10d4e0c886ceeea4e53bdb Mon Sep 17 00:00:00 2001 From: Kristian Date: Thu, 14 Mar 2019 16:09:26 +0100 Subject: [PATCH] Switch to counterfeiter and testify (#85) - Generate fakes using counterfeiter. - Use testify for assertions. --- check_test.go | 48 ++--- e2e/e2e_test.go | 56 ++--- fakes/fake_git.go | 484 +++++++++++++++++++++++++++++++++++++++++++ fakes/fake_github.go | 416 +++++++++++++++++++++++++++++++++++++ git.go | 2 +- github.go | 2 +- go.mod | 2 +- go.sum | 9 +- in_test.go | 189 +++++++---------- mocks/mock_git.go | 106 ---------- mocks/mock_github.go | 97 --------- out_test.go | 61 +++--- 12 files changed, 1049 insertions(+), 423 deletions(-) create mode 100644 fakes/fake_git.go create mode 100644 fakes/fake_github.go delete mode 100644 mocks/mock_git.go delete mode 100644 mocks/mock_github.go diff --git a/check_test.go b/check_test.go index dd63743e..fec663fc 100644 --- a/check_test.go +++ b/check_test.go @@ -1,12 +1,11 @@ package resource_test import ( - "reflect" "testing" - "github.com/golang/mock/gomock" - "github.com/telia-oss/github-pr-resource" - "github.com/telia-oss/github-pr-resource/mocks" + "github.com/stretchr/testify/assert" + resource "github.com/telia-oss/github-pr-resource" + "github.com/telia-oss/github-pr-resource/fakes" ) var ( @@ -141,29 +140,20 @@ func TestCheck(t *testing.T) { for _, tc := range tests { t.Run(tc.description, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() + github := new(fakes.FakeGithub) + github.ListOpenPullRequestsReturns(tc.pullRequests, nil) - github := mocks.NewMockGithub(ctrl) - github.EXPECT().ListOpenPullRequests().Times(1).Return(tc.pullRequests, nil) - - if len(tc.files) > 0 { - // TODO: Figure out how to do this in a loop with variables. As is, it will break when adding new tests. - gomock.InOrder( - github.EXPECT().ListModifiedFiles(gomock.Any()).Times(1).Return(tc.files[0], nil), - github.EXPECT().ListModifiedFiles(gomock.Any()).Times(1).Return(tc.files[1], nil), - ) + for i, file := range tc.files { + github.ListModifiedFilesReturnsOnCall(i, file, nil) } input := resource.CheckRequest{Source: tc.source, Version: tc.version} output, err := resource.Check(input, github) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } - if got, want := output, tc.expected; !reflect.DeepEqual(got, want) { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) + if assert.NoError(t, err) { + assert.Equal(t, tc.expected, output) } + assert.Equal(t, 1, github.ListOpenPullRequestsCallCount()) }) } } @@ -214,9 +204,7 @@ func TestContainsSkipCI(t *testing.T) { for _, tc := range tests { t.Run(tc.description, func(t *testing.T) { got := resource.ContainsSkipCI(tc.message) - if got != tc.want { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, tc.want) - } + assert.Equal(t, tc.want, got) }) } } @@ -282,11 +270,8 @@ func TestFilterPath(t *testing.T) { for _, tc := range cases { t.Run(tc.description, func(t *testing.T) { got, err := resource.FilterPath(tc.files, tc.pattern) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } - if !reflect.DeepEqual(got, tc.want) { - t.Errorf("\ngot:\n%v\nwant:\n%s\n", got, tc.want) + if assert.NoError(t, err) { + assert.Equal(t, tc.want, got) } }) } @@ -353,11 +338,8 @@ func TestFilterIgnorePath(t *testing.T) { for _, tc := range cases { t.Run(tc.description, func(t *testing.T) { got, err := resource.FilterIgnorePath(tc.files, tc.pattern) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } - if !reflect.DeepEqual(got, tc.want) { - t.Errorf("\ngot:\n%v\nwant:\n%s\n", got, tc.want) + if assert.NoError(t, err) { + assert.Equal(t, tc.want, got) } }) } diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index d8cd7033..e2aa34af 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -6,11 +6,12 @@ import ( "io/ioutil" "os" "path/filepath" - "reflect" "testing" "time" - "github.com/telia-oss/github-pr-resource" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + resource "github.com/telia-oss/github-pr-resource" ) var ( @@ -113,17 +114,13 @@ func TestCheckE2E(t *testing.T) { for _, tc := range tests { t.Run(tc.description, func(t *testing.T) { github, err := resource.NewGithubClient(&tc.source) - if err != nil { - t.Fatalf("failed to create github client: %s", err) - } + require.NoError(t, err) input := resource.CheckRequest{Source: tc.source, Version: tc.version} output, err := resource.Check(input, github) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } - if got, want := output, tc.expected; !reflect.DeepEqual(got, want) { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) + + if assert.NoError(t, err) { + assert.Equal(t, tc.expected, output) } }) } @@ -200,49 +197,34 @@ func TestGetAndPutE2E(t *testing.T) { t.Run(tc.description, func(t *testing.T) { // Create temporary directory dir, err := ioutil.TempDir("", "github-pr-resource") - if err != nil { - t.Fatalf("failed to create temporary directory") - } + require.NoError(t, err) defer os.RemoveAll(dir) github, err := resource.NewGithubClient(&tc.source) - if err != nil { - t.Fatalf("failed to create github client: %s", err) - } + require.NoError(t, err) + git, err := resource.NewGitClient(&tc.source, dir, ioutil.Discard) - if err != nil { - t.Fatalf("failed to create git client: %s", err) - } + require.NoError(t, err) // Get (output and files) getRequest := resource.GetRequest{Source: tc.source, Version: tc.version, Params: tc.getParameters} getOutput, err := resource.Get(getRequest, github, git, dir) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } - if got, want := getOutput.Version, tc.version; !reflect.DeepEqual(got, want) { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) - } + + require.NoError(t, err) + assert.Equal(t, tc.version, getOutput.Version) version := readTestFile(t, filepath.Join(dir, ".git", "resource", "version.json")) - if got, want := version, tc.versionString; got != want { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) - } + assert.Equal(t, tc.versionString, version) metadata := readTestFile(t, filepath.Join(dir, ".git", "resource", "metadata.json")) - if got, want := metadata, tc.metadataString; got != want { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) - } + assert.Equal(t, tc.metadataString, metadata) // Put putRequest := resource.PutRequest{Source: tc.source, Params: tc.putParameters} putOutput, err := resource.Put(putRequest, github, dir) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } - if got, want := putOutput.Version, tc.version; !reflect.DeepEqual(got, want) { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) - } + + require.NoError(t, err) + assert.Equal(t, tc.version, putOutput.Version) }) } } diff --git a/fakes/fake_git.go b/fakes/fake_git.go new file mode 100644 index 00000000..fedf6ee9 --- /dev/null +++ b/fakes/fake_git.go @@ -0,0 +1,484 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package fakes + +import ( + "sync" + + resource "github.com/telia-oss/github-pr-resource" +) + +type FakeGit struct { + FetchStub func(string, int) error + fetchMutex sync.RWMutex + fetchArgsForCall []struct { + arg1 string + arg2 int + } + fetchReturns struct { + result1 error + } + fetchReturnsOnCall map[int]struct { + result1 error + } + GitCryptUnlockStub func(string) error + gitCryptUnlockMutex sync.RWMutex + gitCryptUnlockArgsForCall []struct { + arg1 string + } + gitCryptUnlockReturns struct { + result1 error + } + gitCryptUnlockReturnsOnCall map[int]struct { + result1 error + } + InitStub func(string) error + initMutex sync.RWMutex + initArgsForCall []struct { + arg1 string + } + initReturns struct { + result1 error + } + initReturnsOnCall map[int]struct { + result1 error + } + MergeStub func(string) error + mergeMutex sync.RWMutex + mergeArgsForCall []struct { + arg1 string + } + mergeReturns struct { + result1 error + } + mergeReturnsOnCall map[int]struct { + result1 error + } + PullStub func(string, string) error + pullMutex sync.RWMutex + pullArgsForCall []struct { + arg1 string + arg2 string + } + pullReturns struct { + result1 error + } + pullReturnsOnCall map[int]struct { + result1 error + } + RevParseStub func(string) (string, error) + revParseMutex sync.RWMutex + revParseArgsForCall []struct { + arg1 string + } + revParseReturns struct { + result1 string + result2 error + } + revParseReturnsOnCall map[int]struct { + result1 string + result2 error + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *FakeGit) Fetch(arg1 string, arg2 int) error { + fake.fetchMutex.Lock() + ret, specificReturn := fake.fetchReturnsOnCall[len(fake.fetchArgsForCall)] + fake.fetchArgsForCall = append(fake.fetchArgsForCall, struct { + arg1 string + arg2 int + }{arg1, arg2}) + fake.recordInvocation("Fetch", []interface{}{arg1, arg2}) + fake.fetchMutex.Unlock() + if fake.FetchStub != nil { + return fake.FetchStub(arg1, arg2) + } + if specificReturn { + return ret.result1 + } + fakeReturns := fake.fetchReturns + return fakeReturns.result1 +} + +func (fake *FakeGit) FetchCallCount() int { + fake.fetchMutex.RLock() + defer fake.fetchMutex.RUnlock() + return len(fake.fetchArgsForCall) +} + +func (fake *FakeGit) FetchCalls(stub func(string, int) error) { + fake.fetchMutex.Lock() + defer fake.fetchMutex.Unlock() + fake.FetchStub = stub +} + +func (fake *FakeGit) FetchArgsForCall(i int) (string, int) { + fake.fetchMutex.RLock() + defer fake.fetchMutex.RUnlock() + argsForCall := fake.fetchArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2 +} + +func (fake *FakeGit) FetchReturns(result1 error) { + fake.fetchMutex.Lock() + defer fake.fetchMutex.Unlock() + fake.FetchStub = nil + fake.fetchReturns = struct { + result1 error + }{result1} +} + +func (fake *FakeGit) FetchReturnsOnCall(i int, result1 error) { + fake.fetchMutex.Lock() + defer fake.fetchMutex.Unlock() + fake.FetchStub = nil + if fake.fetchReturnsOnCall == nil { + fake.fetchReturnsOnCall = make(map[int]struct { + result1 error + }) + } + fake.fetchReturnsOnCall[i] = struct { + result1 error + }{result1} +} + +func (fake *FakeGit) GitCryptUnlock(arg1 string) error { + fake.gitCryptUnlockMutex.Lock() + ret, specificReturn := fake.gitCryptUnlockReturnsOnCall[len(fake.gitCryptUnlockArgsForCall)] + fake.gitCryptUnlockArgsForCall = append(fake.gitCryptUnlockArgsForCall, struct { + arg1 string + }{arg1}) + fake.recordInvocation("GitCryptUnlock", []interface{}{arg1}) + fake.gitCryptUnlockMutex.Unlock() + if fake.GitCryptUnlockStub != nil { + return fake.GitCryptUnlockStub(arg1) + } + if specificReturn { + return ret.result1 + } + fakeReturns := fake.gitCryptUnlockReturns + return fakeReturns.result1 +} + +func (fake *FakeGit) GitCryptUnlockCallCount() int { + fake.gitCryptUnlockMutex.RLock() + defer fake.gitCryptUnlockMutex.RUnlock() + return len(fake.gitCryptUnlockArgsForCall) +} + +func (fake *FakeGit) GitCryptUnlockCalls(stub func(string) error) { + fake.gitCryptUnlockMutex.Lock() + defer fake.gitCryptUnlockMutex.Unlock() + fake.GitCryptUnlockStub = stub +} + +func (fake *FakeGit) GitCryptUnlockArgsForCall(i int) string { + fake.gitCryptUnlockMutex.RLock() + defer fake.gitCryptUnlockMutex.RUnlock() + argsForCall := fake.gitCryptUnlockArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeGit) GitCryptUnlockReturns(result1 error) { + fake.gitCryptUnlockMutex.Lock() + defer fake.gitCryptUnlockMutex.Unlock() + fake.GitCryptUnlockStub = nil + fake.gitCryptUnlockReturns = struct { + result1 error + }{result1} +} + +func (fake *FakeGit) GitCryptUnlockReturnsOnCall(i int, result1 error) { + fake.gitCryptUnlockMutex.Lock() + defer fake.gitCryptUnlockMutex.Unlock() + fake.GitCryptUnlockStub = nil + if fake.gitCryptUnlockReturnsOnCall == nil { + fake.gitCryptUnlockReturnsOnCall = make(map[int]struct { + result1 error + }) + } + fake.gitCryptUnlockReturnsOnCall[i] = struct { + result1 error + }{result1} +} + +func (fake *FakeGit) Init(arg1 string) error { + fake.initMutex.Lock() + ret, specificReturn := fake.initReturnsOnCall[len(fake.initArgsForCall)] + fake.initArgsForCall = append(fake.initArgsForCall, struct { + arg1 string + }{arg1}) + fake.recordInvocation("Init", []interface{}{arg1}) + fake.initMutex.Unlock() + if fake.InitStub != nil { + return fake.InitStub(arg1) + } + if specificReturn { + return ret.result1 + } + fakeReturns := fake.initReturns + return fakeReturns.result1 +} + +func (fake *FakeGit) InitCallCount() int { + fake.initMutex.RLock() + defer fake.initMutex.RUnlock() + return len(fake.initArgsForCall) +} + +func (fake *FakeGit) InitCalls(stub func(string) error) { + fake.initMutex.Lock() + defer fake.initMutex.Unlock() + fake.InitStub = stub +} + +func (fake *FakeGit) InitArgsForCall(i int) string { + fake.initMutex.RLock() + defer fake.initMutex.RUnlock() + argsForCall := fake.initArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeGit) InitReturns(result1 error) { + fake.initMutex.Lock() + defer fake.initMutex.Unlock() + fake.InitStub = nil + fake.initReturns = struct { + result1 error + }{result1} +} + +func (fake *FakeGit) InitReturnsOnCall(i int, result1 error) { + fake.initMutex.Lock() + defer fake.initMutex.Unlock() + fake.InitStub = nil + if fake.initReturnsOnCall == nil { + fake.initReturnsOnCall = make(map[int]struct { + result1 error + }) + } + fake.initReturnsOnCall[i] = struct { + result1 error + }{result1} +} + +func (fake *FakeGit) Merge(arg1 string) error { + fake.mergeMutex.Lock() + ret, specificReturn := fake.mergeReturnsOnCall[len(fake.mergeArgsForCall)] + fake.mergeArgsForCall = append(fake.mergeArgsForCall, struct { + arg1 string + }{arg1}) + fake.recordInvocation("Merge", []interface{}{arg1}) + fake.mergeMutex.Unlock() + if fake.MergeStub != nil { + return fake.MergeStub(arg1) + } + if specificReturn { + return ret.result1 + } + fakeReturns := fake.mergeReturns + return fakeReturns.result1 +} + +func (fake *FakeGit) MergeCallCount() int { + fake.mergeMutex.RLock() + defer fake.mergeMutex.RUnlock() + return len(fake.mergeArgsForCall) +} + +func (fake *FakeGit) MergeCalls(stub func(string) error) { + fake.mergeMutex.Lock() + defer fake.mergeMutex.Unlock() + fake.MergeStub = stub +} + +func (fake *FakeGit) MergeArgsForCall(i int) string { + fake.mergeMutex.RLock() + defer fake.mergeMutex.RUnlock() + argsForCall := fake.mergeArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeGit) MergeReturns(result1 error) { + fake.mergeMutex.Lock() + defer fake.mergeMutex.Unlock() + fake.MergeStub = nil + fake.mergeReturns = struct { + result1 error + }{result1} +} + +func (fake *FakeGit) MergeReturnsOnCall(i int, result1 error) { + fake.mergeMutex.Lock() + defer fake.mergeMutex.Unlock() + fake.MergeStub = nil + if fake.mergeReturnsOnCall == nil { + fake.mergeReturnsOnCall = make(map[int]struct { + result1 error + }) + } + fake.mergeReturnsOnCall[i] = struct { + result1 error + }{result1} +} + +func (fake *FakeGit) Pull(arg1 string, arg2 string) error { + fake.pullMutex.Lock() + ret, specificReturn := fake.pullReturnsOnCall[len(fake.pullArgsForCall)] + fake.pullArgsForCall = append(fake.pullArgsForCall, struct { + arg1 string + arg2 string + }{arg1, arg2}) + fake.recordInvocation("Pull", []interface{}{arg1, arg2}) + fake.pullMutex.Unlock() + if fake.PullStub != nil { + return fake.PullStub(arg1, arg2) + } + if specificReturn { + return ret.result1 + } + fakeReturns := fake.pullReturns + return fakeReturns.result1 +} + +func (fake *FakeGit) PullCallCount() int { + fake.pullMutex.RLock() + defer fake.pullMutex.RUnlock() + return len(fake.pullArgsForCall) +} + +func (fake *FakeGit) PullCalls(stub func(string, string) error) { + fake.pullMutex.Lock() + defer fake.pullMutex.Unlock() + fake.PullStub = stub +} + +func (fake *FakeGit) PullArgsForCall(i int) (string, string) { + fake.pullMutex.RLock() + defer fake.pullMutex.RUnlock() + argsForCall := fake.pullArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2 +} + +func (fake *FakeGit) PullReturns(result1 error) { + fake.pullMutex.Lock() + defer fake.pullMutex.Unlock() + fake.PullStub = nil + fake.pullReturns = struct { + result1 error + }{result1} +} + +func (fake *FakeGit) PullReturnsOnCall(i int, result1 error) { + fake.pullMutex.Lock() + defer fake.pullMutex.Unlock() + fake.PullStub = nil + if fake.pullReturnsOnCall == nil { + fake.pullReturnsOnCall = make(map[int]struct { + result1 error + }) + } + fake.pullReturnsOnCall[i] = struct { + result1 error + }{result1} +} + +func (fake *FakeGit) RevParse(arg1 string) (string, error) { + fake.revParseMutex.Lock() + ret, specificReturn := fake.revParseReturnsOnCall[len(fake.revParseArgsForCall)] + fake.revParseArgsForCall = append(fake.revParseArgsForCall, struct { + arg1 string + }{arg1}) + fake.recordInvocation("RevParse", []interface{}{arg1}) + fake.revParseMutex.Unlock() + if fake.RevParseStub != nil { + return fake.RevParseStub(arg1) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.revParseReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *FakeGit) RevParseCallCount() int { + fake.revParseMutex.RLock() + defer fake.revParseMutex.RUnlock() + return len(fake.revParseArgsForCall) +} + +func (fake *FakeGit) RevParseCalls(stub func(string) (string, error)) { + fake.revParseMutex.Lock() + defer fake.revParseMutex.Unlock() + fake.RevParseStub = stub +} + +func (fake *FakeGit) RevParseArgsForCall(i int) string { + fake.revParseMutex.RLock() + defer fake.revParseMutex.RUnlock() + argsForCall := fake.revParseArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeGit) RevParseReturns(result1 string, result2 error) { + fake.revParseMutex.Lock() + defer fake.revParseMutex.Unlock() + fake.RevParseStub = nil + fake.revParseReturns = struct { + result1 string + result2 error + }{result1, result2} +} + +func (fake *FakeGit) RevParseReturnsOnCall(i int, result1 string, result2 error) { + fake.revParseMutex.Lock() + defer fake.revParseMutex.Unlock() + fake.RevParseStub = nil + if fake.revParseReturnsOnCall == nil { + fake.revParseReturnsOnCall = make(map[int]struct { + result1 string + result2 error + }) + } + fake.revParseReturnsOnCall[i] = struct { + result1 string + result2 error + }{result1, result2} +} + +func (fake *FakeGit) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.fetchMutex.RLock() + defer fake.fetchMutex.RUnlock() + fake.gitCryptUnlockMutex.RLock() + defer fake.gitCryptUnlockMutex.RUnlock() + fake.initMutex.RLock() + defer fake.initMutex.RUnlock() + fake.mergeMutex.RLock() + defer fake.mergeMutex.RUnlock() + fake.pullMutex.RLock() + defer fake.pullMutex.RUnlock() + fake.revParseMutex.RLock() + defer fake.revParseMutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *FakeGit) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +} + +var _ resource.Git = new(FakeGit) diff --git a/fakes/fake_github.go b/fakes/fake_github.go new file mode 100644 index 00000000..fffca5aa --- /dev/null +++ b/fakes/fake_github.go @@ -0,0 +1,416 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package fakes + +import ( + "sync" + + resource "github.com/telia-oss/github-pr-resource" +) + +type FakeGithub struct { + GetPullRequestStub func(string, string) (*resource.PullRequest, error) + getPullRequestMutex sync.RWMutex + getPullRequestArgsForCall []struct { + arg1 string + arg2 string + } + getPullRequestReturns struct { + result1 *resource.PullRequest + result2 error + } + getPullRequestReturnsOnCall map[int]struct { + result1 *resource.PullRequest + result2 error + } + ListModifiedFilesStub func(int) ([]string, error) + listModifiedFilesMutex sync.RWMutex + listModifiedFilesArgsForCall []struct { + arg1 int + } + listModifiedFilesReturns struct { + result1 []string + result2 error + } + listModifiedFilesReturnsOnCall map[int]struct { + result1 []string + result2 error + } + ListOpenPullRequestsStub func() ([]*resource.PullRequest, error) + listOpenPullRequestsMutex sync.RWMutex + listOpenPullRequestsArgsForCall []struct { + } + listOpenPullRequestsReturns struct { + result1 []*resource.PullRequest + result2 error + } + listOpenPullRequestsReturnsOnCall map[int]struct { + result1 []*resource.PullRequest + result2 error + } + PostCommentStub func(string, string) error + postCommentMutex sync.RWMutex + postCommentArgsForCall []struct { + arg1 string + arg2 string + } + postCommentReturns struct { + result1 error + } + postCommentReturnsOnCall map[int]struct { + result1 error + } + UpdateCommitStatusStub func(string, string, string) error + updateCommitStatusMutex sync.RWMutex + updateCommitStatusArgsForCall []struct { + arg1 string + arg2 string + arg3 string + } + updateCommitStatusReturns struct { + result1 error + } + updateCommitStatusReturnsOnCall map[int]struct { + result1 error + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *FakeGithub) GetPullRequest(arg1 string, arg2 string) (*resource.PullRequest, error) { + fake.getPullRequestMutex.Lock() + ret, specificReturn := fake.getPullRequestReturnsOnCall[len(fake.getPullRequestArgsForCall)] + fake.getPullRequestArgsForCall = append(fake.getPullRequestArgsForCall, struct { + arg1 string + arg2 string + }{arg1, arg2}) + fake.recordInvocation("GetPullRequest", []interface{}{arg1, arg2}) + fake.getPullRequestMutex.Unlock() + if fake.GetPullRequestStub != nil { + return fake.GetPullRequestStub(arg1, arg2) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.getPullRequestReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *FakeGithub) GetPullRequestCallCount() int { + fake.getPullRequestMutex.RLock() + defer fake.getPullRequestMutex.RUnlock() + return len(fake.getPullRequestArgsForCall) +} + +func (fake *FakeGithub) GetPullRequestCalls(stub func(string, string) (*resource.PullRequest, error)) { + fake.getPullRequestMutex.Lock() + defer fake.getPullRequestMutex.Unlock() + fake.GetPullRequestStub = stub +} + +func (fake *FakeGithub) GetPullRequestArgsForCall(i int) (string, string) { + fake.getPullRequestMutex.RLock() + defer fake.getPullRequestMutex.RUnlock() + argsForCall := fake.getPullRequestArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2 +} + +func (fake *FakeGithub) GetPullRequestReturns(result1 *resource.PullRequest, result2 error) { + fake.getPullRequestMutex.Lock() + defer fake.getPullRequestMutex.Unlock() + fake.GetPullRequestStub = nil + fake.getPullRequestReturns = struct { + result1 *resource.PullRequest + result2 error + }{result1, result2} +} + +func (fake *FakeGithub) GetPullRequestReturnsOnCall(i int, result1 *resource.PullRequest, result2 error) { + fake.getPullRequestMutex.Lock() + defer fake.getPullRequestMutex.Unlock() + fake.GetPullRequestStub = nil + if fake.getPullRequestReturnsOnCall == nil { + fake.getPullRequestReturnsOnCall = make(map[int]struct { + result1 *resource.PullRequest + result2 error + }) + } + fake.getPullRequestReturnsOnCall[i] = struct { + result1 *resource.PullRequest + result2 error + }{result1, result2} +} + +func (fake *FakeGithub) ListModifiedFiles(arg1 int) ([]string, error) { + fake.listModifiedFilesMutex.Lock() + ret, specificReturn := fake.listModifiedFilesReturnsOnCall[len(fake.listModifiedFilesArgsForCall)] + fake.listModifiedFilesArgsForCall = append(fake.listModifiedFilesArgsForCall, struct { + arg1 int + }{arg1}) + fake.recordInvocation("ListModifiedFiles", []interface{}{arg1}) + fake.listModifiedFilesMutex.Unlock() + if fake.ListModifiedFilesStub != nil { + return fake.ListModifiedFilesStub(arg1) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.listModifiedFilesReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *FakeGithub) ListModifiedFilesCallCount() int { + fake.listModifiedFilesMutex.RLock() + defer fake.listModifiedFilesMutex.RUnlock() + return len(fake.listModifiedFilesArgsForCall) +} + +func (fake *FakeGithub) ListModifiedFilesCalls(stub func(int) ([]string, error)) { + fake.listModifiedFilesMutex.Lock() + defer fake.listModifiedFilesMutex.Unlock() + fake.ListModifiedFilesStub = stub +} + +func (fake *FakeGithub) ListModifiedFilesArgsForCall(i int) int { + fake.listModifiedFilesMutex.RLock() + defer fake.listModifiedFilesMutex.RUnlock() + argsForCall := fake.listModifiedFilesArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeGithub) ListModifiedFilesReturns(result1 []string, result2 error) { + fake.listModifiedFilesMutex.Lock() + defer fake.listModifiedFilesMutex.Unlock() + fake.ListModifiedFilesStub = nil + fake.listModifiedFilesReturns = struct { + result1 []string + result2 error + }{result1, result2} +} + +func (fake *FakeGithub) ListModifiedFilesReturnsOnCall(i int, result1 []string, result2 error) { + fake.listModifiedFilesMutex.Lock() + defer fake.listModifiedFilesMutex.Unlock() + fake.ListModifiedFilesStub = nil + if fake.listModifiedFilesReturnsOnCall == nil { + fake.listModifiedFilesReturnsOnCall = make(map[int]struct { + result1 []string + result2 error + }) + } + fake.listModifiedFilesReturnsOnCall[i] = struct { + result1 []string + result2 error + }{result1, result2} +} + +func (fake *FakeGithub) ListOpenPullRequests() ([]*resource.PullRequest, error) { + fake.listOpenPullRequestsMutex.Lock() + ret, specificReturn := fake.listOpenPullRequestsReturnsOnCall[len(fake.listOpenPullRequestsArgsForCall)] + fake.listOpenPullRequestsArgsForCall = append(fake.listOpenPullRequestsArgsForCall, struct { + }{}) + fake.recordInvocation("ListOpenPullRequests", []interface{}{}) + fake.listOpenPullRequestsMutex.Unlock() + if fake.ListOpenPullRequestsStub != nil { + return fake.ListOpenPullRequestsStub() + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.listOpenPullRequestsReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *FakeGithub) ListOpenPullRequestsCallCount() int { + fake.listOpenPullRequestsMutex.RLock() + defer fake.listOpenPullRequestsMutex.RUnlock() + return len(fake.listOpenPullRequestsArgsForCall) +} + +func (fake *FakeGithub) ListOpenPullRequestsCalls(stub func() ([]*resource.PullRequest, error)) { + fake.listOpenPullRequestsMutex.Lock() + defer fake.listOpenPullRequestsMutex.Unlock() + fake.ListOpenPullRequestsStub = stub +} + +func (fake *FakeGithub) ListOpenPullRequestsReturns(result1 []*resource.PullRequest, result2 error) { + fake.listOpenPullRequestsMutex.Lock() + defer fake.listOpenPullRequestsMutex.Unlock() + fake.ListOpenPullRequestsStub = nil + fake.listOpenPullRequestsReturns = struct { + result1 []*resource.PullRequest + result2 error + }{result1, result2} +} + +func (fake *FakeGithub) ListOpenPullRequestsReturnsOnCall(i int, result1 []*resource.PullRequest, result2 error) { + fake.listOpenPullRequestsMutex.Lock() + defer fake.listOpenPullRequestsMutex.Unlock() + fake.ListOpenPullRequestsStub = nil + if fake.listOpenPullRequestsReturnsOnCall == nil { + fake.listOpenPullRequestsReturnsOnCall = make(map[int]struct { + result1 []*resource.PullRequest + result2 error + }) + } + fake.listOpenPullRequestsReturnsOnCall[i] = struct { + result1 []*resource.PullRequest + result2 error + }{result1, result2} +} + +func (fake *FakeGithub) PostComment(arg1 string, arg2 string) error { + fake.postCommentMutex.Lock() + ret, specificReturn := fake.postCommentReturnsOnCall[len(fake.postCommentArgsForCall)] + fake.postCommentArgsForCall = append(fake.postCommentArgsForCall, struct { + arg1 string + arg2 string + }{arg1, arg2}) + fake.recordInvocation("PostComment", []interface{}{arg1, arg2}) + fake.postCommentMutex.Unlock() + if fake.PostCommentStub != nil { + return fake.PostCommentStub(arg1, arg2) + } + if specificReturn { + return ret.result1 + } + fakeReturns := fake.postCommentReturns + return fakeReturns.result1 +} + +func (fake *FakeGithub) PostCommentCallCount() int { + fake.postCommentMutex.RLock() + defer fake.postCommentMutex.RUnlock() + return len(fake.postCommentArgsForCall) +} + +func (fake *FakeGithub) PostCommentCalls(stub func(string, string) error) { + fake.postCommentMutex.Lock() + defer fake.postCommentMutex.Unlock() + fake.PostCommentStub = stub +} + +func (fake *FakeGithub) PostCommentArgsForCall(i int) (string, string) { + fake.postCommentMutex.RLock() + defer fake.postCommentMutex.RUnlock() + argsForCall := fake.postCommentArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2 +} + +func (fake *FakeGithub) PostCommentReturns(result1 error) { + fake.postCommentMutex.Lock() + defer fake.postCommentMutex.Unlock() + fake.PostCommentStub = nil + fake.postCommentReturns = struct { + result1 error + }{result1} +} + +func (fake *FakeGithub) PostCommentReturnsOnCall(i int, result1 error) { + fake.postCommentMutex.Lock() + defer fake.postCommentMutex.Unlock() + fake.PostCommentStub = nil + if fake.postCommentReturnsOnCall == nil { + fake.postCommentReturnsOnCall = make(map[int]struct { + result1 error + }) + } + fake.postCommentReturnsOnCall[i] = struct { + result1 error + }{result1} +} + +func (fake *FakeGithub) UpdateCommitStatus(arg1 string, arg2 string, arg3 string) error { + fake.updateCommitStatusMutex.Lock() + ret, specificReturn := fake.updateCommitStatusReturnsOnCall[len(fake.updateCommitStatusArgsForCall)] + fake.updateCommitStatusArgsForCall = append(fake.updateCommitStatusArgsForCall, struct { + arg1 string + arg2 string + arg3 string + }{arg1, arg2, arg3}) + fake.recordInvocation("UpdateCommitStatus", []interface{}{arg1, arg2, arg3}) + fake.updateCommitStatusMutex.Unlock() + if fake.UpdateCommitStatusStub != nil { + return fake.UpdateCommitStatusStub(arg1, arg2, arg3) + } + if specificReturn { + return ret.result1 + } + fakeReturns := fake.updateCommitStatusReturns + return fakeReturns.result1 +} + +func (fake *FakeGithub) UpdateCommitStatusCallCount() int { + fake.updateCommitStatusMutex.RLock() + defer fake.updateCommitStatusMutex.RUnlock() + return len(fake.updateCommitStatusArgsForCall) +} + +func (fake *FakeGithub) UpdateCommitStatusCalls(stub func(string, string, string) error) { + fake.updateCommitStatusMutex.Lock() + defer fake.updateCommitStatusMutex.Unlock() + fake.UpdateCommitStatusStub = stub +} + +func (fake *FakeGithub) UpdateCommitStatusArgsForCall(i int) (string, string, string) { + fake.updateCommitStatusMutex.RLock() + defer fake.updateCommitStatusMutex.RUnlock() + argsForCall := fake.updateCommitStatusArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3 +} + +func (fake *FakeGithub) UpdateCommitStatusReturns(result1 error) { + fake.updateCommitStatusMutex.Lock() + defer fake.updateCommitStatusMutex.Unlock() + fake.UpdateCommitStatusStub = nil + fake.updateCommitStatusReturns = struct { + result1 error + }{result1} +} + +func (fake *FakeGithub) UpdateCommitStatusReturnsOnCall(i int, result1 error) { + fake.updateCommitStatusMutex.Lock() + defer fake.updateCommitStatusMutex.Unlock() + fake.UpdateCommitStatusStub = nil + if fake.updateCommitStatusReturnsOnCall == nil { + fake.updateCommitStatusReturnsOnCall = make(map[int]struct { + result1 error + }) + } + fake.updateCommitStatusReturnsOnCall[i] = struct { + result1 error + }{result1} +} + +func (fake *FakeGithub) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.getPullRequestMutex.RLock() + defer fake.getPullRequestMutex.RUnlock() + fake.listModifiedFilesMutex.RLock() + defer fake.listModifiedFilesMutex.RUnlock() + fake.listOpenPullRequestsMutex.RLock() + defer fake.listOpenPullRequestsMutex.RUnlock() + fake.postCommentMutex.RLock() + defer fake.postCommentMutex.RUnlock() + fake.updateCommitStatusMutex.RLock() + defer fake.updateCommitStatusMutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *FakeGithub) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +} + +var _ resource.Github = new(FakeGithub) diff --git a/git.go b/git.go index fd426ad6..5c65b860 100644 --- a/git.go +++ b/git.go @@ -14,7 +14,7 @@ import ( ) // Git interface for testing purposes. -//go:generate mockgen -destination=mocks/mock_git.go -package=mocks github.com/telia-oss/github-pr-resource Git +//go:generate counterfeiter -o fakes/fake_git.go . Git type Git interface { Init(string) error Pull(string, string) error diff --git a/github.go b/github.go index b1d583ad..144b25a7 100644 --- a/github.go +++ b/github.go @@ -17,7 +17,7 @@ import ( ) // Github for testing purposes. -//go:generate mockgen -destination=mocks/mock_github.go -package=mocks github.com/telia-oss/github-pr-resource Github +//go:generate counterfeiter -o fakes/fake_github.go . Github type Github interface { ListOpenPullRequests() ([]*PullRequest, error) ListModifiedFiles(int) ([]string, error) diff --git a/go.mod b/go.mod index c7c871c7..f78b1a32 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,12 @@ module github.com/telia-oss/github-pr-resource require ( - github.com/golang/mock v1.1.1 github.com/google/go-github v17.0.0+incompatible github.com/google/go-querystring v1.0.0 // indirect github.com/shurcooL/githubv4 v0.0.0-20180925043049-51d7b505e2e9 github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e // indirect github.com/shurcooL/graphql v0.0.0-20180924043259-e4a3a37e6d42 // indirect + github.com/stretchr/testify v1.3.0 golang.org/x/net v0.0.0-20181029044818-c44066c5c816 // indirect golang.org/x/oauth2 v0.0.0-20181031022657-8527f56f7107 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect diff --git a/go.sum b/go.sum index ed63aeb4..49936d04 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,22 @@ -github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shurcooL/githubv4 v0.0.0-20180925043049-51d7b505e2e9 h1:cppRIvEpuZcSdhbhyJZ/3ThCPYlx6xuZg8Qid/0+bz0= github.com/shurcooL/githubv4 v0.0.0-20180925043049-51d7b505e2e9/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo= github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e h1:MZM7FHLqUHYI0Y/mQAt3d2aYa0SiNms/hFqC9qJYolM= github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk= github.com/shurcooL/graphql v0.0.0-20180924043259-e4a3a37e6d42 h1:YIoQLhvoRcfiL0pyxqkESFZXa7jQrcfLTUSSUeyYMO8= github.com/shurcooL/graphql v0.0.0-20180924043259-e4a3a37e6d42/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181029044818-c44066c5c816 h1:mVFkLpejdFLXVUv9E42f3XJVfMdqd0IVLVIVLjZWn5o= golang.org/x/net v0.0.0-20181029044818-c44066c5c816/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/in_test.go b/in_test.go index 05e75e0f..e5a5d2e7 100644 --- a/in_test.go +++ b/in_test.go @@ -5,15 +5,14 @@ import ( "io/ioutil" "os" "path/filepath" - "reflect" "strconv" "testing" "time" - "github.com/golang/mock/gomock" "github.com/shurcooL/githubv4" - "github.com/telia-oss/github-pr-resource" - "github.com/telia-oss/github-pr-resource/mocks" + "github.com/stretchr/testify/assert" + resource "github.com/telia-oss/github-pr-resource" + "github.com/telia-oss/github-pr-resource/fakes" ) func TestGet(t *testing.T) { @@ -43,47 +42,91 @@ func TestGet(t *testing.T) { versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`, metadataString: `[{"name":"pr","value":"1"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"}]`, }, + { + description: "get supports unlocking with git crypt", + source: resource.Source{ + Repository: "itsdalmo/test-repository", + AccessToken: "oauthtoken", + GitCryptKey: "gitcryptkey", + }, + version: resource.Version{ + PR: "pr1", + Commit: "commit1", + CommittedDate: time.Time{}, + }, + parameters: resource.GetParameters{}, + pullRequest: createTestPR(1, false, false), + versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`, + metadataString: `[{"name":"pr","value":"1"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"}]`, + }, } for _, tc := range tests { t.Run(tc.description, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() + github := new(fakes.FakeGithub) + github.GetPullRequestReturns(tc.pullRequest, nil) - github := mocks.NewMockGithub(ctrl) - github.EXPECT().GetPullRequest(tc.version.PR, tc.version.Commit).Times(1).Return(tc.pullRequest, nil) - - git := mocks.NewMockGit(ctrl) - gomock.InOrder( - git.EXPECT().Init(tc.pullRequest.BaseRefName).Times(1).Return(nil), - git.EXPECT().Pull(tc.pullRequest.Repository.URL, tc.pullRequest.BaseRefName).Times(1).Return(nil), - git.EXPECT().RevParse(tc.pullRequest.BaseRefName).Times(1).Return("sha", nil), - git.EXPECT().Fetch(tc.pullRequest.Repository.URL, tc.pullRequest.Number).Times(1).Return(nil), - git.EXPECT().Merge(tc.pullRequest.Tip.OID).Times(1).Return(nil), - ) + git := new(fakes.FakeGit) + git.RevParseReturns("sha", nil) dir := createTestDirectory(t) defer os.RemoveAll(dir) - // Run the get and check output input := resource.GetRequest{Source: tc.source, Version: tc.version, Params: tc.parameters} output, err := resource.Get(input, github, git, dir) - if err != nil { - t.Fatalf("unexpected error: %s", err) + + // Validate output + if assert.NoError(t, err) { + assert.Equal(t, tc.version, output.Version) + + // Verify written files + version := readTestFile(t, filepath.Join(dir, ".git", "resource", "version.json")) + assert.Equal(t, tc.versionString, version) + + metadata := readTestFile(t, filepath.Join(dir, ".git", "resource", "metadata.json")) + assert.Equal(t, tc.metadataString, metadata) } - if got, want := output.Version, tc.version; !reflect.DeepEqual(got, want) { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) + + // Validate Github calls + if assert.Equal(t, 1, github.GetPullRequestCallCount()) { + pr, commit := github.GetPullRequestArgsForCall(0) + assert.Equal(t, tc.version.PR, pr) + assert.Equal(t, tc.version.Commit, commit) + } + + // Validate Git calls + if assert.Equal(t, 1, git.InitCallCount()) { + base := git.InitArgsForCall(0) + assert.Equal(t, tc.pullRequest.BaseRefName, base) + } + + if assert.Equal(t, 1, git.PullCallCount()) { + url, base := git.PullArgsForCall(0) + assert.Equal(t, tc.pullRequest.Repository.URL, url) + assert.Equal(t, tc.pullRequest.BaseRefName, base) } - // Verify written files - version := readTestFile(t, filepath.Join(dir, ".git", "resource", "version.json")) - if got, want := version, tc.versionString; got != want { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) + if assert.Equal(t, 1, git.RevParseCallCount()) { + base := git.RevParseArgsForCall(0) + assert.Equal(t, tc.pullRequest.BaseRefName, base) } - metadata := readTestFile(t, filepath.Join(dir, ".git", "resource", "metadata.json")) - if got, want := metadata, tc.metadataString; got != want { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) + if assert.Equal(t, 1, git.FetchCallCount()) { + url, pr := git.FetchArgsForCall(0) + assert.Equal(t, tc.pullRequest.Repository.URL, url) + assert.Equal(t, tc.pullRequest.Number, pr) + } + + if assert.Equal(t, 1, git.MergeCallCount()) { + tip := git.MergeArgsForCall(0) + assert.Equal(t, tc.pullRequest.Tip.OID, tip) + } + + if tc.source.GitCryptKey != "" { + if assert.Equal(t, 1, git.GitCryptUnlockCallCount()) { + key := git.GitCryptUnlockArgsForCall(0) + assert.Equal(t, tc.source.GitCryptKey, key) + } } }) } @@ -114,97 +157,17 @@ func TestGetSkipDownload(t *testing.T) { for _, tc := range tests { t.Run(tc.description, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - github := mocks.NewMockGithub(ctrl) - git := mocks.NewMockGit(ctrl) + github := new(fakes.FakeGithub) + git := new(fakes.FakeGit) dir := createTestDirectory(t) defer os.RemoveAll(dir) // Run the get and check output input := resource.GetRequest{Source: tc.source, Version: tc.version, Params: tc.parameters} output, err := resource.Get(input, github, git, dir) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } - if got, want := output.Version, tc.version; !reflect.DeepEqual(got, want) { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) - } - }) - } -} - -func TestGetGitCrypt(t *testing.T) { - - tests := []struct { - description string - source resource.Source - version resource.Version - parameters resource.GetParameters - pullRequest *resource.PullRequest - versionString string - metadataString string - }{ - { - description: "get works", - source: resource.Source{ - Repository: "itsdalmo/test-repository", - AccessToken: "oauthtoken", - GitCryptKey: "gitcryptkey", - }, - version: resource.Version{ - PR: "pr1", - Commit: "commit1", - CommittedDate: time.Time{}, - }, - parameters: resource.GetParameters{}, - pullRequest: createTestPR(1, false, false), - versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`, - metadataString: `[{"name":"pr","value":"1"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"}]`, - }, - } - - for _, tc := range tests { - t.Run(tc.description, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - github := mocks.NewMockGithub(ctrl) - github.EXPECT().GetPullRequest(tc.version.PR, tc.version.Commit).Times(1).Return(tc.pullRequest, nil) - - git := mocks.NewMockGit(ctrl) - gomock.InOrder( - git.EXPECT().Init(tc.pullRequest.BaseRefName).Times(1).Return(nil), - git.EXPECT().Pull(tc.pullRequest.Repository.URL, tc.pullRequest.BaseRefName).Times(1).Return(nil), - git.EXPECT().RevParse(tc.pullRequest.BaseRefName).Times(1).Return("sha", nil), - git.EXPECT().Fetch(tc.pullRequest.Repository.URL, tc.pullRequest.Number).Times(1).Return(nil), - git.EXPECT().Merge(tc.pullRequest.Tip.OID).Times(1).Return(nil), - git.EXPECT().GitCryptUnlock(tc.source.GitCryptKey).Times(1).Return(nil), - ) - - dir := createTestDirectory(t) - defer os.RemoveAll(dir) - - // Run the get and check output - input := resource.GetRequest{Source: tc.source, Version: tc.version, Params: tc.parameters} - output, err := resource.Get(input, github, git, dir) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } - if got, want := output.Version, tc.version; !reflect.DeepEqual(got, want) { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) - } - - // Verify written files - version := readTestFile(t, filepath.Join(dir, ".git", "resource", "version.json")) - if got, want := version, tc.versionString; got != want { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) - } - metadata := readTestFile(t, filepath.Join(dir, ".git", "resource", "metadata.json")) - if got, want := metadata, tc.metadataString; got != want { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) + if assert.NoError(t, err) { + assert.Equal(t, tc.version, output.Version) } }) } diff --git a/mocks/mock_git.go b/mocks/mock_git.go deleted file mode 100644 index fbcce615..00000000 --- a/mocks/mock_git.go +++ /dev/null @@ -1,106 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/telia-oss/github-pr-resource (interfaces: Git) - -// Package mocks is a generated GoMock package. -package mocks - -import ( - gomock "github.com/golang/mock/gomock" - reflect "reflect" -) - -// MockGit is a mock of Git interface -type MockGit struct { - ctrl *gomock.Controller - recorder *MockGitMockRecorder -} - -// MockGitMockRecorder is the mock recorder for MockGit -type MockGitMockRecorder struct { - mock *MockGit -} - -// NewMockGit creates a new mock instance -func NewMockGit(ctrl *gomock.Controller) *MockGit { - mock := &MockGit{ctrl: ctrl} - mock.recorder = &MockGitMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockGit) EXPECT() *MockGitMockRecorder { - return m.recorder -} - -// Fetch mocks base method -func (m *MockGit) Fetch(arg0 string, arg1 int) error { - ret := m.ctrl.Call(m, "Fetch", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// Fetch indicates an expected call of Fetch -func (mr *MockGitMockRecorder) Fetch(arg0, arg1 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Fetch", reflect.TypeOf((*MockGit)(nil).Fetch), arg0, arg1) -} - -// GitCryptUnlock mocks base method -func (m *MockGit) GitCryptUnlock(arg0 string) error { - ret := m.ctrl.Call(m, "GitCryptUnlock", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// GitCryptUnlock indicates an expected call of GitCryptUnlock -func (mr *MockGitMockRecorder) GitCryptUnlock(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GitCryptUnlock", reflect.TypeOf((*MockGit)(nil).GitCryptUnlock), arg0) -} - -// Init mocks base method -func (m *MockGit) Init(arg0 string) error { - ret := m.ctrl.Call(m, "Init", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// Init indicates an expected call of Init -func (mr *MockGitMockRecorder) Init(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Init", reflect.TypeOf((*MockGit)(nil).Init), arg0) -} - -// Merge mocks base method -func (m *MockGit) Merge(arg0 string) error { - ret := m.ctrl.Call(m, "Merge", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// Merge indicates an expected call of Merge -func (mr *MockGitMockRecorder) Merge(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Merge", reflect.TypeOf((*MockGit)(nil).Merge), arg0) -} - -// Pull mocks base method -func (m *MockGit) Pull(arg0, arg1 string) error { - ret := m.ctrl.Call(m, "Pull", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// Pull indicates an expected call of Pull -func (mr *MockGitMockRecorder) Pull(arg0, arg1 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Pull", reflect.TypeOf((*MockGit)(nil).Pull), arg0, arg1) -} - -// RevParse mocks base method -func (m *MockGit) RevParse(arg0 string) (string, error) { - ret := m.ctrl.Call(m, "RevParse", arg0) - ret0, _ := ret[0].(string) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// RevParse indicates an expected call of RevParse -func (mr *MockGitMockRecorder) RevParse(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RevParse", reflect.TypeOf((*MockGit)(nil).RevParse), arg0) -} diff --git a/mocks/mock_github.go b/mocks/mock_github.go deleted file mode 100644 index 1e05d426..00000000 --- a/mocks/mock_github.go +++ /dev/null @@ -1,97 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/telia-oss/github-pr-resource (interfaces: Github) - -// Package mocks is a generated GoMock package. -package mocks - -import ( - gomock "github.com/golang/mock/gomock" - github_pr_resource "github.com/telia-oss/github-pr-resource" - reflect "reflect" -) - -// MockGithub is a mock of Github interface -type MockGithub struct { - ctrl *gomock.Controller - recorder *MockGithubMockRecorder -} - -// MockGithubMockRecorder is the mock recorder for MockGithub -type MockGithubMockRecorder struct { - mock *MockGithub -} - -// NewMockGithub creates a new mock instance -func NewMockGithub(ctrl *gomock.Controller) *MockGithub { - mock := &MockGithub{ctrl: ctrl} - mock.recorder = &MockGithubMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockGithub) EXPECT() *MockGithubMockRecorder { - return m.recorder -} - -// GetPullRequest mocks base method -func (m *MockGithub) GetPullRequest(arg0, arg1 string) (*github_pr_resource.PullRequest, error) { - ret := m.ctrl.Call(m, "GetPullRequest", arg0, arg1) - ret0, _ := ret[0].(*github_pr_resource.PullRequest) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetPullRequest indicates an expected call of GetPullRequest -func (mr *MockGithubMockRecorder) GetPullRequest(arg0, arg1 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPullRequest", reflect.TypeOf((*MockGithub)(nil).GetPullRequest), arg0, arg1) -} - -// ListModifiedFiles mocks base method -func (m *MockGithub) ListModifiedFiles(arg0 int) ([]string, error) { - ret := m.ctrl.Call(m, "ListModifiedFiles", arg0) - ret0, _ := ret[0].([]string) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ListModifiedFiles indicates an expected call of ListModifiedFiles -func (mr *MockGithubMockRecorder) ListModifiedFiles(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListModifiedFiles", reflect.TypeOf((*MockGithub)(nil).ListModifiedFiles), arg0) -} - -// ListOpenPullRequests mocks base method -func (m *MockGithub) ListOpenPullRequests() ([]*github_pr_resource.PullRequest, error) { - ret := m.ctrl.Call(m, "ListOpenPullRequests") - ret0, _ := ret[0].([]*github_pr_resource.PullRequest) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ListOpenPullRequests indicates an expected call of ListOpenPullRequests -func (mr *MockGithubMockRecorder) ListOpenPullRequests() *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListOpenPullRequests", reflect.TypeOf((*MockGithub)(nil).ListOpenPullRequests)) -} - -// PostComment mocks base method -func (m *MockGithub) PostComment(arg0, arg1 string) error { - ret := m.ctrl.Call(m, "PostComment", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// PostComment indicates an expected call of PostComment -func (mr *MockGithubMockRecorder) PostComment(arg0, arg1 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PostComment", reflect.TypeOf((*MockGithub)(nil).PostComment), arg0, arg1) -} - -// UpdateCommitStatus mocks base method -func (m *MockGithub) UpdateCommitStatus(arg0, arg1, arg2 string) error { - ret := m.ctrl.Call(m, "UpdateCommitStatus", arg0, arg1, arg2) - ret0, _ := ret[0].(error) - return ret0 -} - -// UpdateCommitStatus indicates an expected call of UpdateCommitStatus -func (mr *MockGithubMockRecorder) UpdateCommitStatus(arg0, arg1, arg2 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateCommitStatus", reflect.TypeOf((*MockGithub)(nil).UpdateCommitStatus), arg0, arg1, arg2) -} diff --git a/out_test.go b/out_test.go index a4a8cc70..e5d5b78b 100644 --- a/out_test.go +++ b/out_test.go @@ -2,13 +2,13 @@ package resource_test import ( "os" - "reflect" "testing" "time" - "github.com/golang/mock/gomock" - "github.com/telia-oss/github-pr-resource" - "github.com/telia-oss/github-pr-resource/mocks" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + resource "github.com/telia-oss/github-pr-resource" + "github.com/telia-oss/github-pr-resource/fakes" ) func TestPut(t *testing.T) { @@ -90,47 +90,44 @@ func TestPut(t *testing.T) { for _, tc := range tests { t.Run(tc.description, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() + github := new(fakes.FakeGithub) + github.GetPullRequestReturns(tc.pullRequest, nil) - github := mocks.NewMockGithub(ctrl) - github.EXPECT().GetPullRequest(tc.version.PR, tc.version.Commit).Times(1).Return(tc.pullRequest, nil) - - git := mocks.NewMockGit(ctrl) - gomock.InOrder( - git.EXPECT().Init(tc.pullRequest.BaseRefName).Times(1).Return(nil), - git.EXPECT().Pull(tc.pullRequest.Repository.URL, tc.pullRequest.BaseRefName).Times(1).Return(nil), - git.EXPECT().RevParse(tc.pullRequest.BaseRefName).Times(1).Return("sha", nil), - git.EXPECT().Fetch(tc.pullRequest.Repository.URL, tc.pullRequest.Number).Times(1).Return(nil), - git.EXPECT().Merge(tc.pullRequest.Tip.OID).Times(1).Return(nil), - ) + git := new(fakes.FakeGit) + git.RevParseReturns("sha", nil) dir := createTestDirectory(t) defer os.RemoveAll(dir) // Run get so we have version and metadata for the put request + // (This is tested in in_test.go) getInput := resource.GetRequest{Source: tc.source, Version: tc.version, Params: resource.GetParameters{}} _, err := resource.Get(getInput, github, git, dir) - if err != nil { - t.Fatalf("unexpected error: %s", err) + require.NoError(t, err) + + putInput := resource.PutRequest{Source: tc.source, Params: tc.parameters} + output, err := resource.Put(putInput, github, dir) + + // Validate output + if assert.NoError(t, err) { + assert.Equal(t, tc.version, output.Version) } - // Set expectations + // Validate method calls put on Github. if tc.parameters.Status != "" { - github.EXPECT().UpdateCommitStatus(tc.version.Commit, tc.parameters.Context, tc.parameters.Status).Times(1).Return(nil) + if assert.Equal(t, 1, github.UpdateCommitStatusCallCount()) { + commit, context, status := github.UpdateCommitStatusArgsForCall(0) + assert.Equal(t, tc.version.Commit, commit) + assert.Equal(t, tc.parameters.Context, context) + assert.Equal(t, tc.parameters.Status, status) + } } if tc.parameters.Comment != "" { - github.EXPECT().PostComment(tc.version.PR, tc.parameters.Comment).Times(1).Return(nil) - } - - // Run put and verify output - putInput := resource.PutRequest{Source: tc.source, Params: tc.parameters} - output, err := resource.Put(putInput, github, dir) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } - if got, want := output.Version, tc.version; !reflect.DeepEqual(got, want) { - t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want) + if assert.Equal(t, 1, github.PostCommentCallCount()) { + pr, comment := github.PostCommentArgsForCall(0) + assert.Equal(t, tc.version.PR, pr) + assert.Equal(t, tc.parameters.Comment, comment) + } } }) }