Skip to content

Commit d02477e

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 9593b74 commit d02477e

24 files changed

+255
-242
lines changed

misc/cgo/errors/errors_test.go

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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.
4+
5+
package errorstest
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
"io/ioutil"
11+
"os"
12+
"os/exec"
13+
"path/filepath"
14+
"regexp"
15+
"strconv"
16+
"strings"
17+
"testing"
18+
)
19+
20+
func path(file string) string {
21+
return filepath.Join("src", file)
22+
}
23+
24+
func check(t *testing.T, file string) {
25+
t.Run(file, func(t *testing.T) {
26+
t.Parallel()
27+
28+
contents, err := ioutil.ReadFile(path(file))
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
var errors []*regexp.Regexp
33+
for i, line := range bytes.Split(contents, []byte("\n")) {
34+
if !bytes.Contains(line, []byte("ERROR HERE")) {
35+
continue
36+
}
37+
var re *regexp.Regexp
38+
frags := bytes.SplitAfterN(line, []byte("ERROR HERE: "), 1)
39+
if len(frags) == 1 {
40+
re = regexp.MustCompile(regexp.QuoteMeta(fmt.Sprintf("%s:%d:", file, i+1)))
41+
} else {
42+
re, err = regexp.Compile(string(frags[1]))
43+
if err != nil {
44+
t.Errorf("Invalid regexp after `ERROR HERE: `: %q", frags[1])
45+
continue
46+
}
47+
}
48+
errors = append(errors, re)
49+
}
50+
if len(errors) == 0 {
51+
t.Fatalf("cannot find ERROR HERE")
52+
}
53+
expect(t, file, errors)
54+
})
55+
}
56+
57+
func expect(t *testing.T, file string, errors []*regexp.Regexp) {
58+
cmd := exec.Command("go", "build", "-gcflags=-C", path(file))
59+
out, err := cmd.CombinedOutput()
60+
if err == nil {
61+
t.Errorf("expected cgo to fail but it succeeded")
62+
}
63+
64+
lines := bytes.Split(out, []byte("\n"))
65+
for _, re := range errors {
66+
found := false
67+
for _, line := range lines {
68+
if re.Match(line) {
69+
found = true
70+
break
71+
}
72+
}
73+
if !found {
74+
t.Errorf("expected error output to contain %q", re)
75+
}
76+
}
77+
78+
if t.Failed() {
79+
t.Logf("actual output:\n%s", out)
80+
}
81+
}
82+
83+
func sizeofLongDouble(t *testing.T) int {
84+
cmd := exec.Command("go", "run", path("long_double_size.go"))
85+
out, err := cmd.CombinedOutput()
86+
if err != nil {
87+
t.Fatalf("%#q: %v:\n%s", strings.Join(cmd.Args, " "), err, out)
88+
}
89+
90+
i, err := strconv.Atoi(strings.TrimSpace(string(out)))
91+
if err != nil {
92+
t.Fatalf("long_double_size.go printed invalid size: %s", out)
93+
}
94+
return i
95+
}
96+
97+
func TestReportsTypeErrors(t *testing.T) {
98+
for _, file := range []string{
99+
"err1.go",
100+
"err2.go",
101+
"err3.go",
102+
"issue7757.go",
103+
"issue8442.go",
104+
"issue11097a.go",
105+
"issue11097b.go",
106+
"issue13129.go",
107+
"issue13423.go",
108+
"issue13635.go",
109+
"issue13830.go",
110+
"issue16116.go",
111+
"issue16591.go",
112+
"issue18452.go",
113+
"issue18889.go",
114+
} {
115+
check(t, file)
116+
}
117+
118+
if sizeofLongDouble(t) > 8 {
119+
check(t, "err4.go")
120+
}
121+
}
122+
123+
func TestToleratesOptimizationFlag(t *testing.T) {
124+
for _, cflags := range []string{
125+
"",
126+
"-O",
127+
} {
128+
cflags := cflags
129+
t.Run(cflags, func(t *testing.T) {
130+
t.Parallel()
131+
132+
cmd := exec.Command("go", "build", path("issue14669.go"))
133+
cmd.Env = append(os.Environ(), "CGO_CFLAGS="+cflags)
134+
out, err := cmd.CombinedOutput()
135+
if err != nil {
136+
t.Errorf("%#q: %v:\n%s", strings.Join(cmd.Args, " "), err, out)
137+
}
138+
})
139+
}
140+
}
141+
142+
func TestMallocCrashesOnNil(t *testing.T) {
143+
t.Parallel()
144+
145+
cmd := exec.Command("go", "run", path("malloc.go"))
146+
out, err := cmd.CombinedOutput()
147+
if err == nil {
148+
t.Logf("%#q:\n%s", strings.Join(cmd.Args, " "), out)
149+
t.Fatalf("succeeded unexpectedly")
150+
}
151+
}

misc/cgo/errors/issue13635.go

-24
This file was deleted.

0 commit comments

Comments
 (0)