@@ -2,6 +2,7 @@ package cmd
22
33import (
44 "bytes"
5+ "encoding/json"
56 "io"
67 "os"
78 "strings"
@@ -67,3 +68,70 @@ func TestRunRainOnRepos_ParallelStallDoesNotBlockHealthy(t *testing.T) {
6768 t .Fatalf ("parallel rain took %v; expected ~%ds timeout bound" , elapsed , timeoutSec )
6869 }
6970}
71+
72+ func TestRunRainReposParallel_PreservesSelectionIndex (t * testing.T ) {
73+ baseURL , cleanup := startStallTCPListener (t )
74+ defer cleanup ()
75+
76+ scenario := testutil .NewScenario (t )
77+ remote := scenario .CreateBareRepo ("upstream" )
78+ fast := scenario .CreateRepo ("repo-alpha" ).
79+ AddFile ("README.md" , "ok\n " ).
80+ Commit ("init" ).
81+ WithRemote ("origin" , remote )
82+ fast .Push ("origin" , fast .GetDefaultBranch ())
83+
84+ stall := testutil .CreateTestRepo (t , testutil.RepoOptions {
85+ Name : "repo-beta" ,
86+ Remotes : map [string ]string {
87+ "origin" : baseURL + "/stall.git" ,
88+ },
89+ })
90+
91+ repos := []git.Repository {
92+ {Name : "repo-alpha" , Path : fast .Path ()},
93+ {Name : "repo-beta" , Path : stall },
94+ }
95+
96+ oldJSON := rainJSON
97+ rainJSON = true
98+ defer func () { rainJSON = oldJSON }()
99+
100+ oldStdout := os .Stdout
101+ r , w , _ := os .Pipe ()
102+ os .Stdout = w
103+ defer func () { os .Stdout = oldStdout }()
104+
105+ totals , reports := runRainReposParallel (& registry.Registry {}, repos , git.RainOptions {FetchTimeoutSec : 2 }, false , 2 )
106+ _ = w .Close ()
107+ var out bytes.Buffer
108+ _ , _ = io .Copy (& out , r )
109+
110+ if totals .updated < 1 {
111+ t .Fatalf ("expected at least one updated repo, totals=%+v" , totals )
112+ }
113+ if len (reports ) != 2 {
114+ t .Fatalf ("expected 2 json reports, got %d" , len (reports ))
115+ }
116+
117+ byName := map [string ]jsonRepoResult {}
118+ for _ , rep := range reports {
119+ byName [rep .Name ] = rep
120+ }
121+ if byName ["repo-alpha" ].Index != 1 || byName ["repo-alpha" ].Total != 2 {
122+ t .Fatalf ("repo-alpha index/total = %d/%d, want 1/2" , byName ["repo-alpha" ].Index , byName ["repo-alpha" ].Total )
123+ }
124+ if byName ["repo-beta" ].Index != 2 || byName ["repo-beta" ].Total != 2 {
125+ t .Fatalf ("repo-beta index/total = %d/%d, want 2/2" , byName ["repo-beta" ].Index , byName ["repo-beta" ].Total )
126+ }
127+
128+ var doc jsonRunDocument
129+ if err := json .Unmarshal ([]byte (out .String ()), & doc ); err == nil && len (doc .Repositories ) > 0 {
130+ for _ , rep := range doc .Repositories {
131+ want := byName [rep .Name ].Index
132+ if rep .Index != want {
133+ t .Fatalf ("stdout json repo %s index %d, want %d" , rep .Name , rep .Index , want )
134+ }
135+ }
136+ }
137+ }
0 commit comments