2017-09-12 16:28:02 -06:00
|
|
|
// 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"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func path(file string) string {
|
2019-02-21 07:06:09 -07:00
|
|
|
return filepath.Join("testdata", file)
|
2017-09-12 16:28:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func check(t *testing.T, file string) {
|
|
|
|
t.Run(file, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2021-04-03 02:10:47 -06:00
|
|
|
contents, err := os.ReadFile(path(file))
|
2017-09-12 16:28:02 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var errors []*regexp.Regexp
|
|
|
|
for i, line := range bytes.Split(contents, []byte("\n")) {
|
2017-09-13 13:14:05 -06:00
|
|
|
if bytes.HasSuffix(line, []byte("ERROR HERE")) {
|
|
|
|
re := regexp.MustCompile(regexp.QuoteMeta(fmt.Sprintf("%s:%d:", file, i+1)))
|
|
|
|
errors = append(errors, re)
|
2017-09-12 16:28:02 -06:00
|
|
|
continue
|
|
|
|
}
|
2017-09-13 13:14:05 -06:00
|
|
|
|
|
|
|
frags := bytes.SplitAfterN(line, []byte("ERROR HERE: "), 2)
|
2017-09-12 16:28:02 -06:00
|
|
|
if len(frags) == 1 {
|
2017-09-13 13:14:05 -06:00
|
|
|
continue
|
|
|
|
}
|
2021-06-04 09:16:58 -06:00
|
|
|
frag := fmt.Sprintf(":%d:.*%s", i+1, frags[1])
|
|
|
|
re, err := regexp.Compile(frag)
|
2017-09-13 13:14:05 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Invalid regexp after `ERROR HERE: `: %#q", frags[1])
|
|
|
|
continue
|
2017-09-12 16:28:02 -06:00
|
|
|
}
|
|
|
|
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) {
|
2021-04-03 02:10:47 -06:00
|
|
|
dir, err := os.MkdirTemp("", filepath.Base(t.Name()))
|
2017-09-13 13:14:05 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
dst := filepath.Join(dir, strings.TrimSuffix(file, ".go"))
|
2019-05-16 08:00:10 -06:00
|
|
|
cmd := exec.Command("go", "build", "-gcflags=-L -e", "-o="+dst, path(file)) // TODO(gri) no need for -gcflags=-L if go tool is adjusted
|
2017-09-12 16:28:02 -06:00
|
|
|
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) {
|
2017-09-13 13:14:05 -06:00
|
|
|
t.Logf("found match for %#q: %q", re, line)
|
2017-09-12 16:28:02 -06:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2017-09-13 13:14:05 -06:00
|
|
|
t.Errorf("expected error output to contain %#q", re)
|
2017-09-12 16:28:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
"issue11097a.go",
|
|
|
|
"issue11097b.go",
|
|
|
|
"issue18452.go",
|
|
|
|
"issue18889.go",
|
2018-12-04 16:01:07 -07:00
|
|
|
"issue28721.go",
|
2019-07-11 12:57:31 -06:00
|
|
|
"issue33061.go",
|
2017-09-12 16:28:02 -06:00
|
|
|
} {
|
|
|
|
check(t, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sizeofLongDouble(t) > 8 {
|
2018-12-03 11:53:10 -07:00
|
|
|
for _, file := range []string{
|
|
|
|
"err4.go",
|
|
|
|
"issue28069.go",
|
|
|
|
} {
|
|
|
|
check(t, file)
|
|
|
|
}
|
2017-09-12 16:28:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|