Commit 814d9223 authored by Bryan C. Mills's avatar Bryan C. Mills Committed by Bryan Mills

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/63692Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 29415eb2
......@@ -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)
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment