1
2
3
4
5 package ld
6
7 import (
8 "bytes"
9 "internal/testenv"
10 "path/filepath"
11 "testing"
12 )
13
14 func TestDeadcode(t *testing.T) {
15 testenv.MustHaveGoBuild(t)
16 t.Parallel()
17
18 tmpdir := t.TempDir()
19
20 tests := []struct {
21 src string
22 pos, neg []string
23 }{
24 {"reflectcall", nil, []string{"main.T.M"}},
25 {"typedesc", nil, []string{"type:main.T"}},
26 {"ifacemethod", nil, []string{"main.T.M"}},
27 {"ifacemethod2", []string{"main.T.M"}, nil},
28 {"ifacemethod3", []string{"main.S.M"}, nil},
29 {"ifacemethod4", nil, []string{"main.T.M"}},
30 {"ifacemethod5", []string{"main.S.M"}, nil},
31 {"ifacemethod6", []string{"main.S.M"}, []string{"main.S.N"}},
32 {"structof_funcof", []string{"main.S.M"}, []string{"main.S.N"}},
33 {"globalmap", []string{"main.small", "main.effect"},
34 []string{"main.large"}},
35 }
36 for _, test := range tests {
37 t.Run(test.src, func(t *testing.T) {
38 t.Parallel()
39 src := filepath.Join("testdata", "deadcode", test.src+".go")
40 exe := filepath.Join(tmpdir, test.src+".exe")
41 cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-dumpdep", "-o", exe, src)
42 out, err := cmd.CombinedOutput()
43 if err != nil {
44 t.Fatalf("%v: %v:\n%s", cmd.Args, err, out)
45 }
46 for _, pos := range test.pos {
47 if !bytes.Contains(out, []byte(pos+"\n")) {
48 t.Errorf("%s should be reachable. Output:\n%s", pos, out)
49 }
50 }
51 for _, neg := range test.neg {
52 if bytes.Contains(out, []byte(neg+"\n")) {
53 t.Errorf("%s should not be reachable. Output:\n%s", neg, out)
54 }
55 }
56 })
57 }
58 }
59
View as plain text