Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add support for writing to ptys #3591

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't move defer tty.Close()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not mandatory but you can just defer closing the tty before the goroutines loop


for _, writer := range gc.ptyWriters {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we use errgroup to wait for the routines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed.
Will update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverting to draft for the time being - being abroad I may not have time to look into this immediately.

Thanks @AkihiroSuda .

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

defer func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defer func() {
defer func() {
// Best effort.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may help when debugging issues ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @fahedouch

this is not ready though - I left it half assed a little while back, but will get it proper soon!

_ = 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
Loading