1 // Copyright 2014 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.
5 // No testdata on Android.
21 "golang.org/x/tools/internal/testenv"
25 // This test currently requires GOPATH mode.
26 // Explicitly disabling module mode should suffix, but
27 // we'll also turn off GOPROXY just for good measure.
28 if err := os.Setenv("GO111MODULE", "off"); err != nil {
31 if err := os.Setenv("GOPROXY", "off"); err != nil {
36 func TestCallgraph(t *testing.T) {
37 testenv.NeedsTool(t, "go")
39 gopath, err := filepath.Abs("testdata")
44 for _, test := range []struct {
49 {"rta", false, []string{
50 // rta imprecisely shows cross product of {main,main2} x {C,D}
51 `pkg.main --> (pkg.C).f`,
52 `pkg.main --> (pkg.D).f`,
53 `pkg.main --> pkg.main2`,
54 `pkg.main2 --> (pkg.C).f`,
55 `pkg.main2 --> (pkg.D).f`,
57 {"pta", false, []string{
58 // pta distinguishes main->C, main2->D. Also has a root node.
59 `<root> --> pkg.init`,
60 `<root> --> pkg.main`,
61 `pkg.main --> (pkg.C).f`,
62 `pkg.main --> pkg.main2`,
63 `pkg.main2 --> (pkg.D).f`,
65 // tests: both the package's main and the test's main are called.
66 // The callgraph includes all the guts of the "testing" package.
67 {"rta", true, []string{
68 `pkg.test.main --> testing.MainStart`,
69 `testing.runExample --> pkg.Example`,
70 `pkg.Example --> (pkg.C).f`,
71 `pkg.main --> (pkg.C).f`,
73 {"pta", true, []string{
74 `<root> --> pkg.test.main`,
75 `<root> --> pkg.main`,
76 `pkg.test.main --> testing.MainStart`,
77 `testing.runExample --> pkg.Example`,
78 `pkg.Example --> (pkg.C).f`,
79 `pkg.main --> (pkg.C).f`,
82 const format = "{{.Caller}} --> {{.Callee}}"
83 stdout = new(bytes.Buffer)
84 if err := doCallgraph("testdata/src", gopath, test.algo, format, test.tests, []string{"pkg"}); err != nil {
89 edges := make(map[string]bool)
90 for _, line := range strings.Split(fmt.Sprint(stdout), "\n") {
93 for _, edge := range test.want {
95 t.Errorf("callgraph(%q, %t): missing edge: %s",
96 test.algo, test.tests, edge)
100 t.Log("got:\n", stdout)