summaryrefslogtreecommitdiffstats
path: root/tpl/internal/go_templates/texttemplate/link_test.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-12-03 13:50:17 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-02-18 14:11:48 +0100
commitcf3e077da304e6f4d7c22f8296e1382335d055c6 (patch)
treedbcec1614cc8e8de56921f4a316fd316dee07f67 /tpl/internal/go_templates/texttemplate/link_test.go
parent66beac99c64b5e5fe7bec0bda437ba5858d49a36 (diff)
tpl/internal: Synch Go templates fork with Go 1.16dev
Diffstat (limited to 'tpl/internal/go_templates/texttemplate/link_test.go')
-rw-r--r--tpl/internal/go_templates/texttemplate/link_test.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/tpl/internal/go_templates/texttemplate/link_test.go b/tpl/internal/go_templates/texttemplate/link_test.go
new file mode 100644
index 000000000..3db8c21e3
--- /dev/null
+++ b/tpl/internal/go_templates/texttemplate/link_test.go
@@ -0,0 +1,66 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build go1.13
+
+package template_test
+
+import (
+ "bytes"
+ "github.com/gohugoio/hugo/tpl/internal/go_templates/testenv"
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "testing"
+)
+
+// Issue 36021: verify that text/template doesn't prevent the linker from removing
+// unused methods.
+func _TestLinkerGC(t *testing.T) {
+ if testing.Short() {
+ t.Skip("skipping in short mode")
+ }
+ testenv.MustHaveGoBuild(t)
+ const prog = `package main
+
+import (
+ _ "text/template"
+)
+
+type T struct{}
+
+func (t *T) Unused() { println("THIS SHOULD BE ELIMINATED") }
+func (t *T) Used() {}
+
+var sink *T
+
+func main() {
+ var t T
+ sink = &t
+ t.Used()
+}
+`
+ td, err := ioutil.TempDir("", "text_template_TestDeadCodeElimination")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(td)
+
+ if err := ioutil.WriteFile(filepath.Join(td, "x.go"), []byte(prog), 0644); err != nil {
+ t.Fatal(err)
+ }
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "x.exe", "x.go")
+ cmd.Dir = td
+ if out, err := cmd.CombinedOutput(); err != nil {
+ t.Fatalf("go build: %v, %s", err, out)
+ }
+ slurp, err := ioutil.ReadFile(filepath.Join(td, "x.exe"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if bytes.Contains(slurp, []byte("THIS SHOULD BE ELIMINATED")) {
+ t.Error("binary contains code that should be deadcode eliminated")
+ }
+}