@@ -23,6 +23,7 @@ import (
2323 "github.com/slackapi/slack-cli/internal/shared"
2424 "github.com/slackapi/slack-cli/internal/slackdeps"
2525 "github.com/spf13/cobra"
26+ "github.com/stretchr/testify/assert"
2627 "github.com/stretchr/testify/mock"
2728 "github.com/stretchr/testify/require"
2829)
@@ -109,3 +110,107 @@ func Test_Update_HasUpdate(t *testing.T) {
109110 })
110111 }
111112}
113+
114+ func Test_Update_InstallUpdatesWithoutPrompt (t * testing.T ) {
115+ for name , tt := range map [string ]struct {
116+ dependencyHasUpdate []bool
117+ installUpdateErrors []error
118+ expectedErrorContains string
119+ }{
120+ "No dependencies have updates" : {
121+ dependencyHasUpdate : []bool {false , false },
122+ installUpdateErrors : []error {nil , nil },
123+ expectedErrorContains : "" ,
124+ },
125+ "Dependencies have updates, all install successfully" : {
126+ dependencyHasUpdate : []bool {true , true },
127+ installUpdateErrors : []error {nil , nil },
128+ expectedErrorContains : "" ,
129+ },
130+ "Dependencies have updates, first fails to install" : {
131+ dependencyHasUpdate : []bool {true , false },
132+ installUpdateErrors : []error {assert .AnError , nil },
133+ expectedErrorContains : "general error for testing" ,
134+ },
135+ "Dependencies have updates, second fails to install" : {
136+ dependencyHasUpdate : []bool {true , true },
137+ installUpdateErrors : []error {nil , assert .AnError },
138+ expectedErrorContains : "general error for testing" ,
139+ },
140+ } {
141+ t .Run (name , func (t * testing.T ) {
142+ // Create clients
143+ clients := shared.ClientFactory {
144+ Config : config .NewConfig (slackdeps .NewFsMock (), slackdeps .NewOsMock ()),
145+ SDKConfig : hooks .NewSDKConfigMock (),
146+ }
147+
148+ // Create the mocks
149+ var dependencies []Dependency
150+
151+ // Special handling for "first fails to install" case
152+ if name == "Dependencies have updates, first fails to install" {
153+ // Only create and add the first dependency since execution will stop after it fails
154+ mockDep1 := new (mockDependency )
155+ mockDep1 .On ("HasUpdate" ).Return (true , nil )
156+ mockDep1 .On ("PrintUpdateNotification" , mock .Anything ).Return (false , nil )
157+ mockDep1 .On ("InstallUpdate" , mock .Anything ).Return (assert .AnError )
158+ dependencies = append (dependencies , mockDep1 )
159+
160+ // Second dependency is never called due to early return
161+ mockDep2 := new (mockDependency )
162+ dependencies = append (dependencies , mockDep2 )
163+ } else {
164+ // Handle all other test cases normally
165+ for i , hasUpdate := range tt .dependencyHasUpdate {
166+ mockDep := new (mockDependency )
167+ mockDep .On ("HasUpdate" ).Return (hasUpdate , nil )
168+
169+ // Only set expectations for PrintUpdateNotification and InstallUpdate
170+ // if the dependency hasUpdate = true
171+ if hasUpdate {
172+ mockDep .On ("PrintUpdateNotification" , mock .Anything ).Return (false , nil )
173+ mockDep .On ("InstallUpdate" , mock .Anything ).Return (tt .installUpdateErrors [i ])
174+ }
175+
176+ dependencies = append (dependencies , mockDep )
177+ }
178+ }
179+
180+ // Create updateNotification
181+ updateNotification = & UpdateNotification {
182+ clients : & clients ,
183+ enabled : true ,
184+ envDisabled : "SLACK_SKIP_UPDATE" ,
185+ hoursToWait : defaultHoursToWait ,
186+ dependencies : dependencies ,
187+ }
188+
189+ // Create test cmd
190+ cmd := & cobra.Command {}
191+
192+ // Test
193+ err := updateNotification .InstallUpdatesWithoutPrompt (cmd )
194+
195+ if tt .expectedErrorContains != "" {
196+ require .Error (t , err )
197+ require .Contains (t , err .Error (), tt .expectedErrorContains )
198+ } else {
199+ require .NoError (t , err )
200+ }
201+
202+ // Only verify expectations on the first dependency for the "first fails" case
203+ // since the second dependency should never be called
204+ if name == "Dependencies have updates, first fails to install" {
205+ mockDep1 := dependencies [0 ].(* mockDependency )
206+ mockDep1 .AssertExpectations (t )
207+ } else {
208+ // Verify all expectations were met for other cases
209+ for _ , dep := range dependencies {
210+ mockDep := dep .(* mockDependency )
211+ mockDep .AssertExpectations (t )
212+ }
213+ }
214+ })
215+ }
216+ }
0 commit comments