Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Merge changes from upstream #5

Merged
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
20 changes: 8 additions & 12 deletions gexpect.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (expect *ExpectSubprocess) Expect(searchString string) (e error) {
if i == target {
unreadIndex := m + i - offset
if len(chunk) > unreadIndex {
expect.buf.PutBack(chunk[unreadIndex:])
expect.buf.PutBack(chunk[unreadIndex:n])
}
return nil
}
Expand Down Expand Up @@ -378,27 +378,26 @@ func (expect *ExpectSubprocess) Interact() {
}

func (expect *ExpectSubprocess) ReadUntil(delim byte) ([]byte, error) {
join := make([]byte, 1, 512)
join := make([]byte, 0, 512)
chunk := make([]byte, 255)

for {

n, err := expect.buf.Read(chunk)

if err != nil {
return join, err
}

for i := 0; i < n; i++ {
if chunk[i] == delim {
if len(chunk) > i+1 {
expect.buf.PutBack(chunk[i+1:])
expect.buf.PutBack(chunk[i+1:n])
}
Copy link

Choose a reason for hiding this comment

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

Shouldn't you do the same kind of changes in Expect(), line 320?

Copy link
Author

Choose a reason for hiding this comment

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

Dunno, it is not a change I have made, just some fix from upstream. But probably yes, would need a test for it to know.

return join, nil
} else {
join = append(join, chunk[i])
}
}

if err != nil {
return join, err
}
}
}

Expand All @@ -408,10 +407,7 @@ func (expect *ExpectSubprocess) Wait() error {

func (expect *ExpectSubprocess) ReadLine() (string, error) {
str, err := expect.ReadUntil('\n')
if err != nil {
return "", err
}
return string(str), nil
return string(str), err
}

func _start(expect *ExpectSubprocess) (*ExpectSubprocess, error) {
Expand Down
29 changes: 27 additions & 2 deletions gexpect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,31 @@ func TestRegexFind(t *testing.T) {
}
}

func TestReadLine(t *testing.T) {
t.Logf("Testing ReadLine...")

child, err := Spawn("echo \"foo\nbar\"")

if err != nil {
t.Fatal(err)
}
s, err := child.ReadLine()

if err != nil {
t.Fatal(err)
}
if s != "foo\r" {
Copy link

Choose a reason for hiding this comment

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

Why does it expect "\r" if the command prints "\n"?

Copy link
Author

Choose a reason for hiding this comment

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

My earlier experiments shown that lines read from pty always end in \r\n.

Copy link
Author

Choose a reason for hiding this comment

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

The comment above was a bit too short - they end in \r\n and ReadLine reads pty until it finds \n, so it returns a line ending with \r.

t.Fatalf("expected 'foo\\r', got '%s'", s)
}
s, err = child.ReadLine()
if err != nil {
t.Fatal(err)
}
if s != "bar\r" {
t.Fatalf("expected 'bar\\r', got '%s'", s)
}
}

func TestRegexWithOutput(t *testing.T) {
t.Logf("Testing Regular Expression search with output...")

Expand Down Expand Up @@ -257,7 +282,7 @@ func TestRegexTimeoutWithOutput(t *testing.T) {

func TestRegexFindNoExcessBytes(t *testing.T) {
t.Logf("Testing Regular Expressions returning output with no excess strings")
repeats := 100
repeats := 50
tests := []struct {
desc string
loopBody string
Expand All @@ -284,7 +309,7 @@ func TestRegexFindNoExcessBytes(t *testing.T) {
},
{
desc: `matching chunks in single line chunk by chunk`,
loopBody: `echo -n "a ${i} b"`,
loopBody: `printf "a ${i} b"`,
searchPattern: `a\s+(\d+)\s+b`,
expectFullTmpl: `a %d b`,
unmatchedData: "",
Expand Down