aboutsummaryrefslogtreecommitdiff
path: root/cmd/callgraph/main_test.go
blob: 7e838f774e52a5dbf632b099da6f6604b8e34656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// No testdata on Android.

//go:build !android && go1.11
// +build !android,go1.11

package main

import (
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"golang.org/x/tools/internal/testenv"
)

func init() {
	// This test currently requires GOPATH mode.
	// Explicitly disabling module mode should suffix, but
	// we'll also turn off GOPROXY just for good measure.
	if err := os.Setenv("GO111MODULE", "off"); err != nil {
		log.Fatal(err)
	}
	if err := os.Setenv("GOPROXY", "off"); err != nil {
		log.Fatal(err)
	}
}

func TestCallgraph(t *testing.T) {
	if b := os.Getenv("GO_BUILDER_NAME"); b == "windows-arm64-10" {
		t.Skipf("skipping due to suspected file corruption bug on %s builder (https://go.dev/issue/50706)", b)
	}

	testenv.NeedsTool(t, "go")

	gopath, err := filepath.Abs("testdata")
	if err != nil {
		t.Fatal(err)
	}

	for _, test := range []struct {
		algo  string
		tests bool
		want  []string
	}{
		{"rta", false, []string{
			// rta imprecisely shows cross product of {main,main2} x {C,D}
			`pkg.main --> (pkg.C).f`,
			`pkg.main --> (pkg.D).f`,
			`pkg.main --> pkg.main2`,
			`pkg.main2 --> (pkg.C).f`,
			`pkg.main2 --> (pkg.D).f`,
		}},
		{"pta", false, []string{
			// pta distinguishes main->C, main2->D.  Also has a root node.
			`<root> --> pkg.init`,
			`<root> --> pkg.main`,
			`pkg.main --> (pkg.C).f`,
			`pkg.main --> pkg.main2`,
			`pkg.main2 --> (pkg.D).f`,
		}},
		// tests: both the package's main and the test's main are called.
		// The callgraph includes all the guts of the "testing" package.
		{"rta", true, []string{
			`pkg.test.main --> testing.MainStart`,
			`testing.runExample --> pkg.Example`,
			`pkg.Example --> (pkg.C).f`,
			`pkg.main --> (pkg.C).f`,
		}},
		{"pta", true, []string{
			`<root> --> pkg.test.main`,
			`<root> --> pkg.main`,
			`pkg.test.main --> testing.MainStart`,
			`testing.runExample --> pkg.Example`,
			`pkg.Example --> (pkg.C).f`,
			`pkg.main --> (pkg.C).f`,
		}},
	} {
		const format = "{{.Caller}} --> {{.Callee}}"
		stdout = new(bytes.Buffer)
		if err := doCallgraph("testdata/src", gopath, test.algo, format, test.tests, []string{"pkg"}); err != nil {
			t.Error(err)
			continue
		}

		edges := make(map[string]bool)
		for _, line := range strings.Split(fmt.Sprint(stdout), "\n") {
			edges[line] = true
		}
		for _, edge := range test.want {
			if !edges[edge] {
				t.Errorf("callgraph(%q, %t): missing edge: %s",
					test.algo, test.tests, edge)
			}
		}
		if t.Failed() {
			t.Log("got:\n", stdout)
		}
	}
}