Thanks to visit codestin.com
Credit goes to github.com

Skip to content

test: Improve TestSSH/ForwardGPG stability on macOS via pty.ReadRune #5739

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

Merged
merged 3 commits into from
Jan 17, 2023
Merged
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
4 changes: 4 additions & 0 deletions cli/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ Expire-Date: 0
// real error from being printed.
t.Cleanup(cancel)

// Wait for the prompt or any output really to indicate the command has
// started and accepting input on stdin.
_ = pty.ReadRune(ctx)
Copy link
Member

Choose a reason for hiding this comment

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

If the goal is to wait for output why not make a method WaitForOutput instead or something, which has similar behavior but doesn't advance the reader (e.g. using Peek instead of Read)

Copy link
Member Author

Choose a reason for hiding this comment

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

That's not a bad suggestion, I think both methods can be useful though. I'll add Peek and use that instead. 👍🏻


pty.WriteLine("echo hello 'world'")
pty.ExpectMatch("hello world")

Expand Down
41 changes: 41 additions & 0 deletions pty/ptytest/ptytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,47 @@ func (p *PTY) ExpectMatch(str string) string {
}
}

func (p *PTY) ReadRune(ctx context.Context) rune {
p.t.Helper()

// A timeout is mandatory, caller can decide by passing a context
// that times out.
if _, ok := ctx.Deadline(); !ok {
timeout := testutil.WaitMedium
p.logf("ReadRune ctx has no deadline, using %s", timeout)
var cancel context.CancelFunc
//nolint:gocritic // Rule guard doesn't detect that we're using testutil.Wait*.
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}

var r rune
match := make(chan error, 1)
go func() {
defer close(match)
var err error
r, _, err = p.runeReader.ReadRune()
match <- err
}()

select {
case err := <-match:
if err != nil {
p.fatalf("read error", "%v (wanted newline; got %q)", err, r)
return 0
}
p.logf("matched rune = %q", r)
return r
case <-ctx.Done():
// Ensure goroutine is cleaned up before test exit.
_ = p.close("read rune context done: " + ctx.Err().Error())
<-match

p.fatalf("read rune context done", "wanted rune; got nothing")
return 0
}
}

func (p *PTY) ReadLine() string {
p.t.Helper()

Expand Down