addressing review comments

This commit is contained in:
aarzilli 2024-10-15 10:33:24 +02:00
parent 4da4d97d60
commit 84ffa30b08
2 changed files with 19 additions and 4 deletions

View file

@ -227,6 +227,20 @@ func TestRpad(t *testing.T) {
} }
} }
// TestDeadcodeElimination checks that a simple program using cobra in its
// default configuration is linked taking full advantage of the linker's
// deadcode elimination step.
//
// If reflect.Value.MethodByName/reflect.Value.Method are reachable the
// linker will not always be able to prove that exported methods are
// unreachable, making deadcode elimination less effective. Using
// text/template and html/template makes reflect.Value.MethodByName
// reachable.
// Since cobra can use text/template templates this test checks that in its
// default configuration that code path can be proven to be unreachable by
// the linker.
//
// See also: https://github.com/spf13/cobra/pull/1956
func TestDeadcodeElimination(t *testing.T) { func TestDeadcodeElimination(t *testing.T) {
// check that a simple program using cobra in its default configuration is // check that a simple program using cobra in its default configuration is
// linked with deadcode elimination enabled. // linked with deadcode elimination enabled.
@ -234,7 +248,8 @@ func TestDeadcodeElimination(t *testing.T) {
dirname = "test_deadcode" dirname = "test_deadcode"
progname = "test_deadcode_elimination" progname = "test_deadcode_elimination"
) )
os.Mkdir(dirname, 0770) _ = os.Mkdir(dirname, 0770)
defer os.RemoveAll(dirname)
filename := filepath.Join(dirname, progname+".go") filename := filepath.Join(dirname, progname+".go")
err := os.WriteFile(filename, []byte(`package main err := os.WriteFile(filename, []byte(`package main
@ -262,11 +277,10 @@ func main() {
os.Exit(1) os.Exit(1)
} }
} }
`), 0660) `), 0600)
if err != nil { if err != nil {
t.Fatalf("could not write test program: %v", err) t.Fatalf("could not write test program: %v", err)
} }
defer os.RemoveAll(dirname)
buf, err := exec.Command("go", "build", filename).CombinedOutput() buf, err := exec.Command("go", "build", filename).CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("could not compile test program: %s", string(buf)) t.Fatalf("could not compile test program: %s", string(buf))

View file

@ -552,7 +552,8 @@ cmd.SetHelpFunc(f func(*Command, []string))
cmd.SetHelpTemplate(s string) cmd.SetHelpTemplate(s string)
``` ```
The latter two will also apply to any children commands. The latter two will also apply to any children commands. Templates specified with SetHelpTemplate are evaluated using
`text/template` which can increase the size of the compiled executable.
## Usage Message ## Usage Message