diff options
| author | Bryan C. Mills <bcmills@google.com> | 2017-09-12 18:28:02 -0400 |
|---|---|---|
| committer | Bryan Mills <bcmills@google.com> | 2017-09-13 17:45:52 +0000 |
| commit | d02477e994b443ef45851e266381800ea5031859 (patch) | |
| tree | fbc83a2d4e36197cbd73ca84053faf76c8077d56 /misc/cgo/errors/errors_test.go | |
| parent | 9593b74a3c993c4ba472163e4011c9feec97e7b9 (diff) | |
| download | go-d02477e994b443ef45851e266381800ea5031859.tar.xz | |
misc/cgo/errors: port test.bash to Go
This makes the test easier to run in isolation and easier to change,
and simplifies the code to run the tests in parallel.
updates #13467
Change-Id: I5622b5cc98276970347da18e95d071dbca3c5cc1
Reviewed-on: https://go-review.googlesource.com/63276
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'misc/cgo/errors/errors_test.go')
| -rw-r--r-- | misc/cgo/errors/errors_test.go | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/misc/cgo/errors/errors_test.go b/misc/cgo/errors/errors_test.go new file mode 100644 index 0000000000..2924993714 --- /dev/null +++ b/misc/cgo/errors/errors_test.go @@ -0,0 +1,151 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package errorstest + +import ( + "bytes" + "fmt" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "regexp" + "strconv" + "strings" + "testing" +) + +func path(file string) string { + return filepath.Join("src", file) +} + +func check(t *testing.T, file string) { + t.Run(file, func(t *testing.T) { + t.Parallel() + + contents, err := ioutil.ReadFile(path(file)) + if err != nil { + t.Fatal(err) + } + var errors []*regexp.Regexp + for i, line := range bytes.Split(contents, []byte("\n")) { + if !bytes.Contains(line, []byte("ERROR HERE")) { + continue + } + var re *regexp.Regexp + frags := bytes.SplitAfterN(line, []byte("ERROR HERE: "), 1) + 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 + } + } + errors = append(errors, re) + } + if len(errors) == 0 { + t.Fatalf("cannot find ERROR HERE") + } + expect(t, file, errors) + }) +} + +func expect(t *testing.T, file string, errors []*regexp.Regexp) { + cmd := exec.Command("go", "build", "-gcflags=-C", path(file)) + out, err := cmd.CombinedOutput() + if err == nil { + t.Errorf("expected cgo to fail but it succeeded") + } + + lines := bytes.Split(out, []byte("\n")) + for _, re := range errors { + found := false + for _, line := range lines { + if re.Match(line) { + found = true + break + } + } + if !found { + t.Errorf("expected error output to contain %q", re) + } + } + + if t.Failed() { + t.Logf("actual output:\n%s", out) + } +} + +func sizeofLongDouble(t *testing.T) int { + cmd := exec.Command("go", "run", path("long_double_size.go")) + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("%#q: %v:\n%s", strings.Join(cmd.Args, " "), err, out) + } + + i, err := strconv.Atoi(strings.TrimSpace(string(out))) + if err != nil { + t.Fatalf("long_double_size.go printed invalid size: %s", out) + } + return i +} + +func TestReportsTypeErrors(t *testing.T) { + for _, file := range []string{ + "err1.go", + "err2.go", + "err3.go", + "issue7757.go", + "issue8442.go", + "issue11097a.go", + "issue11097b.go", + "issue13129.go", + "issue13423.go", + "issue13635.go", + "issue13830.go", + "issue16116.go", + "issue16591.go", + "issue18452.go", + "issue18889.go", + } { + check(t, file) + } + + if sizeofLongDouble(t) > 8 { + check(t, "err4.go") + } +} + +func TestToleratesOptimizationFlag(t *testing.T) { + for _, cflags := range []string{ + "", + "-O", + } { + cflags := cflags + t.Run(cflags, func(t *testing.T) { + t.Parallel() + + cmd := exec.Command("go", "build", path("issue14669.go")) + cmd.Env = append(os.Environ(), "CGO_CFLAGS="+cflags) + out, err := cmd.CombinedOutput() + if err != nil { + t.Errorf("%#q: %v:\n%s", strings.Join(cmd.Args, " "), err, out) + } + }) + } +} + +func TestMallocCrashesOnNil(t *testing.T) { + t.Parallel() + + cmd := exec.Command("go", "run", path("malloc.go")) + out, err := cmd.CombinedOutput() + if err == nil { + t.Logf("%#q:\n%s", strings.Join(cmd.Args, " "), out) + t.Fatalf("succeeded unexpectedly") + } +} |
