Skip to content

Commit eedf561

Browse files
committed
go/analysis/unitchecker: relax integration test
The previous test made assertions about the combined error/output stream, which was inherently fragile. As CL 702815 will change go vet to report -diff and -json output to stdout, this test needs to accept both old and new variants across the transition. Longer term, this awful test needs to be rewritten. For golang/go#75432 Change-Id: Ieaa4bd5453b9ec189050d5e43356074047531d0f Reviewed-on: https://go-review.googlesource.com/c/tools/+/703415 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Michael Matloob <matloob@golang.org> Auto-Submit: Alan Donovan <adonovan@google.com>
1 parent f2e5ab6 commit eedf561

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

go/analysis/unitchecker/unitchecker_test.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package unitchecker_test
66

77
import (
88
"flag"
9+
"fmt"
910
"os"
1011
"os/exec"
1112
"regexp"
@@ -89,29 +90,29 @@ func _() {
8990
}}})
9091
defer exported.Cleanup()
9192

92-
const wantA = `# golang.org/fake/a
93-
([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11: call of MyFunc123\(...\)
93+
const wantA = `
94+
.*a/a.go:4:11: call of MyFunc123\(...\)
9495
`
95-
const wantB = `# golang.org/fake/b
96-
([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?b/b.go:6:13: call of MyFunc123\(...\)
97-
([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?b/b.go:7:11: call of MyFunc123\(...\)
96+
const wantB = `
97+
.*b/b.go:6:13: call of MyFunc123\(...\)
98+
.*b/b.go:7:11: call of MyFunc123\(...\)
9899
`
99-
const wantC = `# golang.org/fake/c
100-
([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?c/c.go:5:5: self-assignment of i
100+
const wantC = `
101+
.*c/c.go:5:5: self-assignment of i
101102
`
102-
const wantAJSON = `# golang.org/fake/a
103+
const wantAJSON = `
103104
\{
104105
"golang.org/fake/a": \{
105106
"findcall": \[
106107
\{
107-
"posn": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11",
108+
"posn": ".*a/a.go:4:11",
108109
"message": "call of MyFunc123\(...\)",
109110
"suggested_fixes": \[
110111
\{
111112
"message": "Add '_TEST_'",
112113
"edits": \[
113114
\{
114-
"filename": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go",
115+
"filename": ".*a/a.go",
115116
"start": 32,
116117
"end": 32,
117118
"new": "_TEST_"
@@ -124,19 +125,19 @@ func _() {
124125
\}
125126
\}
126127
`
127-
const wantCJSON = `# golang.org/fake/c
128+
const wantCJSON = `
128129
\{
129130
"golang.org/fake/c": \{
130131
"assign": \[
131132
\{
132-
"posn": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?c/c.go:5:5",
133+
"posn": ".*c/c.go:5:5",
133134
"message": "self-assignment of i",
134135
"suggested_fixes": \[
135136
\{
136137
"message": "Remove self-assignment",
137138
"edits": \[
138139
\{
139-
"filename": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?c/c.go",
140+
"filename": ".*c/c.go",
140141
"start": 37,
141142
"end": 42,
142143
"new": ""
@@ -151,30 +152,27 @@ func _() {
151152
`
152153
for _, test := range []struct {
153154
args string
154-
wantOut string
155+
wantOut string // multiline regular expression
155156
wantExitError bool
156157
}{
157158
{args: "golang.org/fake/a", wantOut: wantA, wantExitError: true},
158159
{args: "golang.org/fake/b", wantOut: wantB, wantExitError: true},
159160
{args: "golang.org/fake/c", wantOut: wantC, wantExitError: true},
160-
{args: "golang.org/fake/a golang.org/fake/b", wantOut: wantA + wantB, wantExitError: true},
161+
{args: "golang.org/fake/a golang.org/fake/b", wantOut: wantA + ".*" + wantB, wantExitError: true},
161162
{args: "-json golang.org/fake/a", wantOut: wantAJSON, wantExitError: false},
162163
{args: "-json golang.org/fake/c", wantOut: wantCJSON, wantExitError: false},
163164
{args: "-c=0 golang.org/fake/a", wantOut: wantA + "4 MyFunc123\\(\\)\n", wantExitError: true},
164165
} {
165166
cmd := exec.Command("go", "vet", "-vettool="+os.Args[0], "-findcall.name=MyFunc123")
167+
cmd.Stdout = new(strings.Builder)
168+
cmd.Stderr = new(strings.Builder)
166169
cmd.Args = append(cmd.Args, strings.Fields(test.args)...)
167170
cmd.Env = append(exported.Config.Env, "ENTRYPOINT=minivet")
168171
cmd.Dir = exported.Config.Dir
169172

170-
// TODO(golang/go#65729): this is unsound: any extra
171-
// logging by the child process (e.g. due to GODEBUG
172-
// options) will add noise to stderr, causing the
173-
// CombinedOutput to be unparsable as JSON. But we
174-
// can't simply use Output here as some of the tests
175-
// look for substrings of stderr. Rework the test to
173+
// TODO(golang/go#65729): Rework the test to
176174
// be specific about which output stream to match.
177-
out, err := cmd.CombinedOutput()
175+
err := cmd.Run()
178176
exitcode := 0
179177
if exitErr, ok := err.(*exec.ExitError); ok {
180178
exitcode = exitErr.ExitCode()
@@ -187,7 +185,8 @@ func _() {
187185
t.Errorf("%s: got exit code %d, want %s", test.args, exitcode, want)
188186
}
189187

190-
matched, err := regexp.Match(test.wantOut, out)
188+
out := fmt.Sprintf("stdout:\n%s\nstderr:\n%s\n", cmd.Stdout, cmd.Stderr)
189+
matched, err := regexp.MatchString("(?s)"+test.wantOut, out)
191190
if err != nil {
192191
t.Fatalf("regexp.Match(<<%s>>): %v", test.wantOut, err)
193192
}

0 commit comments

Comments
 (0)