Skip to content

Commit 69f5f1e

Browse files
committed
Fixed flaky test
1 parent ca047f3 commit 69f5f1e

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

nuget_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10+
"sort"
1011
"strconv"
1112
"strings"
1213
"testing"
@@ -424,11 +425,11 @@ func TestDotnetRequestedByDeterminism(t *testing.T) {
424425
bi := publishedBuildInfo.BuildInfo
425426
require.NotEmpty(t, bi.Modules, "No modules in build info for run %d", i)
426427

427-
// Extract RequestedBy for each dependency
428+
// Extract RequestedBy for each dependency, normalizing order for comparison
428429
requestedByMap := make(map[string][][]string)
429430
for _, module := range bi.Modules {
430431
for _, dep := range module.Dependencies {
431-
requestedByMap[dep.Id] = dep.RequestedBy
432+
requestedByMap[dep.Id] = sortRequestedByPaths(dep.RequestedBy)
432433
}
433434
}
434435
allRunsRequestedBy = append(allRunsRequestedBy, requestedByMap)
@@ -475,3 +476,24 @@ func TestDotnetRequestedByDeterminism(t *testing.T) {
475476
t.Logf("Successfully verified RequestedBy determinism across %d runs", numRuns)
476477
cleanTestsHomeEnv()
477478
}
479+
480+
// sortRequestedByPaths sorts RequestedBy paths for consistent comparison.
481+
// Paths are sorted by length first (shorter paths first), then lexicographically
482+
// for paths of the same length.
483+
func sortRequestedByPaths(paths [][]string) [][]string {
484+
if len(paths) <= 1 {
485+
return paths
486+
}
487+
// Make a copy to avoid modifying the original
488+
sorted := make([][]string, len(paths))
489+
copy(sorted, paths)
490+
sort.Slice(sorted, func(i, j int) bool {
491+
// Sort by length first (shorter paths come first)
492+
if len(sorted[i]) != len(sorted[j]) {
493+
return len(sorted[i]) < len(sorted[j])
494+
}
495+
// For same length, sort lexicographically by joining elements
496+
return strings.Join(sorted[i], "/") < strings.Join(sorted[j], "/")
497+
})
498+
return sorted
499+
}

0 commit comments

Comments
 (0)