@@ -178,6 +178,133 @@ func TestInstallWithProgress_DryRunReturnsNoInstalledPackages(t *testing.T) {
178178 assert .Empty (t , casks , "dry-run should not report casks as installed" )
179179}
180180
181+ func TestInstallWithProgress_ClassifiesFromPostInstallLookup (t * testing.T ) {
182+ oldGetInstalledPackages := getInstalledPackagesFn
183+ oldPreInstallChecks := preInstallChecksFn
184+ oldRunBrewInstallBatch := runBrewInstallBatchFn
185+ oldInstallFormulaWithError := installFormulaWithErrorFn
186+ oldInstallSmartCaskWithError := installSmartCaskWithErrorFn
187+
188+ t .Cleanup (func () {
189+ getInstalledPackagesFn = oldGetInstalledPackages
190+ preInstallChecksFn = oldPreInstallChecks
191+ runBrewInstallBatchFn = oldRunBrewInstallBatch
192+ installFormulaWithErrorFn = oldInstallFormulaWithError
193+ installSmartCaskWithErrorFn = oldInstallSmartCaskWithError
194+ })
195+
196+ calls := 0
197+ var retryCalls []string
198+ getInstalledPackagesFn = func () (map [string ]bool , map [string ]bool , error ) {
199+ calls ++
200+ switch calls {
201+ case 1 :
202+ return map [string ]bool {}, map [string ]bool {}, nil
203+ case 2 :
204+ return map [string ]bool {"git" : true }, map [string ]bool {}, nil
205+ default :
206+ return nil , nil , assert .AnError
207+ }
208+ }
209+ preInstallChecksFn = func (int ) error { return nil }
210+ runBrewInstallBatchFn = func (args ... string ) (string , error ) {
211+ assert .Equal (t , []string {"install" , "git" , "curl" }, args )
212+ return "" , nil
213+ }
214+ installFormulaWithErrorFn = func (pkg string ) string {
215+ retryCalls = append (retryCalls , pkg )
216+ return ""
217+ }
218+ installSmartCaskWithErrorFn = func (pkg string ) string {
219+ t .Fatalf ("unexpected cask retry for %s" , pkg )
220+ return ""
221+ }
222+
223+ oldStdout := os .Stdout
224+ r , w , err := os .Pipe ()
225+ require .NoError (t , err )
226+ os .Stdout = w
227+
228+ formulae , casks , runErr := InstallWithProgress ([]string {"git" , "curl" }, nil , false )
229+
230+ w .Close ()
231+ os .Stdout = oldStdout
232+
233+ outputBytes , copyErr := io .ReadAll (r )
234+ require .NoError (t , copyErr )
235+ _ = outputBytes
236+
237+ assert .NoError (t , runErr )
238+ assert .ElementsMatch (t , []string {"git" , "curl" }, formulae )
239+ assert .Empty (t , casks )
240+ assert .Equal (t , []string {"curl" }, retryCalls )
241+ assert .Equal (t , 2 , calls )
242+ }
243+
244+ func TestInstallWithProgress_FallsBackWhenPostInstallLookupFails (t * testing.T ) {
245+ oldGetInstalledPackages := getInstalledPackagesFn
246+ oldPreInstallChecks := preInstallChecksFn
247+ oldRunBrewInstallBatch := runBrewInstallBatchFn
248+ oldInstallFormulaWithError := installFormulaWithErrorFn
249+ oldInstallSmartCaskWithError := installSmartCaskWithErrorFn
250+
251+ t .Cleanup (func () {
252+ getInstalledPackagesFn = oldGetInstalledPackages
253+ preInstallChecksFn = oldPreInstallChecks
254+ runBrewInstallBatchFn = oldRunBrewInstallBatch
255+ installFormulaWithErrorFn = oldInstallFormulaWithError
256+ installSmartCaskWithErrorFn = oldInstallSmartCaskWithError
257+ })
258+
259+ calls := 0
260+ installFormulaRetries := 0
261+ getInstalledPackagesFn = func () (map [string ]bool , map [string ]bool , error ) {
262+ calls ++
263+ switch calls {
264+ case 1 :
265+ return map [string ]bool {}, map [string ]bool {}, nil
266+ case 2 :
267+ return nil , nil , assert .AnError
268+ default :
269+ return nil , nil , assert .AnError
270+ }
271+ }
272+ preInstallChecksFn = func (int ) error { return nil }
273+ runBrewInstallBatchFn = func (args ... string ) (string , error ) {
274+ assert .Equal (t , []string {"install" , "git" , "curl" }, args )
275+ return "" , nil
276+ }
277+ installFormulaWithErrorFn = func (pkg string ) string {
278+ installFormulaRetries ++
279+ t .Fatalf ("unexpected retry for %s" , pkg )
280+ return ""
281+ }
282+ installSmartCaskWithErrorFn = func (pkg string ) string {
283+ t .Fatalf ("unexpected cask retry for %s" , pkg )
284+ return ""
285+ }
286+
287+ oldStdout := os .Stdout
288+ r , w , err := os .Pipe ()
289+ require .NoError (t , err )
290+ os .Stdout = w
291+
292+ formulae , casks , runErr := InstallWithProgress ([]string {"git" , "curl" }, nil , false )
293+
294+ w .Close ()
295+ os .Stdout = oldStdout
296+
297+ outputBytes , copyErr := io .ReadAll (r )
298+ require .NoError (t , copyErr )
299+ _ = outputBytes
300+
301+ assert .NoError (t , runErr )
302+ assert .ElementsMatch (t , []string {"git" , "curl" }, formulae )
303+ assert .Empty (t , casks )
304+ assert .Equal (t , 0 , installFormulaRetries )
305+ assert .Equal (t , 2 , calls )
306+ }
307+
181308func TestUpdate_DryRun (t * testing.T ) {
182309 err := Update (true )
183310 assert .NoError (t , err )
0 commit comments