diff --git a/gexpect.go b/gexpect.go index 6e918cb..3a2cfd0 100644 --- a/gexpect.go +++ b/gexpect.go @@ -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 @@ -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 { diff --git a/gexpect_test.go b/gexpect_test.go index 8d3771a..5a3ec9b 100644 --- a/gexpect_test.go +++ b/gexpect_test.go @@ -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...")