@@ -2527,35 +2527,32 @@ func (tester *PullRequestCommentPlacementTester) withBranchCheckout(action func(
25272527}
25282528
25292529func (tester * PullRequestCommentPlacementTester ) assertFilesChangedDiff (expectedCommitID string , rowAssertions []diffTableRow , note ... string ) {
2530- req := NewRequest (tester .t , "GET" ,
2531- fmt .Sprintf ("/%s/%s/pulls/%d/files" , tester .repo .OwnerName , tester .repo .Name , tester .pr .Index ))
2532- resp := tester .session .MakeRequest (tester .t , req , http .StatusOK )
2533- doc := NewHTMLParser (tester .t , resp .Body )
2530+ fetchPage := func () * HTMLDoc {
2531+ req := NewRequest (tester .t , "GET" ,
2532+ fmt .Sprintf ("/%s/%s/pulls/%d/files" , tester .repo .OwnerName , tester .repo .Name , tester .pr .Index ))
2533+ resp := tester .session .MakeRequest (tester .t , req , http .StatusOK )
2534+ return NewHTMLParser (tester .t , resp .Body )
2535+ }
25342536
2537+ var doc * HTMLDoc
25352538 if expectedCommitID != "" {
2536- commitIDInput := doc .Find ("input[name=commit_id]" )
2537- if uiCommitID , exists := commitIDInput .Attr ("value" ); exists {
2538- if uiCommitID != expectedCommitID {
2539- // In attempting to understand the intermittent test failure in
2540- // https://codeberg.org/forgejo/forgejo/issues/13275, it has been identified that the wrong commit can be
2541- // fetched from `/%s/%s/pulls/%d/files` after a change is made to the PR. Because the test failure is
2542- // intermittent, perhaps it is a race condition of some kind? When this situation is discovered here, retry
2543- // fetching the page and see if the commit ID changes in order to validate that it is a race condition.
2544- tester .t .Logf ("expected commit ID on the files changed page to be %q, but it was actually %q" , expectedCommitID , uiCommitID )
2545-
2546- for i := range 10 {
2547- time .Sleep (time .Second )
2548- req := NewRequest (tester .t , "GET" ,
2549- fmt .Sprintf ("/%s/%s/pulls/%d/files" , tester .repo .OwnerName , tester .repo .Name , tester .pr .Index ))
2550- resp := tester .session .MakeRequest (tester .t , req , http .StatusOK )
2551- doc := NewHTMLParser (tester .t , resp .Body )
2552- commitIDInput := doc .Find ("input[name=commit_id]" )
2553- uiCommitID , exists := commitIDInput .Attr ("value" )
2554- tester .t .Logf ("after %d seconds, page is reloaded and commit ID is now %q (%v)" , i , uiCommitID , exists )
2555- }
2556- tester .t .FailNow ()
2557- }
2558- }
2539+ // In services/pull/pull.go, `TestPullRequest` (which is, to be clear, not a test) will spawn a concurrent
2540+ // goroutine during the post-receive hook. This goroutine is responsible for updating the refs/pull/%d/head
2541+ // reference in the repo, which is what the UI uses in order to render the pull request diff page. Because this
2542+ // routine is concurrent, occasionally the test will reach this point and it won't yet be completed.
2543+ // Experimental testing shows that it completes very fast (under a 1 second retry) on CI, so here we give it 10
2544+ // seconds to reach the expected commit ID, reloading the page each time.
2545+ require .EventuallyWithT (tester .t , func (collect * assert.CollectT ) {
2546+ doc = fetchPage ()
2547+ commitIDInput := doc .Find ("input[name=commit_id]" )
2548+ uiCommitID , exists := commitIDInput .Attr ("value" )
2549+ require .True (collect , exists )
2550+ assert .Equal (collect , expectedCommitID , uiCommitID )
2551+ }, 10 * time .Second , time .Second , "expected to find PR diff page with commit ID %s" , expectedCommitID )
2552+ } else {
2553+ // For some tests we don't know the expected commit ID for rendering the page, typically when a force push is
2554+ // used and we're not using Forgejo's API which provides the commit ID. No retry loop here.
2555+ doc = fetchPage ()
25592556 }
25602557
25612558 var testNote string
@@ -2671,17 +2668,6 @@ func checkDiffTableRow(t *testing.T, tableRow *html.Node, rowAssertion diffTable
26712668func assertDiffTable (t * testing.T , doc * HTMLDoc , rowAssertions []diffTableRow , note string ) {
26722669 require .NotEmpty (t , rowAssertions )
26732670
2674- logCommitID := func () {
2675- // Investigation for https://codeberg.org/forgejo/forgejo/issues/13275
2676- //
2677- // If we couldn't find the diff that we expected, find the hidden <input name="commit_id"> on the page (if
2678- // possible) and log the commit ID that we're currently viewing, so that we can see if it is the correct commit
2679- // ID.
2680- commitIDInput := doc .Find ("input[name=commit_id]" )
2681- uiCommitID , exists := commitIDInput .Attr ("value" )
2682- t .Logf ("input name=commit_id exists? %v, value = %q" , exists , uiCommitID )
2683- }
2684-
26852671 diffTable := doc .Find ("table.chroma" )
26862672 require .Equal (t , 1 , diffTable .Length ())
26872673
@@ -2706,7 +2692,6 @@ func assertDiffTable(t *testing.T, doc *HTMLDoc, rowAssertions []diffTableRow, n
27062692 for _ , mm := range firstRowMismatches {
27072693 t .Logf ("\t %s" , mm )
27082694 }
2709- logCommitID ()
27102695 require .Failf (t , "unable to find first row" , "test %s: failed to find first row assertion" , note ) // assert place
27112696 }
27122697
@@ -2717,14 +2702,12 @@ func assertDiffTable(t *testing.T, doc *HTMLDoc, rowAssertions []diffTableRow, n
27172702
27182703 tableIdx := tableFirstRowIndex + idx
27192704 if tableIdx >= rows .Length () {
2720- logCommitID ()
27212705 require .Failf (t , "ran out of table rows" , "test %s: row assertion at index %d couldn't be satisfied" , note , idx )
27222706 }
27232707
27242708 tableRow := rows .Get (tableIdx )
27252709 check := checkDiffTableRow (t , tableRow , assertion )
27262710 if check != "" {
2727- logCommitID ()
27282711 assert .Failf (t , check , "test %s: row assertion at index %d couldn't be satisfied" , note , idx )
27292712 }
27302713 }
0 commit comments