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

+2
View File
@@ -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)
+5 -1
View File
@@ -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"))