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.
20 func path(file
string) string {
21 return filepath
.Join("src", file
)
24 func check(t
*testing
.T
, file
string) {
25 t
.Run(file
, func(t
*testing
.T
) {
28 contents
, err
:= ioutil
.ReadFile(path(file
))
32 var errors
[]*regexp
.Regexp
33 for i
, line
:= range bytes
.Split(contents
, []byte("\n")) {
34 if bytes
.HasSuffix(line
, []byte("ERROR HERE")) {
35 re
:= regexp
.MustCompile(regexp
.QuoteMeta(fmt
.Sprintf("%s:%d:", file
, i
+1)))
36 errors
= append(errors
, re
)
40 frags
:= bytes
.SplitAfterN(line
, []byte("ERROR HERE: "), 2)
44 re
, err
:= regexp
.Compile(string(frags
[1]))
46 t
.Errorf("Invalid regexp after `ERROR HERE: `: %#q", frags
[1])
49 errors
= append(errors
, re
)
52 t
.Fatalf("cannot find ERROR HERE")
54 expect(t
, file
, errors
)
58 func expect(t
*testing
.T
, file
string, errors
[]*regexp
.Regexp
) {
59 dir
, err
:= ioutil
.TempDir("", filepath
.Base(t
.Name()))
63 defer os
.RemoveAll(dir
)
65 dst
:= filepath
.Join(dir
, strings
.TrimSuffix(file
, ".go"))
66 cmd
:= exec
.Command("go", "build", "-gcflags=-L", "-o="+dst
, path(file
)) // TODO(gri) no need for -gcflags=-L if go tool is adjusted
67 out
, err
:= cmd
.CombinedOutput()
69 t
.Errorf("expected cgo to fail but it succeeded")
72 lines
:= bytes
.Split(out
, []byte("\n"))
73 for _
, re
:= range errors
{
75 for _
, line
:= range lines
{
77 t
.Logf("found match for %#q: %q", re
, line
)
83 t
.Errorf("expected error output to contain %#q", re
)
88 t
.Logf("actual output:\n%s", out
)
92 func sizeofLongDouble(t
*testing
.T
) int {
93 cmd
:= exec
.Command("go", "run", path("long_double_size.go"))
94 out
, err
:= cmd
.CombinedOutput()
96 t
.Fatalf("%#q: %v:\n%s", strings
.Join(cmd
.Args
, " "), err
, out
)
99 i
, err
:= strconv
.Atoi(strings
.TrimSpace(string(out
)))
101 t
.Fatalf("long_double_size.go printed invalid size: %s", out
)
106 func TestReportsTypeErrors(t
*testing
.T
) {
107 for _
, file
:= range []string{
128 if sizeofLongDouble(t
) > 8 {
133 func TestToleratesOptimizationFlag(t
*testing
.T
) {
134 for _
, cflags
:= range []string{
139 t
.Run(cflags
, func(t
*testing
.T
) {
142 cmd
:= exec
.Command("go", "build", path("issue14669.go"))
143 cmd
.Env
= append(os
.Environ(), "CGO_CFLAGS="+cflags
)
144 out
, err
:= cmd
.CombinedOutput()
146 t
.Errorf("%#q: %v:\n%s", strings
.Join(cmd
.Args
, " "), err
, out
)
152 func TestMallocCrashesOnNil(t
*testing
.T
) {
155 cmd
:= exec
.Command("go", "run", path("malloc.go"))
156 out
, err
:= cmd
.CombinedOutput()
158 t
.Logf("%#q:\n%s", strings
.Join(cmd
.Args
, " "), out
)
159 t
.Fatalf("succeeded unexpectedly")