@@ -29,8 +29,8 @@ type UpdatePkgMock struct {
2929 mock.Mock
3030}
3131
32- func (m * UpdatePkgMock ) CheckForUpdates (clients * shared.ClientFactory , cmd * cobra.Command , autoApprove bool ) error {
33- args := m .Called (clients , cmd , autoApprove )
32+ func (m * UpdatePkgMock ) CheckForUpdates (clients * shared.ClientFactory , cmd * cobra.Command , cli bool , sdk bool ) error {
33+ args := m .Called (clients , cmd , cli , sdk )
3434 return args .Error (0 )
3535}
3636
@@ -49,32 +49,62 @@ func TestUpgradeCommand(t *testing.T) {
4949 updatePkgMock := new (UpdatePkgMock )
5050 checkForUpdatesFunc = updatePkgMock .CheckForUpdates
5151
52- // Test default behavior (no auto-approve )
53- updatePkgMock .On ("CheckForUpdates" , mock .Anything , mock .Anything , false ).Return (nil )
52+ // Test default behavior (no flags )
53+ updatePkgMock .On ("CheckForUpdates" , mock .Anything , mock .Anything , false , false ).Return (nil )
5454 err := cmd .ExecuteContext (ctx )
5555 if err != nil {
5656 assert .Fail (t , "cmd.Upgrade had unexpected error" )
5757 }
58- updatePkgMock .AssertCalled (t , "CheckForUpdates" , mock .Anything , mock .Anything , false )
58+ updatePkgMock .AssertCalled (t , "CheckForUpdates" , mock .Anything , mock .Anything , false , false )
5959
60- // Test with auto-approve flag
60+ // Test with CLI flag
6161 cmd = NewCommand (clients )
6262 testutil .MockCmdIO (clients .IO , cmd )
63- cmd .SetArgs ([]string {"--auto-approve " })
63+ cmd .SetArgs ([]string {"--cli " })
6464
6565 updatePkgMock = new (UpdatePkgMock )
6666 checkForUpdatesFunc = updatePkgMock .CheckForUpdates
67- updatePkgMock .On ("CheckForUpdates" , mock .Anything , mock .Anything , true ).Return (nil )
67+ updatePkgMock .On ("CheckForUpdates" , mock .Anything , mock .Anything , true , false ).Return (nil )
6868
6969 err = cmd .ExecuteContext (ctx )
7070 if err != nil {
71- assert .Fail (t , "cmd.Upgrade with auto-approve had unexpected error" )
71+ assert .Fail (t , "cmd.Upgrade with cli flag had unexpected error" )
7272 }
73- updatePkgMock .AssertCalled (t , "CheckForUpdates" , mock .Anything , mock .Anything , true )
73+ updatePkgMock .AssertCalled (t , "CheckForUpdates" , mock .Anything , mock .Anything , true , false )
74+
75+ // Test with SDK flag
76+ cmd = NewCommand (clients )
77+ testutil .MockCmdIO (clients .IO , cmd )
78+ cmd .SetArgs ([]string {"--sdk" })
79+
80+ updatePkgMock = new (UpdatePkgMock )
81+ checkForUpdatesFunc = updatePkgMock .CheckForUpdates
82+ updatePkgMock .On ("CheckForUpdates" , mock .Anything , mock .Anything , false , true ).Return (nil )
83+
84+ err = cmd .ExecuteContext (ctx )
85+ if err != nil {
86+ assert .Fail (t , "cmd.Upgrade with sdk flag had unexpected error" )
87+ }
88+ updatePkgMock .AssertCalled (t , "CheckForUpdates" , mock .Anything , mock .Anything , false , true )
89+
90+ // Test with both CLI and SDK flags
91+ cmd = NewCommand (clients )
92+ testutil .MockCmdIO (clients .IO , cmd )
93+ cmd .SetArgs ([]string {"--cli" , "--sdk" })
94+
95+ updatePkgMock = new (UpdatePkgMock )
96+ checkForUpdatesFunc = updatePkgMock .CheckForUpdates
97+ updatePkgMock .On ("CheckForUpdates" , mock .Anything , mock .Anything , true , true ).Return (nil )
98+
99+ err = cmd .ExecuteContext (ctx )
100+ if err != nil {
101+ assert .Fail (t , "cmd.Upgrade with both cli and sdk flags had unexpected error" )
102+ }
103+ updatePkgMock .AssertCalled (t , "CheckForUpdates" , mock .Anything , mock .Anything , true , true )
74104}
75105
76- func TestUpgradeCommandWithAutoApproveError (t * testing.T ) {
77- // Create a mock of UpdateNotification that returns an error on InstallUpdatesWithoutPrompt
106+ func TestUpgradeCommandWithFlagError (t * testing.T ) {
107+ // Create a mock of UpdateNotification that returns an error on InstallUpdatesWithComponentFlags
78108 originalCheckForUpdates := checkForUpdatesFunc
79109 defer func () {
80110 checkForUpdatesFunc = originalCheckForUpdates
@@ -87,23 +117,47 @@ func TestUpgradeCommandWithAutoApproveError(t *testing.T) {
87117 // Create clients that is mocked for testing
88118 clients := shared .NewClientFactory (clientsMock .MockClientFactory ())
89119
90- // Mock the checkForUpdates function to simulate an error during auto-approve updates
91- checkForUpdatesFunc = func (clients * shared.ClientFactory , cmd * cobra.Command , autoApprove bool ) error {
92- if autoApprove {
93- return assert .AnError // Simulate error when auto-approve is true
120+ // Mock the checkForUpdates function to simulate an error during flag-based updates
121+ checkForUpdatesFunc = func (clients * shared.ClientFactory , cmd * cobra.Command , cli bool , sdk bool ) error {
122+ if cli || sdk {
123+ return assert .AnError // Simulate error when either flag is true
94124 }
95125 return nil
96126 }
97127
98- // Create the command with auto-approve flag
128+ // Test with CLI flag causing error
99129 cmd := NewCommand (clients )
100130 testutil .MockCmdIO (clients .IO , cmd )
101- cmd .SetArgs ([]string {"--auto-approve " })
131+ cmd .SetArgs ([]string {"--cli " })
102132
103133 // Execute the command and verify it returns the error
104134 err := cmd .ExecuteContext (ctx )
105135
106136 // Verify the error was properly propagated
107137 assert .Error (t , err )
108138 assert .Equal (t , assert .AnError , err )
139+
140+ // Test with SDK flag causing error
141+ cmd = NewCommand (clients )
142+ testutil .MockCmdIO (clients .IO , cmd )
143+ cmd .SetArgs ([]string {"--sdk" })
144+
145+ // Execute the command and verify it returns the error
146+ err = cmd .ExecuteContext (ctx )
147+
148+ // Verify the error was properly propagated
149+ assert .Error (t , err )
150+ assert .Equal (t , assert .AnError , err )
151+
152+ // Test with both flags causing error
153+ cmd = NewCommand (clients )
154+ testutil .MockCmdIO (clients .IO , cmd )
155+ cmd .SetArgs ([]string {"--cli" , "--sdk" })
156+
157+ // Execute the command and verify it returns the error
158+ err = cmd .ExecuteContext (ctx )
159+
160+ // Verify the error was properly propagated
161+ assert .Error (t , err )
162+ assert .Equal (t , assert .AnError , err )
109163}
0 commit comments