2020-12-03 07:50:17 -05:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package template_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2024-08-14 05:34:21 -04:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/tpl/internal/go_templates/testenv"
|
2020-12-03 07:50:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
`
|
2022-03-16 03:48:16 -04:00
|
|
|
td := t.TempDir()
|
2020-12-03 07:50:17 -05:00
|
|
|
|
2024-08-14 05:34:21 -04:00
|
|
|
if err := os.WriteFile(filepath.Join(td, "x.go"), []byte(prog), 0o644); err != nil {
|
2020-12-03 07:50:17 -05:00
|
|
|
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)
|
|
|
|
}
|
2021-01-29 11:15:42 -05:00
|
|
|
slurp, err := os.ReadFile(filepath.Join(td, "x.exe"))
|
2020-12-03 07:50:17 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|