mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-23 01:33:44 +02: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
@@ -9,6 +9,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/models/unittest"
|
||||
"gitea.dev/modules/test"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -16,6 +17,7 @@ import (
|
||||
)
|
||||
|
||||
func TestDbfsBasic(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
defer test.MockVariableValue(&defaultFileBlockSize, 4)()
|
||||
|
||||
// test basic write/read
|
||||
@@ -165,6 +167,7 @@ func TestDbfsBasic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDbfsReadWrite(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
defer test.MockVariableValue(&defaultFileBlockSize, 4)()
|
||||
|
||||
f1, err := OpenFile(t.Context(), "test.log", os.O_RDWR|os.O_CREATE)
|
||||
@@ -197,6 +200,7 @@ func TestDbfsReadWrite(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDbfsSeekWrite(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
defer test.MockVariableValue(&defaultFileBlockSize, 4)()
|
||||
|
||||
// write something
|
||||
|
||||
+16
-40
@@ -4,25 +4,28 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/test"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func mockUnitConfig(t *testing.T) {
|
||||
disabledRepoUnits := DisabledRepoUnitsGet()
|
||||
t.Cleanup(func() { DisabledRepoUnitsSet(disabledRepoUnits) })
|
||||
t.Cleanup(test.MockVariableValue(&DefaultRepoUnits, slices.Clone(DefaultRepoUnits)))
|
||||
t.Cleanup(test.MockVariableValue(&DefaultForkRepoUnits, slices.Clone(DefaultForkRepoUnits)))
|
||||
t.Cleanup(test.MockVariableValue(&DefaultMirrorRepoUnits, slices.Clone(DefaultMirrorRepoUnits)))
|
||||
t.Cleanup(test.MockVariableValue(&DefaultTemplateRepoUnits, slices.Clone(DefaultTemplateRepoUnits)))
|
||||
t.Cleanup(test.MockVariableValue(&setting.Repository))
|
||||
}
|
||||
|
||||
func TestLoadUnitConfig(t *testing.T) {
|
||||
t.Run("regular", func(t *testing.T) {
|
||||
defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []Type) {
|
||||
DisabledRepoUnitsSet(disabledRepoUnits)
|
||||
DefaultRepoUnits = defaultRepoUnits
|
||||
DefaultForkRepoUnits = defaultForkRepoUnits
|
||||
}(DisabledRepoUnitsGet(), DefaultRepoUnits, DefaultForkRepoUnits)
|
||||
defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []string) {
|
||||
setting.Repository.DisabledRepoUnits = disabledRepoUnits
|
||||
setting.Repository.DefaultRepoUnits = defaultRepoUnits
|
||||
setting.Repository.DefaultForkRepoUnits = defaultForkRepoUnits
|
||||
}(setting.Repository.DisabledRepoUnits, setting.Repository.DefaultRepoUnits, setting.Repository.DefaultForkRepoUnits)
|
||||
mockUnitConfig(t)
|
||||
|
||||
setting.Repository.DisabledRepoUnits = []string{"repo.issues"}
|
||||
setting.Repository.DefaultRepoUnits = []string{"repo.code", "repo.releases", "repo.issues", "repo.pulls"}
|
||||
@@ -33,16 +36,7 @@ func TestLoadUnitConfig(t *testing.T) {
|
||||
assert.Equal(t, []Type{TypeReleases}, DefaultForkRepoUnits)
|
||||
})
|
||||
t.Run("invalid", func(t *testing.T) {
|
||||
defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []Type) {
|
||||
DisabledRepoUnitsSet(disabledRepoUnits)
|
||||
DefaultRepoUnits = defaultRepoUnits
|
||||
DefaultForkRepoUnits = defaultForkRepoUnits
|
||||
}(DisabledRepoUnitsGet(), DefaultRepoUnits, DefaultForkRepoUnits)
|
||||
defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []string) {
|
||||
setting.Repository.DisabledRepoUnits = disabledRepoUnits
|
||||
setting.Repository.DefaultRepoUnits = defaultRepoUnits
|
||||
setting.Repository.DefaultForkRepoUnits = defaultForkRepoUnits
|
||||
}(setting.Repository.DisabledRepoUnits, setting.Repository.DefaultRepoUnits, setting.Repository.DefaultForkRepoUnits)
|
||||
mockUnitConfig(t)
|
||||
|
||||
setting.Repository.DisabledRepoUnits = []string{"repo.issues", "invalid.1"}
|
||||
setting.Repository.DefaultRepoUnits = []string{"repo.code", "invalid.2", "repo.releases", "repo.issues", "repo.pulls"}
|
||||
@@ -53,16 +47,7 @@ func TestLoadUnitConfig(t *testing.T) {
|
||||
assert.Equal(t, []Type{TypeReleases}, DefaultForkRepoUnits)
|
||||
})
|
||||
t.Run("duplicate", func(t *testing.T) {
|
||||
defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []Type) {
|
||||
DisabledRepoUnitsSet(disabledRepoUnits)
|
||||
DefaultRepoUnits = defaultRepoUnits
|
||||
DefaultForkRepoUnits = defaultForkRepoUnits
|
||||
}(DisabledRepoUnitsGet(), DefaultRepoUnits, DefaultForkRepoUnits)
|
||||
defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []string) {
|
||||
setting.Repository.DisabledRepoUnits = disabledRepoUnits
|
||||
setting.Repository.DefaultRepoUnits = defaultRepoUnits
|
||||
setting.Repository.DefaultForkRepoUnits = defaultForkRepoUnits
|
||||
}(setting.Repository.DisabledRepoUnits, setting.Repository.DefaultRepoUnits, setting.Repository.DefaultForkRepoUnits)
|
||||
mockUnitConfig(t)
|
||||
|
||||
setting.Repository.DisabledRepoUnits = []string{"repo.issues", "repo.issues"}
|
||||
setting.Repository.DefaultRepoUnits = []string{"repo.code", "repo.releases", "repo.issues", "repo.pulls", "repo.code"}
|
||||
@@ -73,16 +58,7 @@ func TestLoadUnitConfig(t *testing.T) {
|
||||
assert.Equal(t, []Type{TypeReleases}, DefaultForkRepoUnits)
|
||||
})
|
||||
t.Run("empty_default", func(t *testing.T) {
|
||||
defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []Type) {
|
||||
DisabledRepoUnitsSet(disabledRepoUnits)
|
||||
DefaultRepoUnits = defaultRepoUnits
|
||||
DefaultForkRepoUnits = defaultForkRepoUnits
|
||||
}(DisabledRepoUnitsGet(), DefaultRepoUnits, DefaultForkRepoUnits)
|
||||
defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []string) {
|
||||
setting.Repository.DisabledRepoUnits = disabledRepoUnits
|
||||
setting.Repository.DefaultRepoUnits = defaultRepoUnits
|
||||
setting.Repository.DefaultForkRepoUnits = defaultForkRepoUnits
|
||||
}(setting.Repository.DisabledRepoUnits, setting.Repository.DefaultRepoUnits, setting.Repository.DefaultForkRepoUnits)
|
||||
mockUnitConfig(t)
|
||||
|
||||
setting.Repository.DisabledRepoUnits = []string{"repo.issues", "repo.issues"}
|
||||
setting.Repository.DefaultRepoUnits = []string{}
|
||||
|
||||
@@ -169,10 +169,21 @@ func LoadFixtures() error {
|
||||
return err
|
||||
}
|
||||
// Now if we're running postgres we need to tell it to update the sequences
|
||||
if GetXORMEngine().Dialect().URI().DBType == schemas.POSTGRES {
|
||||
switch GetXORMEngine().Dialect().URI().DBType {
|
||||
case schemas.POSTGRES:
|
||||
if err := loadFixtureResetSeqPgsql(GetXORMEngine()); err != nil {
|
||||
return err
|
||||
}
|
||||
case schemas.SQLITE:
|
||||
hasSequence, err := GetXORMEngine().IsTableExist("sqlite_sequence") // only exists once an AUTOINCREMENT table was created
|
||||
if err != nil {
|
||||
return fmt.Errorf("LoadFixtures: %w", err)
|
||||
}
|
||||
if hasSequence {
|
||||
if _, err := GetXORMEngine().DB().DB.Exec("DELETE FROM sqlite_sequence"); err != nil {
|
||||
return fmt.Errorf("LoadFixtures: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -128,6 +128,8 @@ func TestUserEmails(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCanCreateOrganization(t *testing.T) {
|
||||
defer test.MockVariableValue(&setting.Admin.DisableRegularOrgCreation)()
|
||||
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||
|
||||
@@ -15,6 +15,5 @@ func TestMain(m *testing.M) {
|
||||
"webhook.yml",
|
||||
"hook_task.yml",
|
||||
},
|
||||
SetUp: prepareWebhookTestData,
|
||||
})
|
||||
}
|
||||
@@ -14,6 +14,8 @@ import (
|
||||
)
|
||||
|
||||
func TestListSystemWebhookOptions(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hookSystem := unittest.AssertExistsAndLoadBean(t, &Webhook{URL: "https://www.example.com/system"})
|
||||
hookDefault := unittest.AssertExistsAndLoadBean(t, &Webhook{URL: "https://www.example.com/default"})
|
||||
opts := ListSystemWebhookOptions{IsSystem: optional.None[bool]()}
|
||||
|
||||
@@ -74,9 +74,6 @@ func prepareWebhookTestData() error {
|
||||
Events: `{"push_only":true,"branch_filter":"{master,feature*}"}`,
|
||||
})
|
||||
ctx := context.Background()
|
||||
if err := db.TruncateBeans(ctx, &Webhook{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.Insert(ctx, hooks); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -86,9 +83,6 @@ func prepareWebhookTestData() error {
|
||||
tasks = append(tasks, &HookTask{HookID: hook.ID, UUID: uuid.New().String()})
|
||||
tasks = append(tasks, &HookTask{HookID: hook.ID, UUID: uuid.New().String()})
|
||||
tasks = append(tasks, &HookTask{HookID: hook.ID, UUID: uuid.New().String()})
|
||||
if err := db.TruncateBeans(ctx, &HookTask{}); err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Insert(ctx, tasks)
|
||||
}
|
||||
|
||||
@@ -101,6 +95,8 @@ func TestWebHookContentType(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWebhook_History(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, IsActive: true})
|
||||
tasks, err := hook.History(t.Context(), 0)
|
||||
require.NoError(t, err)
|
||||
@@ -113,7 +109,6 @@ func TestWebhook_History(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWebhook_UpdateEvent(t *testing.T) {
|
||||
webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, IsActive: true})
|
||||
hookEvent := &webhook_module.HookEvent{
|
||||
PushOnly: true,
|
||||
SendEverything: false,
|
||||
@@ -124,7 +119,7 @@ func TestWebhook_UpdateEvent(t *testing.T) {
|
||||
webhook_module.HookEventPullRequest: false,
|
||||
},
|
||||
}
|
||||
webhook.HookEvent = hookEvent
|
||||
webhook := &Webhook{HookEvent: hookEvent}
|
||||
assert.NoError(t, webhook.UpdateEvent())
|
||||
assert.NotEmpty(t, webhook.Events)
|
||||
actualHookEvent := &webhook_module.HookEvent{}
|
||||
@@ -154,6 +149,8 @@ func TestWebhook_EventsArray(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateWebhook(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
hook := &Webhook{
|
||||
RepoID: 3,
|
||||
URL: "https://www.example.com/unit_test",
|
||||
@@ -166,6 +163,8 @@ func TestCreateWebhook(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetWebhookByRepoID(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, IsActive: true})
|
||||
loaded, err := GetWebhookByRepoID(t.Context(), 1, hook.ID)
|
||||
assert.NoError(t, err)
|
||||
@@ -177,6 +176,8 @@ func TestGetWebhookByRepoID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetWebhookByOwnerID(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{OwnerID: 3})
|
||||
loaded, err := GetWebhookByOwnerID(t.Context(), 3, hook.ID)
|
||||
require.NoError(t, err)
|
||||
@@ -188,6 +189,8 @@ func TestGetWebhookByOwnerID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetActiveWebhooksByRepoID(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, IsActive: true})
|
||||
hooks, err := db.Find[Webhook](t.Context(), ListWebhookOptions{RepoID: 1, IsActive: optional.Some(true)})
|
||||
require.NoError(t, err)
|
||||
@@ -197,6 +200,8 @@ func TestGetActiveWebhooksByRepoID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetWebhooksByRepoID(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hooks, err := db.Find[Webhook](t.Context(), ListWebhookOptions{RepoID: 1})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, hooks, 2)
|
||||
@@ -207,6 +212,8 @@ func TestGetWebhooksByRepoID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetActiveWebhooksByOwnerID(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hooks, err := db.Find[Webhook](t.Context(), ListWebhookOptions{OwnerID: 3, IsActive: optional.Some(true)})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, hooks, 1)
|
||||
@@ -215,6 +222,8 @@ func TestGetActiveWebhooksByOwnerID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetWebhooksByOwnerID(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hooks, err := db.Find[Webhook](t.Context(), ListWebhookOptions{OwnerID: 3})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, hooks, 2)
|
||||
@@ -225,6 +234,8 @@ func TestGetWebhooksByOwnerID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateWebhook(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, Events: `{}`})
|
||||
require.False(t, hook.IsActive)
|
||||
hook.IsActive = true
|
||||
@@ -235,6 +246,8 @@ func TestUpdateWebhook(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteWebhookByRepoID(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, Events: `{}`})
|
||||
assert.NoError(t, DeleteWebhookByRepoID(t.Context(), 1, hook.ID))
|
||||
unittest.AssertNotExistsBean(t, &Webhook{ID: hook.ID, RepoID: 1})
|
||||
@@ -244,6 +257,8 @@ func TestDeleteWebhookByRepoID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteWebhookByOwnerID(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{OwnerID: 3, Events: `{}`})
|
||||
assert.NoError(t, DeleteWebhookByOwnerID(t.Context(), 3, hook.ID))
|
||||
unittest.AssertNotExistsBean(t, &Webhook{ID: hook.ID, OwnerID: 3})
|
||||
@@ -253,6 +268,8 @@ func TestDeleteWebhookByOwnerID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHookTasks(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, IsActive: true})
|
||||
hookTasks, err := HookTasks(t.Context(), hook.ID, 1)
|
||||
assert.NoError(t, err)
|
||||
@@ -263,6 +280,8 @@ func TestHookTasks(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateHookTask(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{OwnerID: 3, IsActive: true})
|
||||
hookTask := &HookTask{HookID: hook.ID, PayloadVersion: 2}
|
||||
unittest.AssertNotExistsBean(t, hookTask)
|
||||
@@ -272,6 +291,8 @@ func TestCreateHookTask(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateHookTask(t *testing.T) {
|
||||
require.NoError(t, prepareWebhookTestData())
|
||||
|
||||
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{OwnerID: 3, IsActive: true})
|
||||
hookTask := &HookTask{HookID: hook.ID, PayloadVersion: 2}
|
||||
_, err := CreateHookTask(t.Context(), hookTask)
|
||||
@@ -285,6 +306,8 @@ func TestUpdateHookTask(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
hook := &Webhook{RepoID: 3, URL: "https://www.example.com/cleanup1", ContentType: ContentTypeJSON, Events: `{"push_only":true}`}
|
||||
require.NoError(t, db.Insert(t.Context(), hook))
|
||||
hookTask := &HookTask{
|
||||
@@ -303,6 +326,8 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
hook := &Webhook{RepoID: 3, URL: "https://www.example.com/cleanup2", ContentType: ContentTypeJSON, Events: `{"push_only":true}`}
|
||||
require.NoError(t, db.Insert(t.Context(), hook))
|
||||
hookTask := &HookTask{
|
||||
@@ -320,6 +345,8 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
hook := &Webhook{RepoID: 3, URL: "https://www.example.com/cleanup3", ContentType: ContentTypeJSON, Events: `{"push_only":true}`}
|
||||
require.NoError(t, db.Insert(t.Context(), hook))
|
||||
hookTask := &HookTask{
|
||||
@@ -338,6 +365,8 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
hook := &Webhook{RepoID: 3, URL: "https://www.example.com/cleanup4", ContentType: ContentTypeJSON, Events: `{"push_only":true}`}
|
||||
require.NoError(t, db.Insert(t.Context(), hook))
|
||||
hookTask := &HookTask{
|
||||
@@ -356,6 +385,8 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
hook := &Webhook{RepoID: 3, URL: "https://www.example.com/cleanup5", ContentType: ContentTypeJSON, Events: `{"push_only":true}`}
|
||||
require.NoError(t, db.Insert(t.Context(), hook))
|
||||
hookTask := &HookTask{
|
||||
@@ -373,6 +404,8 @@ func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
hook := &Webhook{RepoID: 3, URL: "https://www.example.com/cleanup6", ContentType: ContentTypeJSON, Events: `{"push_only":true}`}
|
||||
require.NoError(t, db.Insert(t.Context(), hook))
|
||||
hookTask := &HookTask{
|
||||
|
||||
@@ -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