1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 func path(file string) string {
20 return filepath.Join("testdata", file)
23 func check(t *testing.T, file string) {
24 t.Run(file, func(t *testing.T) {
27 contents, err := os.ReadFile(path(file))
31 var errors []*regexp.Regexp
32 for i, line := range bytes.Split(contents, []byte("\n")) {
33 if bytes.HasSuffix(line, []byte("ERROR HERE")) {
34 re := regexp.MustCompile(regexp.QuoteMeta(fmt.Sprintf("%s:%d:", file, i+1)))
35 errors = append(errors, re)
39 _, frag, ok := bytes.Cut(line, []byte("ERROR HERE: "))
43 re, err := regexp.Compile(fmt.Sprintf(":%d:.*%s", i+1, frag))
45 t.Errorf("Invalid regexp after `ERROR HERE: `: %#q", frag)
48 errors = append(errors, re)
51 t.Fatalf("cannot find ERROR HERE")
53 expect(t, file, errors)
57 func expect(t *testing.T, file string, errors []*regexp.Regexp) {
58 dir, err := os.MkdirTemp("", filepath.Base(t.Name()))
62 defer os.RemoveAll(dir)
64 dst := filepath.Join(dir, strings.TrimSuffix(file, ".go"))
65 cmd := exec.Command("go", "build", "-gcflags=-L -e", "-o="+dst, path(file)) // TODO(gri) no need for -gcflags=-L if go tool is adjusted
66 out, err := cmd.CombinedOutput()
68 t.Errorf("expected cgo to fail but it succeeded")
71 lines := bytes.Split(out, []byte("\n"))
72 for _, re := range errors {
74 for _, line := range lines {
76 t.Logf("found match for %#q: %q", re, line)
82 t.Errorf("expected error output to contain %#q", re)
87 t.Logf("actual output:\n%s", out)
91 func sizeofLongDouble(t *testing.T) int {
92 cmd := exec.Command("go", "run", path("long_double_size.go"))
93 out, err := cmd.CombinedOutput()
95 t.Fatalf("%#q: %v:\n%s", strings.Join(cmd.Args, " "), err, out)
98 i, err := strconv.Atoi(strings.TrimSpace(string(out)))
100 t.Fatalf("long_double_size.go printed invalid size: %s", out)
105 func TestReportsTypeErrors(t *testing.T) {
106 for _, file := range []string{
120 if sizeofLongDouble(t) > 8 {
121 for _, file := range []string{
130 func TestToleratesOptimizationFlag(t *testing.T) {
131 for _, cflags := range []string{
136 t.Run(cflags, func(t *testing.T) {
139 cmd := exec.Command("go", "build", path("issue14669.go"))
140 cmd.Env = append(os.Environ(), "CGO_CFLAGS="+cflags)
141 out, err := cmd.CombinedOutput()
143 t.Errorf("%#q: %v:\n%s", strings.Join(cmd.Args, " "), err, out)
149 func TestMallocCrashesOnNil(t *testing.T) {
152 cmd := exec.Command("go", "run", path("malloc.go"))
153 out, err := cmd.CombinedOutput()
155 t.Logf("%#q:\n%s", strings.Join(cmd.Args, " "), out)
156 t.Fatalf("succeeded unexpectedly")