mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-26 06:16:56 +09:00
test: fix unit tests failing on repeated runs (#39915)
`go test -count=2` failed in 12 packages because tests leaked state into later runs. - Reset SQLite sequences after each fixture load, like PostgreSQL already does - Prepare the database in tests that relied on earlier tests doing it - Restore mutated globals with `test.MockVariableValue`
This commit is contained in:
1 parent
ef55965d98
commit
98c613d943
20 files changed
+124
-66
No files matched your search
@@ -174,6 +174,8 @@ func TestEntries_GetCommitsInfo_ContextErr(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer repo.Close()
|
||||
|
||||
repo.LastCommitCache.ttlFn = func() int64 { return -1 }
|
||||
|
||||
commit, err := repo.GetCommit(t.Context(), "feaf4ba6bc635fec442f46ddd4512416ec43c2c2")
|
||||
require.NoError(t, err)
|
||||
entries, err := commit.Tree().ListEntries(t.Context(), repo)
|
||||
|
||||
@@ -23,6 +23,8 @@ func gitConfigContains(sub string) bool {
|
||||
}
|
||||
|
||||
func TestGitConfig(t *testing.T) {
|
||||
defer test.MockVariableValue(&setting.Git.HomePath, t.TempDir())()
|
||||
|
||||
ctx := t.Context()
|
||||
assert.False(t, gitConfigContains("key-a"))
|
||||
|
||||
@@ -61,7 +63,9 @@ func TestGitConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSyncGitConfig(t *testing.T) {
|
||||
defer test.MockVariableValue(&setting.GitConfig)()
|
||||
defer test.MockVariableValue(&setting.Git.HomePath, t.TempDir())()
|
||||
defer test.MockVariableValue(&setting.GitConfig.Options, map[string]string{})()
|
||||
defer test.MockVariableValue(&GlobalConfig)()
|
||||
|
||||
assert.Empty(t, setting.GitConfig.Options)
|
||||
assert.NoError(t, configSet(t.Context(), "gc.reflogExpire", "90"))
|
||||
|
||||
@@ -79,8 +79,9 @@ func TestIndexer(t *testing.T, indexer internal.Indexer) {
|
||||
}
|
||||
|
||||
// test counting
|
||||
c.SearchOptions.Paginator = &db.ListOptions{PageSize: 0}
|
||||
countResult, err := indexer.Search(t.Context(), c.SearchOptions)
|
||||
countOptions := c.SearchOptions.Copy()
|
||||
countOptions.Paginator = &db.ListOptions{PageSize: 0}
|
||||
countResult, err := indexer.Search(t.Context(), countOptions)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, countResult.Hits)
|
||||
assert.Equal(t, result.Total, countResult.Total)
|
||||
|
||||
@@ -7,12 +7,14 @@ import (
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/setting"
|
||||
testModule "gitea.dev/modules/test"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCamoHandleLink(t *testing.T) {
|
||||
setting.AppURL = "https://gitea.com"
|
||||
defer testModule.MockVariableValue(&setting.AppURL, "https://gitea.com")()
|
||||
defer testModule.MockVariableValue(&setting.Camo)()
|
||||
// Test media proxy
|
||||
setting.Camo.Enabled = true
|
||||
setting.Camo.Always = false
|
||||
@@ -42,7 +44,4 @@ func TestCamoHandleLink(t *testing.T) {
|
||||
assert.Equal(t,
|
||||
"https://image.proxy/eivin43gJwGVIjR9MiYYtFIk0mw/aHR0cDovL3Rlc3RpbWFnZXMub3JnL2ltZy5qcGc",
|
||||
camoHandleLink("http://testimages.org/img.jpg"))
|
||||
|
||||
// Restore previous settings
|
||||
setting.Camo.Enabled = false
|
||||
}
|
||||
@@ -5,6 +5,7 @@ package markup_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"maps"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -326,8 +327,9 @@ func TestRender_email(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRender_emoji(t *testing.T) {
|
||||
setting.AppURL = markup.TestAppURL
|
||||
setting.StaticURLPrefix = strings.TrimSuffix(markup.TestAppURL, "/")
|
||||
defer testModule.MockVariableValue(&setting.AppURL, markup.TestAppURL)()
|
||||
defer testModule.MockVariableValue(&setting.StaticURLPrefix, strings.TrimSuffix(markup.TestAppURL, "/"))()
|
||||
defer testModule.MockVariableValue(&setting.UI.CustomEmojisMap, maps.Clone(setting.UI.CustomEmojisMap))()
|
||||
|
||||
test := func(input, expected string) {
|
||||
expected = strings.ReplaceAll(expected, "&", "&")
|
||||
|
||||
@@ -8,12 +8,14 @@ import (
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/test"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestManager(t *testing.T) {
|
||||
setting.AppDataPath = t.TempDir()
|
||||
defer test.MockVariableValue(&setting.AppDataPath, t.TempDir())()
|
||||
defer test.MockVariableValue(&manager, &Manager{Queues: make(map[int64]ManagedWorkerPoolQueue)})()
|
||||
|
||||
newQueueFromConfig := func(name, cfg string) (*WorkerPoolQueue[int], error) {
|
||||
cfgProvider, err := setting.NewConfigProviderFromData(cfg)
|
||||
|
||||
@@ -10,16 +10,17 @@ import (
|
||||
)
|
||||
|
||||
func TestRegexpLru(t *testing.T) {
|
||||
r, err := UserCache().GetCompiled("a")
|
||||
cache := NewCache(2)
|
||||
r, err := cache.GetCompiled("a")
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, r.MatchString("a"))
|
||||
|
||||
r, err = UserCache().GetCompiled("a")
|
||||
r, err = cache.GetCompiled("a")
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, r.MatchString("a"))
|
||||
assert.Equal(t, 1, UserCache().lruCache.Len())
|
||||
assert.Equal(t, 1, cache.lruCache.Len())
|
||||
|
||||
_, err = UserCache().GetCompiled("(")
|
||||
_, err = cache.GetCompiled("(")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 2, UserCache().lruCache.Len())
|
||||
assert.Equal(t, 2, cache.lruCache.Len())
|
||||
}
|
||||
@@ -6,6 +6,8 @@ package setting
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/test"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -103,6 +105,9 @@ STORAGE_TYPE = minio
|
||||
}
|
||||
|
||||
func Test_LFSClientServerConfigs(t *testing.T) {
|
||||
defer test.MockVariableValue(&LFS)()
|
||||
defer test.MockVariableValue(&LFSClient)()
|
||||
|
||||
iniStr := `
|
||||
[server]
|
||||
LFS_MAX_BATCH_SIZE = 100
|
||||
|
||||
@@ -6,10 +6,17 @@ package setting
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/test"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLoadMarkup(t *testing.T) {
|
||||
defer test.MockVariableValue(&Markdown)()
|
||||
defer test.MockVariableValue(&ExternalMarkupRenderers)()
|
||||
defer test.MockVariableValue(&ExternalSanitizerRules)()
|
||||
defer test.MockVariableValue(&MermaidMaxSourceCharacters)()
|
||||
|
||||
cfg, _ := NewConfigProviderFromData(``)
|
||||
loadMarkupFrom(cfg)
|
||||
assert.Equal(t, MarkdownMathCodeBlockOptions{ParseInlineDollar: true, ParseBlockDollar: true}, Markdown.MathCodeBlockOptions)
|
||||
|
||||
@@ -60,6 +60,8 @@ func TestGetGeneralSigningSecretSave(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestOauth2DefaultApplications(t *testing.T) {
|
||||
defer test.MockVariableValue(&OAuth2)()
|
||||
|
||||
cfg, _ := NewConfigProviderFromData(``)
|
||||
loadOAuth2From(cfg)
|
||||
assert.Equal(t, []string{"git-credential-oauth", "git-credential-manager", "tea", "gitea-app", "gitea-vscode"}, OAuth2.DefaultApplications)
|
||||
|
||||
@@ -6,10 +6,14 @@ package setting
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/test"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLoadSecurityFrom(t *testing.T) {
|
||||
defer test.MockVariableValue(&Security)()
|
||||
|
||||
assert.Equal(t, "SAMEORIGIN", Security.XFrameOptions)
|
||||
assert.Equal(t, "nosniff", Security.XContentTypeOptions)
|
||||
assert.Equal(t, "lax", Security.EgressMode)
|
||||
|
||||
@@ -33,6 +33,8 @@ func (*testItem2) Name() string {
|
||||
}
|
||||
|
||||
func TestAppStateDB(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
as := &DBStore{}
|
||||
|
||||
item1 := new(testItem1)
|
||||
|
||||
@@ -319,7 +319,7 @@ func TestRenderLabels(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserMention(t *testing.T) {
|
||||
markup.RenderBehaviorForTesting.DisableAdditionalAttributes = true
|
||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
|
||||
rendered := newTestRenderUtils(t).MarkdownToHtml("@no-such-user @mention-user @mention-user")
|
||||
assert.Equal(t, `<p>@no-such-user <a href="/mention-user" rel="nofollow">@mention-user</a> <a href="/mention-user" rel="nofollow">@mention-user</a></p>`, strings.TrimSpace(string(rendered)))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user