Skip to content

Commit ace35f0

Browse files
authored
fix: avoid panic with plugin without description (#5312)
1 parent 09489d5 commit ace35f0

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Diff for: pkg/commands/help.go

+6
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ func printLinters(lcs []*linter.Config) {
210210
}
211211

212212
func formatDescription(desc string) string {
213+
desc = strings.TrimSpace(desc)
214+
215+
if desc == "" {
216+
return desc
217+
}
218+
213219
// If the linter description spans multiple lines, truncate everything following the first newline
214220
endFirstLine := strings.IndexRune(desc, '\n')
215221
if endFirstLine > 0 {

Diff for: pkg/commands/help_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package commands
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_formatDescription(t *testing.T) {
10+
testCases := []struct {
11+
desc string
12+
doc string
13+
expected string
14+
}{
15+
{
16+
desc: "empty description",
17+
doc: "",
18+
expected: "",
19+
},
20+
{
21+
desc: "simple description",
22+
doc: "this is a test",
23+
expected: "This is a test.",
24+
},
25+
{
26+
desc: "formatted description",
27+
doc: "This is a test.",
28+
expected: "This is a test.",
29+
},
30+
{
31+
desc: "multiline description",
32+
doc: "this is a test\nanother line\n",
33+
expected: "This is a test.",
34+
},
35+
{
36+
desc: "leading newline",
37+
doc: "\nThis is a test.",
38+
expected: "This is a test.",
39+
},
40+
}
41+
42+
for _, test := range testCases {
43+
t.Run(test.desc, func(t *testing.T) {
44+
t.Parallel()
45+
46+
v := formatDescription(test.doc)
47+
48+
assert.Equal(t, test.expected, v)
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)