Permalink
Browse files

misc/cgo/errors: fix erroneous regexp detection

I had passed 1 instead of 2 to the SplitAfterN call in
errorstest.check, so all of the cases were erroneously falling through
to the non-regexp case (and passing even if the actual error didn't
match).

Now, we use bytes.HasSuffix to check for the non-regexp case, so we
will not incorrectly match a regexp comment to the non-regexp case.

updates #13467

Change-Id: Ia6be928a495425f2b7bae5001bd01346e115dcfa
Reviewed-on: https://go-review.googlesource.com/63692
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information...
bcmills committed Sep 13, 2017
1 parent 29415eb commit 814d92230a6befef18084fdf62431a9e0ecab7d4
Showing with 21 additions and 12 deletions.
  1. +21 −12 misc/cgo/errors/errors_test.go
@@ -31,19 +31,20 @@ func check(t *testing.T, file string) {
}
var errors []*regexp.Regexp
for i, line := range bytes.Split(contents, []byte("\n")) {
if !bytes.Contains(line, []byte("ERROR HERE")) {
if bytes.HasSuffix(line, []byte("ERROR HERE")) {
re := regexp.MustCompile(regexp.QuoteMeta(fmt.Sprintf("%s:%d:", file, i+1)))
errors = append(errors, re)
continue
}
var re *regexp.Regexp
frags := bytes.SplitAfterN(line, []byte("ERROR HERE: "), 1)
frags := bytes.SplitAfterN(line, []byte("ERROR HERE: "), 2)
if len(frags) == 1 {
re = regexp.MustCompile(regexp.QuoteMeta(fmt.Sprintf("%s:%d:", file, i+1)))
} else {
re, err = regexp.Compile(string(frags[1]))
if err != nil {
t.Errorf("Invalid regexp after `ERROR HERE: `: %q", frags[1])
continue
}
continue
}
re, err := regexp.Compile(string(frags[1]))
if err != nil {
t.Errorf("Invalid regexp after `ERROR HERE: `: %#q", frags[1])
continue
}
errors = append(errors, re)
}
@@ -55,7 +56,14 @@ func check(t *testing.T, file string) {
}
func expect(t *testing.T, file string, errors []*regexp.Regexp) {
cmd := exec.Command("go", "build", "-gcflags=-C", path(file))
dir, err := ioutil.TempDir("", filepath.Base(t.Name()))
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dst := filepath.Join(dir, strings.TrimSuffix(file, ".go"))
cmd := exec.Command("go", "build", "-o="+dst, "-gcflags=-C", path(file))
out, err := cmd.CombinedOutput()
if err == nil {
t.Errorf("expected cgo to fail but it succeeded")
@@ -66,12 +74,13 @@ func expect(t *testing.T, file string, errors []*regexp.Regexp) {
found := false
for _, line := range lines {
if re.Match(line) {
t.Logf("found match for %#q: %q", re, line)
found = true
break
}
}
if !found {
t.Errorf("expected error output to contain %q", re)
t.Errorf("expected error output to contain %#q", re)
}
}

0 comments on commit 814d922

Please sign in to comment.