MCPcopy Index your code
hub / github.com/cli/cli / TestNewCmdExtension_UpdateCheckIsNonblocking

Function TestNewCmdExtension_UpdateCheckIsNonblocking

pkg/cmd/root/extension_test.go:182–242  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

180}
181
182func TestNewCmdExtension_UpdateCheckIsNonblocking(t *testing.T) {
183 ios, _, _, _ := iostreams.Test()
184
185 em := &extensions.ExtensionManagerMock{
186 DispatchFunc: func(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) (bool, error) {
187 // Assume extension executed / dispatched without problems as test is focused on upgrade checking.
188 return true, nil
189 },
190 }
191
192 ext := &extensions.ExtensionMock{
193 CurrentVersionFunc: func() string {
194 return "1.0.0"
195 },
196 IsPinnedFunc: func() bool {
197 return false
198 },
199 LatestVersionFunc: func() string {
200 return "2.0.0"
201 },
202 NameFunc: func() string {
203 return "major-update"
204 },
205 OwnerFunc: func() string {
206 return ""
207 },
208 UpdateAvailableFunc: func() bool {
209 return true
210 },
211 URLFunc: func() string {
212 return "https//github.com/dne/major-update"
213 },
214 }
215
216 // When the extension command is executed, the checkFunc will run in the background longer than the extension dispatch.
217 // If the update check is non-blocking, then the extension command will complete immediately while checkFunc is still running.
218 checkFunc := func(em extensions.ExtensionManager, ext extensions.Extension) (*update.ReleaseInfo, error) {
219 time.Sleep(30 * time.Second)
220 return nil, fmt.Errorf("update check should not have completed")
221 }
222
223 cmd := root.NewCmdExtension(ios, em, ext, checkFunc)
224
225 // The test whether update check is non-blocking is based on how long it takes for the extension command execution.
226 // If there is no wait time as checkFunc is sleeping sufficiently long, we can trust update check is non-blocking.
227 // Otherwise, if any amount of wait is encountered, it is a decent indicator that update checking is blocking.
228 // This is not an ideal test and indicates the update design should be revisited to be easier to understand and manage.
229 completed := make(chan struct{})
230 go func() {
231 _, err := cmd.ExecuteC()
232 require.NoError(t, err)
233 close(completed)
234 }()
235
236 select {
237 case <-completed:
238 // Expected behavior assuming extension dispatch exits immediately while checkFunc is still running.
239 case <-time.After(1 * time.Second):

Callers

nothing calls this directly

Calls 3

TestFunction · 0.92
NewCmdExtensionFunction · 0.92
ErrorfMethod · 0.65

Tested by

no test coverage detected