(t *testing.T)
| 20 | ) |
| 21 | |
| 22 | func TestPrompt(t *testing.T) { |
| 23 | t.Parallel() |
| 24 | t.Run("Success", func(t *testing.T) { |
| 25 | t.Parallel() |
| 26 | ctx := testutil.Context(t, testutil.WaitShort) |
| 27 | ptty := ptytest.New(t) |
| 28 | msgChan := make(chan string) |
| 29 | go func() { |
| 30 | resp, err := newPrompt(ctx, ptty, cliui.PromptOptions{ |
| 31 | Text: "Example", |
| 32 | }, nil) |
| 33 | assert.NoError(t, err) |
| 34 | msgChan <- resp |
| 35 | }() |
| 36 | ptty.ExpectMatch("Example") |
| 37 | ptty.WriteLine("hello") |
| 38 | resp := testutil.TryReceive(ctx, t, msgChan) |
| 39 | require.Equal(t, "hello", resp) |
| 40 | }) |
| 41 | |
| 42 | t.Run("Confirm", func(t *testing.T) { |
| 43 | t.Parallel() |
| 44 | ctx := testutil.Context(t, testutil.WaitShort) |
| 45 | ptty := ptytest.New(t) |
| 46 | doneChan := make(chan string) |
| 47 | go func() { |
| 48 | resp, err := newPrompt(ctx, ptty, cliui.PromptOptions{ |
| 49 | Text: "Example", |
| 50 | IsConfirm: true, |
| 51 | }, nil) |
| 52 | assert.NoError(t, err) |
| 53 | doneChan <- resp |
| 54 | }() |
| 55 | ptty.ExpectMatch("Example") |
| 56 | ptty.WriteLine("yes") |
| 57 | resp := testutil.TryReceive(ctx, t, doneChan) |
| 58 | require.Equal(t, "yes", resp) |
| 59 | }) |
| 60 | |
| 61 | t.Run("Skip", func(t *testing.T) { |
| 62 | t.Parallel() |
| 63 | ctx := testutil.Context(t, testutil.WaitShort) |
| 64 | ptty := ptytest.New(t) |
| 65 | var buf bytes.Buffer |
| 66 | |
| 67 | // Copy all data written out to a buffer. When we close the ptty, we can |
| 68 | // no longer read from the ptty.Output(), but we can read what was |
| 69 | // written to the buffer. |
| 70 | dataRead, doneReading := context.WithCancel(ctx) |
| 71 | go func() { |
| 72 | // This will throw an error sometimes. The underlying ptty |
| 73 | // has its own cleanup routines in t.Cleanup. Instead of |
| 74 | // trying to control the close perfectly, just let the ptty |
| 75 | // double close. This error isn't important, we just |
| 76 | // want to know the ptty is done sending output. |
| 77 | _, _ = io.Copy(&buf, ptty.Output()) |
| 78 | doneReading() |
| 79 | }() |
nothing calls this directly
no test coverage detected