Test_Start_copy tests that we can use io.Copy() on command output without deadlocking.
(t *testing.T)
| 18 | // Test_Start_copy tests that we can use io.Copy() on command output |
| 19 | // without deadlocking. |
| 20 | func Test_Start_copy(t *testing.T) { |
| 21 | t.Parallel() |
| 22 | |
| 23 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 24 | defer cancel() |
| 25 | |
| 26 | pc, cmd, err := pty.Start(pty.CommandContext(ctx, cmdEcho, argEcho...)) |
| 27 | require.NoError(t, err) |
| 28 | b := &bytes.Buffer{} |
| 29 | readDone := make(chan error, 1) |
| 30 | go func() { |
| 31 | _, err := io.Copy(b, pc.OutputReader()) |
| 32 | readDone <- err |
| 33 | }() |
| 34 | |
| 35 | select { |
| 36 | case err := <-readDone: |
| 37 | require.NoError(t, err) |
| 38 | case <-ctx.Done(): |
| 39 | t.Error("read timed out") |
| 40 | } |
| 41 | assert.Contains(t, b.String(), "test") |
| 42 | |
| 43 | cmdDone := make(chan error, 1) |
| 44 | go func() { |
| 45 | cmdDone <- cmd.Wait() |
| 46 | }() |
| 47 | |
| 48 | select { |
| 49 | case err := <-cmdDone: |
| 50 | require.NoError(t, err) |
| 51 | case <-ctx.Done(): |
| 52 | t.Error("cmd.Wait() timed out") |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Test_Start_truncation tests that we can read command output without truncation |
| 57 | // even after the command has exited. |
nothing calls this directly
no test coverage detected