Skip to content

Commit

Permalink
Add support for writing to ptys
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <[email protected]>
  • Loading branch information
apostasie committed Dec 4, 2024
1 parent 1f8fe6a commit a16ccf1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
18 changes: 15 additions & 3 deletions pkg/testutil/test/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type GenericCommand struct {
stdin io.Reader
async bool
pty bool
ptyWriters []func(*os.File) error
timeout time.Duration
workingDir string

Expand All @@ -69,8 +70,9 @@ func (gc *GenericCommand) WithWrapper(binary string, args ...string) {
gc.helperArgs = args
}

func (gc *GenericCommand) WithPseudoTTY() {
func (gc *GenericCommand) WithPseudoTTY(writers ...func(*os.File) error) {
gc.pty = true
gc.ptyWriters = writers
}

func (gc *GenericCommand) WithStdin(r io.Reader) {
Expand Down Expand Up @@ -105,8 +107,18 @@ func (gc *GenericCommand) Run(expect *Expected) {
iCmdCmd.Stdin = tty
iCmdCmd.Stdout = tty
iCmdCmd.Stderr = tty
defer pty.Close()
defer tty.Close()

for _, writer := range gc.ptyWriters {
go func() {
err := writer(pty)
assert.NilError(gc.t, err)
}()
}

defer func() {
_ = tty.Close()
_ = pty.Close()
}()
}

// Run it
Expand Down
3 changes: 2 additions & 1 deletion pkg/testutil/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package test

import (
"io"
"os"
"testing"
"time"
)
Expand Down Expand Up @@ -98,7 +99,7 @@ type TestableCommand interface {
// WithWrapper allows wrapping a command with another command (for example: `time`, `unbuffer`)
WithWrapper(binary string, args ...string)
// WithPseudoTTY
WithPseudoTTY()
WithPseudoTTY(writers ...func(*os.File) error)
// WithStdin allows passing a reader to be used for stdin for the command
WithStdin(r io.Reader)
// WithCwd allows specifying the working directory for the command
Expand Down

0 comments on commit a16ccf1

Please sign in to comment.