From 428ee9fcce7928bf5405900345d43e9ba1b01564 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 28 May 2026 10:53:38 -0700 Subject: [PATCH] fix(testing): Fix random failure test (#37887) Fix the flaky npm package web view test that compared rendered HTML as a raw string. Fix https://github.com/go-gitea/gitea/actions/runs/26524574688/job/78124662707?pr=36564 --------- Co-authored-by: wxiaoguang Co-authored-by: Nicolas --- tests/integration/api_packages_npm_test.go | 2 +- tests/integration/html_helper.go | 39 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/integration/api_packages_npm_test.go b/tests/integration/api_packages_npm_test.go index 54d972d47d..1159b64da8 100644 --- a/tests/integration/api_packages_npm_test.go +++ b/tests/integration/api_packages_npm_test.go @@ -304,7 +304,7 @@ func TestPackageNpm(t *testing.T) { resp := MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) rendered, _ := doc.Find(".markup.markdown").Html() - assert.Equal(t, `

docs + assertHTMLEq(t, `

docs logo

`, rendered) }) diff --git a/tests/integration/html_helper.go b/tests/integration/html_helper.go index c2045453e6..cefe5592c4 100644 --- a/tests/integration/html_helper.go +++ b/tests/integration/html_helper.go @@ -5,10 +5,13 @@ package integration import ( "io" + "slices" + "strings" "testing" "github.com/PuerkitoBio/goquery" "github.com/stretchr/testify/assert" + "golang.org/x/net/html" ) // HTMLDoc struct @@ -47,3 +50,39 @@ func AssertHTMLElement[T int | bool](t testing.TB, doc *HTMLDoc, selector string assert.Equal(t, v, sel.Length()) } } + +func assertHTMLEq(t testing.TB, expected, actual string) { + t.Helper() + if expected == actual { + return + } + exp, err := html.Parse(strings.NewReader(expected)) + if !assert.NoError(t, err) { + return + } + act, err := html.Parse(strings.NewReader(actual)) + if !assert.NoError(t, err) { + return + } + var normalize func(n *html.Node) + normalize = func(n *html.Node) { + slices.SortFunc(n.Attr, func(a, b html.Attribute) int { + if cmp := strings.Compare(a.Namespace, b.Namespace); cmp != 0 { + return cmp + } + if cmp := strings.Compare(a.Key, b.Key); cmp != 0 { + return cmp + } + return strings.Compare(a.Val, b.Val) + }) + for c := n.FirstChild; c != nil; c = c.NextSibling { + normalize(c) + } + } + normalize(exp) + normalize(act) + var expNormalized, actNormalized strings.Builder + assert.NoError(t, html.Render(&expNormalized, exp)) + assert.NoError(t, html.Render(&actNormalized, act)) + assert.Equal(t, expNormalized.String(), actNormalized.String()) +}