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

Expect regex find show output on regex match fail #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 2 deletions gexpect.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (expect *ExpectSubprocess) expectRegexFind(regex string, output bool) ([]st
// convert indexes to strings

if len(result) == 0 {
err = fmt.Errorf("ExpectRegex didn't find regex '%v'.", regex)
err = fmt.Errorf("ExpectRegex didn't find regex '%v'.\nOutput:\n%s", regex, stringIndexedInto)
} else {
// The number in pairs[1] is an index of a first
// character outside the whole match
Expand Down Expand Up @@ -387,7 +387,7 @@ func (expect *ExpectSubprocess) ReadUntil(delim byte) ([]byte, error) {
for i := 0; i < n; i++ {
if chunk[i] == delim {
if len(chunk) > i+1 {
expect.buf.PutBack(chunk[i+1:n])
expect.buf.PutBack(chunk[i+1 : n])
}
return join, nil
} else {
Expand Down
32 changes: 32 additions & 0 deletions gexpect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,38 @@ func TestRegexFind(t *testing.T) {
}
}

// TestRegexFindFailure checks to ensure that when ExpectRegexFind()
// fails, the returned error contains both the regex and the collected
// output.
func TestRegexFindFailure(t *testing.T) {
input := "hello world"
re := "this does not match"

child, err := Spawn("echo \"" + input + "\"")
if err != nil {
t.Fatal(err)
}

matches, err := child.ExpectRegexFind(re)
if err == nil {
t.Errorf("Expected error object")
}

if len(matches) > 0 {
t.Errorf("Expected no matches, got %v", matches)
}

s := err.Error()

if strings.Contains(s, re) == false {
t.Errorf("Expected error object to contain regex %q, got %v", re, s)
}

if strings.Contains(s, input) == false {
t.Errorf("Expected error object to contain input data %q, got %v", input, s)
}
}

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

Expand Down