go/analysis/unitchecker: rewrite test in terms of packagestest

This means the test excercise modules and gopath workspaces. That will
allow this test to run once tools becomes a module...

The test still doesn't pass under Windows.

Change-Id: Ic5e46b9b92c5aac909a6ceb56f30851832fc09ac
Reviewed-on: https://go-review.googlesource.com/c/162404
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Michael Matloob 2019-02-13 16:57:09 -05:00
parent f3aee3358b
commit 88f95592de
3 changed files with 59 additions and 51 deletions

View File

@ -1,7 +0,0 @@
package a
func _() {
MyFunc123()
}
func MyFunc123() {}

View File

@ -1,10 +0,0 @@
package b
import "a"
func _() {
a.MyFunc123()
MyFunc123()
}
func MyFunc123() {}

View File

@ -14,14 +14,15 @@ import (
"flag"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"testing"
"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/findcall"
"golang.org/x/tools/go/analysis/passes/printf"
"golang.org/x/tools/go/analysis/unitchecker"
"golang.org/x/tools/go/packages/packagestest"
)
func TestMain(m *testing.M) {
@ -31,7 +32,6 @@ func TestMain(m *testing.M) {
panic("unreachable")
}
// test
flag.Parse()
os.Exit(m.Run())
}
@ -46,31 +46,55 @@ func main() {
// This is a very basic integration test of modular
// analysis with facts using unitchecker under "go vet".
// It fork/execs the main function above.
func TestIntegration(t *testing.T) {
if runtime.GOOS != "linux" {
func TestIntegration(t *testing.T) { packagestest.TestAll(t, testIntegration) }
func testIntegration(t *testing.T, exporter packagestest.Exporter) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skipf("skipping fork/exec test on this platform")
}
testdata := analysistest.TestData()
exported := packagestest.Export(t, exporter, []packagestest.Module{{
Name: "golang.org/fake",
Files: map[string]interface{}{
"a/a.go": `package a
const wantA = `# a
testdata/src/a/a.go:4:11: call of MyFunc123(...)
func _() {
MyFunc123()
}
func MyFunc123() {}
`,
"b/b.go": `package b
import "golang.org/fake/a"
func _() {
a.MyFunc123()
MyFunc123()
}
func MyFunc123() {}
`,
}}})
defer exported.Cleanup()
const wantA = `# golang.org/fake/a
([/._a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11: call of MyFunc123\(...\)
`
const wantB = `# b
testdata/src/b/b.go:6:13: call of MyFunc123(...)
testdata/src/b/b.go:7:11: call of MyFunc123(...)
const wantB = `# golang.org/fake/b
([/._a-zA-Z0-9]+[\\/]fake[\\/])?b/b.go:6:13: call of MyFunc123\(...\)
([/._a-zA-Z0-9]+[\\/]fake[\\/])?b/b.go:7:11: call of MyFunc123\(...\)
`
const wantAJSON = `# a
{
"a": {
"findcall": [
{
"posn": "$GOPATH/src/a/a.go:4:11",
"message": "call of MyFunc123(...)"
}
]
}
}
const wantAJSON = `# golang.org/fake/a
\{
"golang.org/fake/a": \{
"findcall": \[
\{
"posn": "([/._a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11",
"message": "call of MyFunc123\(...\)"
\}
\]
\}
\}
`
for _, test := range []struct {
@ -78,18 +102,16 @@ testdata/src/b/b.go:7:11: call of MyFunc123(...)
wantOut string
wantExit int
}{
{args: "a", wantOut: wantA, wantExit: 2},
{args: "b", wantOut: wantB, wantExit: 2},
{args: "a b", wantOut: wantA + wantB, wantExit: 2},
{args: "-json a", wantOut: wantAJSON, wantExit: 0},
{args: "-c=0 a", wantOut: wantA + "4 MyFunc123()\n", wantExit: 2},
{args: "golang.org/fake/a", wantOut: wantA, wantExit: 2},
{args: "golang.org/fake/b", wantOut: wantB, wantExit: 2},
{args: "golang.org/fake/a golang.org/fake/b", wantOut: wantA + wantB, wantExit: 2},
{args: "-json golang.org/fake/a", wantOut: wantAJSON, wantExit: 0},
{args: "-c=0 golang.org/fake/a", wantOut: wantA + "4 MyFunc123\\(\\)\n", wantExit: 2},
} {
cmd := exec.Command("go", "vet", "-vettool="+os.Args[0], "-findcall.name=MyFunc123")
cmd.Args = append(cmd.Args, strings.Fields(test.args)...)
cmd.Env = append(os.Environ(),
"UNITCHECKER_CHILD=1",
"GOPATH="+testdata,
)
cmd.Env = append(exported.Config.Env, "UNITCHECKER_CHILD=1")
cmd.Dir = exported.Config.Dir
out, err := cmd.CombinedOutput()
exitcode := 0
@ -99,10 +121,13 @@ testdata/src/b/b.go:7:11: call of MyFunc123(...)
if exitcode != test.wantExit {
t.Errorf("%s: got exit code %d, want %d", test.args, exitcode, test.wantExit)
}
got := strings.Replace(string(out), testdata, "$GOPATH", -1)
if got != test.wantOut {
t.Errorf("%s: got <<%s>>, want <<%s>>", test.args, got, test.wantOut)
matched, err := regexp.Match(test.wantOut, out)
if err != nil {
t.Fatal(err)
}
if !matched {
t.Errorf("%s: got <<%s>>, want match of regexp <<%s>>", test.args, out, test.wantOut)
}
}
}