From 4cbb43b860548da07132bcec79e42cbc038a9a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=9C=C9=B4=E1=B4=8B=C9=B4=E1=B4=A1=E1=B4=8F=C9=B4?= Date: Sun, 29 Mar 2020 20:15:42 +0800 Subject: [PATCH] gitutil: simplify mock with `t.Cleanup` (#6033) * gitutil: simplify mock with t.Cleanup * Scope mock to tests only --- internal/gitutil/{mock.go => mock_test.go} | 10 ++++++++++ internal/gitutil/pull_request_test.go | 7 +------ internal/gitutil/tag_test.go | 7 +------ 3 files changed, 12 insertions(+), 12 deletions(-) rename internal/gitutil/{mock.go => mock_test.go} (94%) diff --git a/internal/gitutil/mock.go b/internal/gitutil/mock_test.go similarity index 94% rename from internal/gitutil/mock.go rename to internal/gitutil/mock_test.go index 73727c31b..161d44745 100644 --- a/internal/gitutil/mock.go +++ b/internal/gitutil/mock_test.go @@ -5,6 +5,8 @@ package gitutil import ( + "testing" + "github.com/gogs/git-module" ) @@ -54,3 +56,11 @@ func (m *MockModuleStore) PullRequestMeta(headPath, basePath, headBranch, baseBr 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 1322a674a..ca0b40ada 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 7f61baf72..f8fb2a03d 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 {