Remove extra newline when message contains trailing newlines. (#387)
This commit is contained in:
@@ -373,7 +373,7 @@ func (k *Kong) applyHookToDefaultFlags(ctx *Context, node *Node, name string) er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func formatMultilineMessage(w io.Writer, leaders []string, format string, args ...interface{}) {
|
func formatMultilineMessage(w io.Writer, leaders []string, format string, args ...interface{}) {
|
||||||
lines := strings.Split(fmt.Sprintf(format, args...), "\n")
|
lines := strings.Split(strings.TrimRight(fmt.Sprintf(format, args...), "\n"), "\n")
|
||||||
leader := ""
|
leader := ""
|
||||||
for _, l := range leaders {
|
for _, l := range leaders {
|
||||||
if l == "" {
|
if l == "" {
|
||||||
|
|||||||
+18
-5
@@ -593,11 +593,24 @@ func TestSliceWithDisabledSeparator(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMultilineMessage(t *testing.T) {
|
func TestMultilineMessage(t *testing.T) {
|
||||||
w := &bytes.Buffer{}
|
tests := []struct {
|
||||||
var cli struct{}
|
name string
|
||||||
p := mustNew(t, &cli, kong.Writers(w, w))
|
text string
|
||||||
p.Printf("hello\nworld")
|
want string
|
||||||
assert.Equal(t, "test: hello\n world\n", w.String())
|
}{
|
||||||
|
{"Simple", "hello\nworld", "test: hello\n world\n"},
|
||||||
|
{"WithNewline", "hello\nworld\n", "test: hello\n world\n"},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
w := &bytes.Buffer{}
|
||||||
|
var cli struct{}
|
||||||
|
p := mustNew(t, &cli, kong.Writers(w, w))
|
||||||
|
p.Printf(test.text)
|
||||||
|
assert.Equal(t, test.want, w.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type cmdWithRun struct {
|
type cmdWithRun struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user