From 5fa37539ded111a003e31daf36a7636d56c2f5a7 Mon Sep 17 00:00:00 2001 From: Gusted Date: Wed, 18 Jun 2025 21:12:08 +0200 Subject: [PATCH] chore: sort mailer messages in test assertion (#8226) - Ref https://codeberg.org/forgejo/forgejo/issues/8221#issuecomment-5461218 - Databases might return users in a different order, sort them before doing assertions on them. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8226 Reviewed-by: Earl Warren Co-authored-by: Gusted Co-committed-by: Gusted --- tests/integration/integration_test.go | 8 ++++++++ tests/integration/pull_review_test.go | 2 ++ 2 files changed, 10 insertions(+) diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 4f49408157..34a975de44 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -19,6 +19,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strconv" "strings" "sync/atomic" @@ -40,6 +41,7 @@ import ( "forgejo.org/routers" "forgejo.org/services/auth/source/remote" gitea_context "forgejo.org/services/context" + "forgejo.org/services/mailer" user_service "forgejo.org/services/user" "forgejo.org/tests" @@ -694,3 +696,9 @@ func GetHTMLTitle(t testing.TB, session *TestSession, urlStr string) string { doc := NewHTMLParser(t, resp.Body) return doc.Find("head title").Text() } + +func SortMailerMessages(msgs []*mailer.Message) { + slices.SortFunc(msgs, func(a, b *mailer.Message) int { + return strings.Compare(b.To, a.To) + }) +} diff --git a/tests/integration/pull_review_test.go b/tests/integration/pull_review_test.go index 0c05b3da37..603252f45f 100644 --- a/tests/integration/pull_review_test.go +++ b/tests/integration/pull_review_test.go @@ -680,6 +680,7 @@ func TestPullRequestReplyMail(t *testing.T) { called := false defer test.MockVariableValue(&mailer.SendAsync, func(msgs ...*mailer.Message) { assert.Len(t, msgs, 2) + SortMailerMessages(msgs) assert.Equal(t, "user1@example.com", msgs[0].To) assert.Equal(t, "Re: [user2/repo1] issue2 (PR #2)", msgs[0].Subject) assert.Contains(t, msgs[0].Body, "Notification time!") @@ -708,6 +709,7 @@ func TestPullRequestReplyMail(t *testing.T) { called := false defer test.MockVariableValue(&mailer.SendAsync, func(msgs ...*mailer.Message) { assert.Len(t, msgs, 2) + SortMailerMessages(msgs) assert.Equal(t, "user1@example.com", msgs[0].To) assert.Equal(t, "Re: [user2/repo1] issue2 (PR #2)", msgs[0].Subject) assert.Contains(t, msgs[0].Body, "Notification time 2!")