(ctx context.Context, t *testing.T, cmd *cobra.Command, cli *FakeCli)
| 13 | ) |
| 14 | |
| 15 | func TerminatePrompt(ctx context.Context, t *testing.T, cmd *cobra.Command, cli *FakeCli) { |
| 16 | t.Helper() |
| 17 | |
| 18 | errChan := make(chan error) |
| 19 | defer close(errChan) |
| 20 | |
| 21 | // wrap the out stream to detect when the prompt is ready |
| 22 | writerHookChan := make(chan struct{}) |
| 23 | defer close(writerHookChan) |
| 24 | |
| 25 | outStream := streams.NewOut(NewWriterWithHook(cli.OutBuffer(), func(p []byte) { |
| 26 | writerHookChan <- struct{}{} |
| 27 | })) |
| 28 | cli.SetOut(outStream) |
| 29 | |
| 30 | r, _, err := os.Pipe() |
| 31 | assert.NilError(t, err) |
| 32 | cli.SetIn(streams.NewIn(r)) |
| 33 | |
| 34 | notifyCtx, notifyCancel := context.WithCancel(ctx) |
| 35 | t.Cleanup(notifyCancel) |
| 36 | |
| 37 | go func() { |
| 38 | errChan <- cmd.ExecuteContext(notifyCtx) |
| 39 | }() |
| 40 | |
| 41 | writeCtx, writeCancel := context.WithTimeout(ctx, 100*time.Millisecond) |
| 42 | defer writeCancel() |
| 43 | |
| 44 | // wait for the prompt to be ready |
| 45 | select { |
| 46 | case <-writeCtx.Done(): |
| 47 | t.Fatalf("command %s did not write prompt to stdout", cmd.Name()) |
| 48 | case <-writerHookChan: |
| 49 | // drain the channel for future buffer writes |
| 50 | go func() { |
| 51 | for { |
| 52 | select { |
| 53 | case <-ctx.Done(): |
| 54 | return |
| 55 | case <-writerHookChan: |
| 56 | } |
| 57 | } |
| 58 | }() |
| 59 | } |
| 60 | |
| 61 | assert.Check(t, cli.OutBuffer().Len() > 0) |
| 62 | |
| 63 | // a small delay to ensure the plugin is prompting |
| 64 | time.Sleep(100 * time.Microsecond) |
| 65 | |
| 66 | errCtx, errCancel := context.WithTimeout(ctx, 100*time.Millisecond) |
| 67 | defer errCancel() |
| 68 | |
| 69 | // sigint and sigterm are caught by the prompt |
| 70 | // this allows us to gracefully exit the prompt with a 0 exit code |
| 71 | notifyCancel() |
| 72 |
nothing calls this directly
no test coverage detected
searching dependent graphs…