diff --git a/pkg/commands/help.go b/pkg/commands/help.go index 02a586f4c1d8..de4a9998fb8f 100644 --- a/pkg/commands/help.go +++ b/pkg/commands/help.go @@ -210,6 +210,12 @@ func printLinters(lcs []*linter.Config) { } func formatDescription(desc string) string { + desc = strings.TrimSpace(desc) + + if desc == "" { + return desc + } + // If the linter description spans multiple lines, truncate everything following the first newline endFirstLine := strings.IndexRune(desc, '\n') if endFirstLine > 0 { diff --git a/pkg/commands/help_test.go b/pkg/commands/help_test.go new file mode 100644 index 000000000000..eedc61c27c35 --- /dev/null +++ b/pkg/commands/help_test.go @@ -0,0 +1,51 @@ +package commands + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_formatDescription(t *testing.T) { + testCases := []struct { + desc string + doc string + expected string + }{ + { + desc: "empty description", + doc: "", + expected: "", + }, + { + desc: "simple description", + doc: "this is a test", + expected: "This is a test.", + }, + { + desc: "formatted description", + doc: "This is a test.", + expected: "This is a test.", + }, + { + desc: "multiline description", + doc: "this is a test\nanother line\n", + expected: "This is a test.", + }, + { + desc: "leading newline", + doc: "\nThis is a test.", + expected: "This is a test.", + }, + } + + for _, test := range testCases { + t.Run(test.desc, func(t *testing.T) { + t.Parallel() + + v := formatDescription(test.doc) + + assert.Equal(t, test.expected, v) + }) + } +}