link_test.go (1332B)
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build go1.13
6 // +build go1.13
7
8 package template_test
9
10 import (
11 "bytes"
12 "github.com/gohugoio/hugo/tpl/internal/go_templates/testenv"
13 "os"
14 "os/exec"
15 "path/filepath"
16 "testing"
17 )
18
19 // Issue 36021: verify that text/template doesn't prevent the linker from removing
20 // unused methods.
21 func _TestLinkerGC(t *testing.T) {
22 if testing.Short() {
23 t.Skip("skipping in short mode")
24 }
25 testenv.MustHaveGoBuild(t)
26 const prog = `package main
27
28 import (
29 _ "text/template"
30 )
31
32 type T struct{}
33
34 func (t *T) Unused() { println("THIS SHOULD BE ELIMINATED") }
35 func (t *T) Used() {}
36
37 var sink *T
38
39 func main() {
40 var t T
41 sink = &t
42 t.Used()
43 }
44 `
45 td := t.TempDir()
46
47 if err := os.WriteFile(filepath.Join(td, "x.go"), []byte(prog), 0644); err != nil {
48 t.Fatal(err)
49 }
50 cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "x.exe", "x.go")
51 cmd.Dir = td
52 if out, err := cmd.CombinedOutput(); err != nil {
53 t.Fatalf("go build: %v, %s", err, out)
54 }
55 slurp, err := os.ReadFile(filepath.Join(td, "x.exe"))
56 if err != nil {
57 t.Fatal(err)
58 }
59 if bytes.Contains(slurp, []byte("THIS SHOULD BE ELIMINATED")) {
60 t.Error("binary contains code that should be deadcode eliminated")
61 }
62 }