internal/cmdtest: trim newline at beginning of regular expression

This commit is contained in:
Felix Lange 2018-01-02 11:56:04 +01:00
parent d2533d0efb
commit da45ea3b57

View file

@ -25,6 +25,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"regexp" "regexp"
"strings"
"sync" "sync"
"testing" "testing"
"text/template" "text/template"
@ -141,9 +142,10 @@ func (tt *TestCmd) matchExactOutput(want []byte) error {
// Note that an arbitrary amount of output may be consumed by the // Note that an arbitrary amount of output may be consumed by the
// regular expression. This usually means that expect cannot be used // regular expression. This usually means that expect cannot be used
// after ExpectRegexp. // after ExpectRegexp.
func (tt *TestCmd) ExpectRegexp(resource string) (*regexp.Regexp, []string) { func (tt *TestCmd) ExpectRegexp(regex string) (*regexp.Regexp, []string) {
regex = strings.TrimPrefix(regex, "\n")
var ( var (
re = regexp.MustCompile(resource) re = regexp.MustCompile(regex)
rtee = &runeTee{in: tt.stdout} rtee = &runeTee{in: tt.stdout}
matches []int matches []int
) )
@ -151,7 +153,7 @@ func (tt *TestCmd) ExpectRegexp(resource string) (*regexp.Regexp, []string) {
output := rtee.buf.Bytes() output := rtee.buf.Bytes()
if matches == nil { if matches == nil {
tt.Fatalf("Output did not match:\n---------------- (stdout text)\n%s\n---------------- (regular expression)\n%s", tt.Fatalf("Output did not match:\n---------------- (stdout text)\n%s\n---------------- (regular expression)\n%s",
output, resource) output, regex)
return re, nil return re, nil
} }
tt.Logf("Matched stdout text:\n%s", output) tt.Logf("Matched stdout text:\n%s", output)