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:
silverwind authored and GitHub committed 2026-10-22 01:27:02 +00:00
1 parent ef55965d98
commit 98c613d943
20 files changed
+124 -66

No files matched your search

+3 -4
View File
@@ -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
}
+4 -2
View File
@@ -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, "&", "&")