Skip to content

Commit 5d7fba2

Browse files
committed
Add a test for banned packages in randomizer-lambda
This is to help keep me honest about my recent optimization work, and is again influenced by the fact that the Cobra change included a test. Note that I don't just match MethodByName like that test does. We have some MethodByName calls in the final binary, which are fine because the compiler can statically determine the methods being looked up. It's only when you call that method with a dynamic value that you block all possibility of dead code elimination.
1 parent 18b66d6 commit 5d7fba2

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

cmd/randomizer-lambda/main_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"path/filepath"
7+
"runtime"
8+
"strings"
9+
"testing"
10+
)
11+
12+
func TestBannedPackages(t *testing.T) {
13+
// According to https://github.com/spf13/cobra/blob/v1.9.0/cobra_test.go#L247,
14+
// `go tool nm` doesn't work on Windows.
15+
if runtime.GOOS == "windows" {
16+
t.SkipNow()
17+
}
18+
19+
const name = "randomizer-lambda"
20+
binpath := filepath.Join(t.TempDir(), name)
21+
build := exec.Command("go", "build", "-tags=lambda.norpc,grpcnotrace", "-o", binpath, ".")
22+
build.Stdout, build.Stderr = os.Stdout, os.Stderr
23+
if err := build.Run(); err != nil {
24+
t.Fatalf("failed to build: %v", err)
25+
}
26+
27+
var nmout strings.Builder
28+
nm := exec.Command("go", "tool", "nm", binpath)
29+
nm.Stdout, nm.Stderr = &nmout, os.Stderr
30+
if err := nm.Run(); err != nil {
31+
t.Fatalf("failed to run go tool nm: %v", err)
32+
}
33+
34+
if strings.Contains(nmout.String(), "T text/template.") {
35+
t.Errorf("%s imports text/template, blocking dead code elimination", name)
36+
}
37+
if strings.Contains(nmout.String(), "T html/template.") {
38+
t.Errorf("%s imports html/template, blocking dead code elimination", name)
39+
}
40+
}

0 commit comments

Comments
 (0)