Decouple unit test code from business code (#17623)

This commit is contained in:
wxiaoguang 2021-11-12 22:36:47 +08:00 committed by GitHub
parent 7f802631c5
commit df64fa4865
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
136 changed files with 1058 additions and 830 deletions

View file

@ -8,13 +8,13 @@ import (
"path/filepath"
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
db.MainTest(m, filepath.Join("..", ".."), "")
unittest.MainTest(m, filepath.Join("..", ".."), "")
}
type testItem1 struct {
@ -35,7 +35,7 @@ func (*testItem2) Name() string {
}
func TestAppStateDB(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
as := &DBStore{}

View file

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
@ -18,7 +19,7 @@ import (
)
func TestToCommitMeta(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
headRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
sha1, _ := git.NewIDFromString("0000000000000000000000000000000000000000")
signature := &git.Signature{Name: "Test Signature", Email: "test@email.com", When: time.Unix(0, 0)}

View file

@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
@ -19,7 +20,7 @@ import (
)
func TestLabel_ToLabel(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
label := db.AssertExistsAndLoadBean(t, &models.Label{ID: 1}).(*models.Label)
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: label.RepoID}).(*models.Repository)
assert.Equal(t, &api.Label{

View file

@ -8,9 +8,9 @@ import (
"path/filepath"
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
)
func TestMain(m *testing.M) {
db.MainTest(m, filepath.Join("..", ".."))
unittest.MainTest(m, filepath.Join("..", ".."))
}

View file

@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
@ -16,7 +17,7 @@ import (
func TestPullRequest_APIFormat(t *testing.T) {
//with HeadRepo
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
headRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
pr := db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
assert.NoError(t, pr.LoadAttributes())

View file

@ -9,13 +9,14 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
api "code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
)
func TestUser_ToUser(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
user1 := db.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User)

View file

@ -8,14 +8,14 @@ import (
"os"
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/util"
"github.com/stretchr/testify/assert"
)
func TestBleveIndexAndSearch(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
dir, err := os.MkdirTemp("", "bleve.index")
assert.NoError(t, err)

View file

@ -8,12 +8,13 @@ import (
"os"
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)
func TestESIndexAndSearch(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
u := os.Getenv("TEST_INDEXER_CODE_ES_URL")
if u == "" {

View file

@ -8,12 +8,13 @@ import (
"path/filepath"
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
db.MainTest(m, filepath.Join("..", "..", ".."))
unittest.MainTest(m, filepath.Join("..", "..", ".."))
}
func testIndexer(name string, t *testing.T, indexer Indexer) {

View file

@ -11,7 +11,7 @@ import (
"testing"
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
@ -21,11 +21,11 @@ import (
)
func TestMain(m *testing.M) {
db.MainTest(m, filepath.Join("..", "..", ".."))
unittest.MainTest(m, filepath.Join("..", "..", ".."))
}
func TestBleveSearchIssues(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
setting.Cfg = ini.Empty()
tmpIndexerDir, err := os.MkdirTemp("", "issues-indexer")
@ -74,7 +74,7 @@ func TestBleveSearchIssues(t *testing.T) {
}
func TestDBSearchIssues(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
setting.Indexer.IssueType = "db"
InitIssueIndexer(true)

View file

@ -10,7 +10,7 @@ import (
"time"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"
"gopkg.in/ini.v1"
@ -19,11 +19,11 @@ import (
)
func TestMain(m *testing.M) {
db.MainTest(m, filepath.Join("..", "..", ".."))
unittest.MainTest(m, filepath.Join("..", "..", ".."))
}
func TestRepoStatsIndex(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
setting.Cfg = ini.Empty()
setting.NewQueueService()

View file

@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/migrations/base"
"code.gitea.io/gitea/modules/structs"
@ -24,7 +25,7 @@ func TestGiteaUploadRepo(t *testing.T) {
// FIXME: Since no accesskey or user/password will trigger rate limit of github, just skip
t.Skip()
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)

View file

@ -10,14 +10,14 @@ import (
"testing"
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/migrations/base"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
db.MainTest(m, filepath.Join("..", ".."))
unittest.MainTest(m, filepath.Join("..", ".."))
}
func timePtr(t time.Time) *time.Time {

View file

@ -10,13 +10,14 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert"
)
func TestMigrateWhiteBlocklist(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
adminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User)
nonAdminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User)

View file

@ -11,15 +11,16 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
db.MainTest(m, filepath.Join("..", "..", ".."))
unittest.MainTest(m, filepath.Join("..", "..", ".."))
}
func TestRenameRepoAction(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
repo := db.AssertExistsAndLoadBean(t, &models.Repository{OwnerID: user.ID}).(*models.Repository)

View file

@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
@ -16,7 +17,7 @@ import (
)
func TestUpdateIssuesCommit(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
pushCommits := []*repository.PushCommit{
{
Sha1: "abcdef1",
@ -118,7 +119,7 @@ func TestUpdateIssuesCommit(t *testing.T) {
}
func TestUpdateIssuesCommit_Colon(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
pushCommits := []*repository.PushCommit{
{
Sha1: "abcdef2",
@ -143,7 +144,7 @@ func TestUpdateIssuesCommit_Colon(t *testing.T) {
}
func TestUpdateIssuesCommit_Issue5957(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
// Test that push to a non-default branch closes an issue.
@ -177,7 +178,7 @@ func TestUpdateIssuesCommit_Issue5957(t *testing.T) {
}
func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
// Test that a push to default branch closes issue in another repo
@ -212,7 +213,7 @@ func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) {
}
func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
// Test that a push to default branch closes issue in another repo
@ -247,7 +248,7 @@ func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) {
}
func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 10}).(*models.User)
// Test that a push with close reference *can not* close issue

View file

@ -7,7 +7,8 @@ package repofiles
import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
@ -15,7 +16,7 @@ import (
)
func TestGetBlobBySHA(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
test.LoadRepo(t, ctx, 1)
test.LoadRepoCommit(t, ctx)

View file

@ -8,7 +8,8 @@ import (
"path/filepath"
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
@ -16,7 +17,7 @@ import (
)
func TestMain(m *testing.M) {
db.MainTest(m, filepath.Join("..", ".."))
unittest.MainTest(m, filepath.Join("..", ".."))
}
func getExpectedReadmeContentsResponse() *api.ContentsResponse {
@ -49,7 +50,7 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse {
}
func TestGetContents(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
@ -77,7 +78,7 @@ func TestGetContents(t *testing.T) {
}
func TestGetContentsOrListForDir(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
@ -112,7 +113,7 @@ func TestGetContentsOrListForDir(t *testing.T) {
}
func TestGetContentsOrListForFile(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
@ -140,7 +141,7 @@ func TestGetContentsOrListForFile(t *testing.T) {
}
func TestGetContentsErrors(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
@ -171,7 +172,7 @@ func TestGetContentsErrors(t *testing.T) {
}
func TestGetContentsOrListErrors(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
@ -202,7 +203,7 @@ func TestGetContentsOrListErrors(t *testing.T) {
}
func TestGetContentsOrListOfEmptyRepos(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo15")
ctx.SetParams(":id", "15")
test.LoadRepo(t, ctx, 15)

View file

@ -8,7 +8,7 @@ import (
"testing"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/services/gitdiff"
@ -16,7 +16,7 @@ import (
)
func TestGetDiffPreview(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
@ -129,7 +129,7 @@ func TestGetDiffPreview(t *testing.T) {
}
func TestGetDiffPreviewErrors(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)

View file

@ -7,7 +7,7 @@ package repofiles
import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
@ -81,7 +81,7 @@ func getExpectedFileResponse() *api.FileResponse {
}
func TestGetFileResponseFromCommit(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)

View file

@ -7,7 +7,8 @@ package repofiles
import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
@ -15,7 +16,7 @@ import (
)
func TestGetTreeBySHA(t *testing.T) {
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
test.LoadRepo(t, ctx, 1)
test.LoadRepoCommit(t, ctx)

View file

@ -12,12 +12,13 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git"
"github.com/stretchr/testify/assert"
)
func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
pushCommits := NewPushCommits()
pushCommits.Commits = []*PushCommit{
@ -100,7 +101,7 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
}
func TestPushCommits_AvatarLink(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
pushCommits := NewPushCommits()
pushCommits.Commits = []*PushCommit{

View file

@ -10,13 +10,14 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
)
func TestIncludesAllRepositoriesTeams(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
testTeamRepositories := func(teamID int64, repoIds []int64) {
team := db.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team)

View file

@ -9,11 +9,12 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)
func TestForkRepository(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
// user 13 has already forked repo10
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 13}).(*models.User)

View file

@ -8,9 +8,9 @@ import (
"path/filepath"
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
)
func TestMain(m *testing.M) {
db.MainTest(m, filepath.Join("..", ".."))
unittest.MainTest(m, filepath.Join("..", ".."))
}

View file

@ -0,0 +1,46 @@
// Copyright 2021 The Gitea 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 unittestbridge
// Usage: generally, non-unit-test code shouldn't depend on unit test code.
// However, we have some code like models.CheckConsistencyFor, which need to do some unit test works.
// Now we can not decouple models.CheckConsistencyFor from unit test code easily (cycle-import reasons).
// So we introduce this `unit test bridge`:
// * When a release binary is built, no testing/assert framework would be compiled into the binary, and CheckConsistencyFor won't run unit test related code
// * When a unit test binary is built, the unit test code will init this bridge, then CheckConsistencyFor can run unit test related code
//
// Tester/Assert are intermediate interfaces, they should NOT be used in new code.
// One day, if CheckConsistencyFor is clean enough, we can remove these intermediate interfaces.
// Tester is the same as TestingT in "stretchr/testify/assert"
// Tester can be used in non-unit-test code (ex: models.CheckConsistencyFor), it is used to isolate dependencies
type Tester interface {
Errorf(format string, args ...interface{})
}
// Asserter can be used in non-unit-test code (ex: models.CheckConsistencyFor), it is used to isolate dependencies
type Asserter interface {
Tester
NoError(err error, msgAndArgs ...interface{}) bool
EqualValues(expected, actual interface{}, msgAndArgs ...interface{}) bool
Equal(expected, actual interface{}, msgAndArgs ...interface{}) bool
True(value bool, msgAndArgs ...interface{}) bool
False(value bool, msgAndArgs ...interface{}) bool
}
var newAsserterFunc func(t Tester) Asserter
// NewAsserter returns a new asserter, only works in unit test
func NewAsserter(t Tester) Asserter {
if newAsserterFunc == nil {
panic("the newAsserterFunc is not set. you can only use assert in unit test.")
}
return newAsserterFunc(t)
}
// SetNewAsserterFunc in unit test, the asserter must be set first
func SetNewAsserterFunc(f func(t Tester) Asserter) {
newAsserterFunc = f
}