Skip to content

Commit

Permalink
addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aarzilli committed Oct 15, 2024
1 parent 4da4d97 commit 84ffa30
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 17 additions & 3 deletions cobra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,29 @@ 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) {
// check that a simple program using cobra in its default configuration is
// linked with deadcode elimination enabled.
const (
dirname = "test_deadcode"
progname = "test_deadcode_elimination"
)
os.Mkdir(dirname, 0770)
_ = os.Mkdir(dirname, 0770)
defer os.RemoveAll(dirname)
filename := filepath.Join(dirname, progname+".go")
err := os.WriteFile(filename, []byte(`package main
Expand Down Expand Up @@ -262,11 +277,10 @@ func main() {
os.Exit(1)
}
}
`), 0660)
`), 0600)
if err != nil {
t.Fatalf("could not write test program: %v", err)
}
defer os.RemoveAll(dirname)
buf, err := exec.Command("go", "build", filename).CombinedOutput()
if err != nil {
t.Fatalf("could not compile test program: %s", string(buf))
Expand Down
3 changes: 2 additions & 1 deletion site/content/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ cmd.SetHelpFunc(f func(*Command, []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

Expand Down

0 comments on commit 84ffa30

Please sign in to comment.