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