github.com/april1989/origin-go-tools@v0.0.32/cmd/callgraph/main_test.go (about) 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. 4 5 // No testdata on Android. 6 7 // +build !android 8 // +build go1.11 9 10 package main 11 12 import ( 13 "bytes" 14 "fmt" 15 "log" 16 "os" 17 "path/filepath" 18 "strings" 19 "testing" 20 21 "github.com/april1989/origin-go-tools/internal/testenv" 22 ) 23 24 func init() { 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 { 29 log.Fatal(err) 30 } 31 if err := os.Setenv("GOPROXY", "off"); err != nil { 32 log.Fatal(err) 33 } 34 } 35 36 func TestCallgraph(t *testing.T) { 37 testenv.NeedsTool(t, "go") 38 39 gopath, err := filepath.Abs("testdata") 40 if err != nil { 41 t.Fatal(err) 42 } 43 44 for _, test := range []struct { 45 algo string 46 tests bool 47 want []string 48 }{ 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`, 56 }}, 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`, 64 }}, 65 // bz: comment; not needed test cases included 66 //// tests: both the package's main and the test's main are called. 67 //// The callgraph includes all the guts of the "golibexec_testing" package. 68 //{"rta", true, []string{ 69 // `pkg.test.main --> golibexec_testing.MainStart`, 70 // `golibexec_testing.runExample --> pkg.Example`, 71 // `pkg.Example --> (pkg.C).f`, 72 // `pkg.main --> (pkg.C).f`, 73 //}}, 74 //{"pta", true, []string{ 75 // `<root> --> pkg.test.main`, 76 // `<root> --> pkg.main`, 77 // `pkg.test.main --> golibexec_testing.MainStart`, 78 // `golibexec_testing.runExample --> pkg.Example`, 79 // `pkg.Example --> (pkg.C).f`, 80 // `pkg.main --> (pkg.C).f`, 81 //}}, 82 } { 83 const format = "graphviz" // bz: change from "{{.Caller}} --> {{.Callee}}" to "graphviz", i wanna see it ... 84 stdout = new(bytes.Buffer) 85 if err := doCallgraph("testdata/src", gopath, test.algo, format, test.tests, []string{"pkg"}); err != nil { 86 t.Error(err) 87 continue 88 } 89 90 edges := make(map[string]bool) 91 for _, line := range strings.Split(fmt.Sprint(stdout), "\n") { 92 edges[line] = true 93 } 94 for _, edge := range test.want { 95 if !edges[edge] { 96 t.Errorf("callgraph(%q, %t): missing edge: %s", 97 test.algo, test.tests, edge) 98 } 99 } 100 if t.Failed() { 101 t.Log("got:\n", stdout) 102 } 103 } 104 }