mirror of
https://github.com/coder/coder.git
synced 2025-07-21 01:28:49 +00:00
fix: pty.Start respects context on Windows too (#7373)
* fix: pty.Start respects context on Windows too Signed-off-by: Spike Curtis <spike@coder.com> * Fix windows imports; rename ToExec -> AsExec Signed-off-by: Spike Curtis <spike@coder.com> * Fix import in windows test Signed-off-by: Spike Curtis <spike@coder.com> --------- Signed-off-by: Spike Curtis <spike@coder.com>
This commit is contained in:
@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@ -26,7 +25,7 @@ func Test_Start_copy(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
||||
defer cancel()
|
||||
|
||||
pc, cmd, err := pty.Start(exec.CommandContext(ctx, cmdEcho, argEcho...))
|
||||
pc, cmd, err := pty.Start(pty.CommandContext(ctx, cmdEcho, argEcho...))
|
||||
require.NoError(t, err)
|
||||
b := &bytes.Buffer{}
|
||||
readDone := make(chan error, 1)
|
||||
@ -64,7 +63,7 @@ func Test_Start_truncation(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
|
||||
defer cancel()
|
||||
|
||||
pc, cmd, err := pty.Start(exec.CommandContext(ctx, cmdCount, argCount...))
|
||||
pc, cmd, err := pty.Start(pty.CommandContext(ctx, cmdCount, argCount...))
|
||||
|
||||
require.NoError(t, err)
|
||||
readDone := make(chan struct{})
|
||||
@ -114,6 +113,35 @@ func Test_Start_truncation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Test_Start_cancel_context tests that we can cancel the command context and kill the process.
|
||||
func Test_Start_cancel_context(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
|
||||
defer cancel()
|
||||
cmdCtx, cmdCancel := context.WithCancel(ctx)
|
||||
|
||||
pc, cmd, err := pty.Start(pty.CommandContext(cmdCtx, cmdSleep, argSleep...))
|
||||
require.NoError(t, err)
|
||||
defer func() {
|
||||
_ = pc.Close()
|
||||
}()
|
||||
cmdCancel()
|
||||
|
||||
cmdDone := make(chan struct{})
|
||||
go func() {
|
||||
defer close(cmdDone)
|
||||
_ = cmd.Wait()
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-cmdDone:
|
||||
// OK!
|
||||
case <-ctx.Done():
|
||||
t.Error("cmd.Wait() timed out")
|
||||
}
|
||||
}
|
||||
|
||||
// readUntil reads one byte at a time until we either see the string we want, or the context expires
|
||||
func readUntil(ctx context.Context, t *testing.T, want string, r io.Reader) error {
|
||||
// output can contain virtual terminal sequences, so we need to parse these
|
||||
|
Reference in New Issue
Block a user