]> Cypherpunks repositories - gostls13.git/commitdiff
misc/cgo/errors: fix erroneous regexp detection
authorBryan C. Mills <bcmills@google.com>
Wed, 13 Sep 2017 19:14:05 +0000 (15:14 -0400)
committerBryan Mills <bcmills@google.com>
Thu, 14 Sep 2017 02:24:04 +0000 (02:24 +0000)
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>
misc/cgo/errors/errors_test.go

index 29249937141597382f1ce3c859cd91857c63f59b..e2b91063a6fef4180b08ea46ae5cd0429daed5d8 100644 (file)
@@ -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)
                }
        }