-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathdce_test.go
More file actions
49 lines (39 loc) · 1.28 KB
/
Copy pathdce_test.go
File metadata and controls
49 lines (39 loc) · 1.28 KB
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
package kingpin
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/require"
)
// TestDeadCodeElimination verifies that programs using kingpin's default
// UsageRenderer do not link in reflect.MethodByName, which is the key
// indicator that dead code elimination is working.
func TestDeadCodeElimination(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("go tool nm not reliable on Windows")
}
dir := t.TempDir()
filename := filepath.Join(dir, "main.go")
err := os.WriteFile(filename, []byte(`package main
import (
"os"
"github.com/alecthomas/kingpin/v2"
)
func main() {
app := kingpin.New("test", "A test app.")
app.UsageRenderer(kingpin.RenderDefault)
app.Flag("verbose", "Enable verbose mode.").Bool()
app.Command("sub", "A subcommand.")
app.Parse(os.Args[1:])
}
`), 0o600)
require.NoError(t, err)
binPath := filepath.Join(dir, "test_binary")
buf, err := exec.Command("go", "build", "-trimpath", "-o", binPath, filename).CombinedOutput()
require.NoError(t, err, "go build failed: %s", buf)
buf, err = exec.Command("go", "tool", "nm", binPath).CombinedOutput()
require.NoError(t, err, "go tool nm failed: %s", buf)
require.NotContains(t, string(buf), "MethodByName", "text/template was not eliminated by dead code elimination")
}