From 4cbb43b860548da07132bcec79e42cbc038a9a36 Mon Sep 17 00:00:00 2001 From: ᴜɴᴋɴᴡᴏɴ Date: Sun, 29 Mar 2020 20:15:42 +0800 Subject: gitutil: simplify mock with `t.Cleanup` (#6033) * gitutil: simplify mock with t.Cleanup * Scope mock to tests only --- internal/gitutil/mock.go | 56 ----------------------------- internal/gitutil/mock_test.go | 66 +++++++++++++++++++++++++++++++++++ internal/gitutil/pull_request_test.go | 7 +--- internal/gitutil/tag_test.go | 7 +--- 4 files changed, 68 insertions(+), 68 deletions(-) delete mode 100644 internal/gitutil/mock.go create mode 100644 internal/gitutil/mock_test.go (limited to 'internal/gitutil') diff --git a/internal/gitutil/mock.go b/internal/gitutil/mock.go deleted file mode 100644 index 73727c31..00000000 --- a/internal/gitutil/mock.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2020 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package gitutil - -import ( - "github.com/gogs/git-module" -) - -var _ ModuleStore = (*MockModuleStore)(nil) - -// MockModuleStore is a mock implementation of ModuleStore interface. -type MockModuleStore struct { - repoAddRemote func(repoPath, name, url string, opts ...git.AddRemoteOptions) error - repoDiffNameOnly func(repoPath, base, head string, opts ...git.DiffNameOnlyOptions) ([]string, error) - repoLog func(repoPath, rev string, opts ...git.LogOptions) ([]*git.Commit, error) - repoMergeBase func(repoPath, base, head string, opts ...git.MergeBaseOptions) (string, error) - repoRemoveRemote func(repoPath, name string, opts ...git.RemoveRemoteOptions) error - repoTags func(repoPath string, opts ...git.TagsOptions) ([]string, error) - - pullRequestMeta func(headPath, basePath, headBranch, baseBranch string) (*PullRequestMeta, error) - listTagsAfter func(repoPath, after string, limit int) (*TagsPage, error) -} - -func (m *MockModuleStore) RepoAddRemote(repoPath, name, url string, opts ...git.AddRemoteOptions) error { - return m.repoAddRemote(repoPath, name, url, opts...) -} - -func (m *MockModuleStore) RepoDiffNameOnly(repoPath, base, head string, opts ...git.DiffNameOnlyOptions) ([]string, error) { - return m.repoDiffNameOnly(repoPath, base, head, opts...) -} - -func (m *MockModuleStore) RepoLog(repoPath, rev string, opts ...git.LogOptions) ([]*git.Commit, error) { - return m.repoLog(repoPath, rev, opts...) -} - -func (m *MockModuleStore) RepoMergeBase(repoPath, base, head string, opts ...git.MergeBaseOptions) (string, error) { - return m.repoMergeBase(repoPath, base, head, opts...) -} - -func (m *MockModuleStore) RepoRemoveRemote(repoPath, name string, opts ...git.RemoveRemoteOptions) error { - return m.repoRemoveRemote(repoPath, name, opts...) -} - -func (m *MockModuleStore) RepoTags(repoPath string, opts ...git.TagsOptions) ([]string, error) { - return m.repoTags(repoPath, opts...) -} - -func (m *MockModuleStore) PullRequestMeta(headPath, basePath, headBranch, baseBranch string) (*PullRequestMeta, error) { - return m.pullRequestMeta(headPath, basePath, headBranch, baseBranch) -} - -func (m *MockModuleStore) ListTagsAfter(repoPath, after string, limit int) (*TagsPage, error) { - return m.listTagsAfter(repoPath, after, limit) -} diff --git a/internal/gitutil/mock_test.go b/internal/gitutil/mock_test.go new file mode 100644 index 00000000..161d4474 --- /dev/null +++ b/internal/gitutil/mock_test.go @@ -0,0 +1,66 @@ +// Copyright 2020 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gitutil + +import ( + "testing" + + "github.com/gogs/git-module" +) + +var _ ModuleStore = (*MockModuleStore)(nil) + +// MockModuleStore is a mock implementation of ModuleStore interface. +type MockModuleStore struct { + repoAddRemote func(repoPath, name, url string, opts ...git.AddRemoteOptions) error + repoDiffNameOnly func(repoPath, base, head string, opts ...git.DiffNameOnlyOptions) ([]string, error) + repoLog func(repoPath, rev string, opts ...git.LogOptions) ([]*git.Commit, error) + repoMergeBase func(repoPath, base, head string, opts ...git.MergeBaseOptions) (string, error) + repoRemoveRemote func(repoPath, name string, opts ...git.RemoveRemoteOptions) error + repoTags func(repoPath string, opts ...git.TagsOptions) ([]string, error) + + pullRequestMeta func(headPath, basePath, headBranch, baseBranch string) (*PullRequestMeta, error) + listTagsAfter func(repoPath, after string, limit int) (*TagsPage, error) +} + +func (m *MockModuleStore) RepoAddRemote(repoPath, name, url string, opts ...git.AddRemoteOptions) error { + return m.repoAddRemote(repoPath, name, url, opts...) +} + +func (m *MockModuleStore) RepoDiffNameOnly(repoPath, base, head string, opts ...git.DiffNameOnlyOptions) ([]string, error) { + return m.repoDiffNameOnly(repoPath, base, head, opts...) +} + +func (m *MockModuleStore) RepoLog(repoPath, rev string, opts ...git.LogOptions) ([]*git.Commit, error) { + return m.repoLog(repoPath, rev, opts...) +} + +func (m *MockModuleStore) RepoMergeBase(repoPath, base, head string, opts ...git.MergeBaseOptions) (string, error) { + return m.repoMergeBase(repoPath, base, head, opts...) +} + +func (m *MockModuleStore) RepoRemoveRemote(repoPath, name string, opts ...git.RemoveRemoteOptions) error { + return m.repoRemoveRemote(repoPath, name, opts...) +} + +func (m *MockModuleStore) RepoTags(repoPath string, opts ...git.TagsOptions) ([]string, error) { + return m.repoTags(repoPath, opts...) +} + +func (m *MockModuleStore) PullRequestMeta(headPath, basePath, headBranch, baseBranch string) (*PullRequestMeta, error) { + return m.pullRequestMeta(headPath, basePath, headBranch, baseBranch) +} + +func (m *MockModuleStore) ListTagsAfter(repoPath, after string, limit int) (*TagsPage, error) { + return m.listTagsAfter(repoPath, after, limit) +} + +func SetMockModuleStore(t *testing.T, mock ModuleStore) { + before := Module + Module = mock + t.Cleanup(func() { + Module = before + }) +} diff --git a/internal/gitutil/pull_request_test.go b/internal/gitutil/pull_request_test.go index 1322a674..ca0b40ad 100644 --- a/internal/gitutil/pull_request_test.go +++ b/internal/gitutil/pull_request_test.go @@ -24,7 +24,7 @@ func TestModuler_PullRequestMeta(t *testing.T) { {ID: git.MustIDFromString("adfd6da3c0a3fb038393144becbf37f14f780087")}, } - mockModule := &MockModuleStore{ + SetMockModuleStore(t, &MockModuleStore{ repoAddRemote: func(repoPath, name, url string, opts ...git.AddRemoteOptions) error { if repoPath != headPath { return fmt.Errorf("repoPath: want %q but got %q", headPath, repoPath) @@ -93,11 +93,6 @@ func TestModuler_PullRequestMeta(t *testing.T) { }, pullRequestMeta: Module.PullRequestMeta, - } - beforeModule := Module - Module = mockModule - t.Cleanup(func() { - Module = beforeModule }) meta, err := Module.PullRequestMeta(headPath, basePath, headBranch, baseBranch) diff --git a/internal/gitutil/tag_test.go b/internal/gitutil/tag_test.go index 7f61baf7..f8fb2a03 100644 --- a/internal/gitutil/tag_test.go +++ b/internal/gitutil/tag_test.go @@ -12,7 +12,7 @@ import ( ) func TestModuler_ListTagsAfter(t *testing.T) { - mockModule := &MockModuleStore{ + SetMockModuleStore(t, &MockModuleStore{ repoTags: func(string, ...git.TagsOptions) ([]string, error) { return []string{ "v2.3.0", "v2.2.1", "v2.1.0", @@ -22,11 +22,6 @@ func TestModuler_ListTagsAfter(t *testing.T) { }, listTagsAfter: Module.ListTagsAfter, - } - beforeModule := Module - Module = mockModule - t.Cleanup(func() { - Module = beforeModule }) tests := []struct { -- cgit v1.2.3