Skip to content

Commit

Permalink
Merge #177
Browse files Browse the repository at this point in the history
177: Prevent panic with negative string sizes r=zegl a=zegl

Fixes #176 
Fixes #175

```
RELNOTE: Fixes for panics in the human output mode when the term size can not be detected, or was too small
```

Co-authored-by: Gustav Westling <[email protected]>
  • Loading branch information
bors[bot] and zegl authored Aug 20, 2019
2 parents 714217f + 38856b2 commit c108212
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
30 changes: 26 additions & 4 deletions cmd/kube-score/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ Use "-" as filename to read from STDIN.`)
w.WriteString(string(d))
r = w
} else if *outputFormat == "human" {
termWidth, _, _ := terminal.GetSize(int(os.Stdin.Fd()))
termWidth, _, err := terminal.GetSize(int(os.Stdin.Fd()))
// Assume a width of 80 if it can't be detected
if err != nil {
termWidth = 80
}
r = outputHuman(scoreCard, *verboseOutput, termWidth)
} else {
r = outputCi(scoreCard)
Expand Down Expand Up @@ -205,6 +209,20 @@ func listChecks() {
output.Flush()
}

func safeRepeat(s string, count int) string {
if count < 0 {
return ""
}
return strings.Repeat(s, count)
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func outputHuman(scoreCard *scorecard.Scorecard, verboseOutput int, termWidth int) io.Reader {
// Print the items sorted by scorecard key
var keys []string
Expand All @@ -226,8 +244,8 @@ func outputHuman(scoreCard *scorecard.Scorecard, verboseOutput int, termWidth in
writtenHeaderChars += written2
}

// Adjust to 80 columns wide
fmt.Fprintf(w, strings.Repeat(" ", 80-writtenHeaderChars-2))
// Adjust to termsize
fmt.Fprintf(w, safeRepeat(" ", min(80, termWidth)-writtenHeaderChars-2))

if scoredObject.AnyBelowOrEqualToGrade(scorecard.GradeCritical) {
fmt.Fprintf(w, "💥\n")
Expand Down Expand Up @@ -289,7 +307,11 @@ func outputHumanStep(card scorecard.TestScore, verboseOutput int, termWidth int)
fmt.Fprint(w, comment.Summary)

if len(comment.Description) > 0 {
wrapper := wordwrap.Wrapper(termWidth-12, false)
wrapWidth := termWidth - 12
if wrapWidth < 40 {
wrapWidth = 40
}
wrapper := wordwrap.Wrapper(wrapWidth, false)
wrapped := wrapper(comment.Description)
fmt.Fprintln(w)
fmt.Fprintf(w, wordwrap.Indent(wrapped, strings.Repeat(" ", 12), false))
Expand Down
68 changes: 68 additions & 0 deletions cmd/kube-score/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,71 @@ func TestHumanOutputLogDescription80Width(t *testing.T) {
nisl venenatis, elementum augue a, porttitor libero.
`, string(all))
}

func TestHumanOutputLogDescription0Width(t *testing.T) {
r := outputHuman(getTestCardLongDescription(), 0, 0)
all, err := ioutil.ReadAll(r)
assert.Nil(t, err)
assert.Equal(t, `v1/Testing foo in foofoo🤔
[WARNING] test-warning-two-comments
· a -> summary
Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Cras elementum sagittis
lacus, a dictum tortor lobortis vel.
Pellentesque habitant morbi tristique
senectus et netus et malesuada fames ac
turpis egestas. Nulla eu neque erat.
Vestibulum ante ipsum primis in faucibus
orci luctus et ultrices posuere cubilia
Curae; Maecenas et nisl venenatis,
elementum augue a, porttitor libero.
`, string(all))
}

func getTestCardLongTitle() *scorecard.Scorecard {
checks := []scorecard.TestScore{
{
Check: domain.Check{
Name: "test-warning-two-comments",
},
Grade: scorecard.GradeWarning,
Comments: []scorecard.TestScoreComment{
{
Path: "a",
Summary: "summary",
Description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras elementum sagittis lacus, a dictum tortor lobortis vel. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla eu neque erat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Maecenas et nisl venenatis, elementum augue a, porttitor libero.",
},
},
},
}

return &scorecard.Scorecard{
"a": &scorecard.ScoredObject{
TypeMeta: v1.TypeMeta{
Kind: "Testing",
APIVersion: "v1",
},
ObjectMeta: v1.ObjectMeta{
Name: "this-is-a-very-long-title-this-is-a-very-long-title-this-is-a-very-long-title-this-is-a-very-long-title-this-is-a-very-long-title",
Namespace: "foofoo",
},
Checks: checks,
},
}
}

func TestHumanOutputWithLongObjectNames(t *testing.T) {
r := outputHuman(getTestCardLongTitle(), 0, 80)
all, err := ioutil.ReadAll(r)
assert.Nil(t, err)
assert.Equal(t, `v1/Testing this-is-a-very-long-title-this-is-a-very-long-title-this-is-a-very-long-title-this-is-a-very-long-title-this-is-a-very-long-title in foofoo🤔
[WARNING] test-warning-two-comments
· a -> summary
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras
elementum sagittis lacus, a dictum tortor lobortis vel. Pellentesque
habitant morbi tristique senectus et netus et malesuada fames ac
turpis egestas. Nulla eu neque erat. Vestibulum ante ipsum primis in
faucibus orci luctus et ultrices posuere cubilia Curae; Maecenas et
nisl venenatis, elementum augue a, porttitor libero.
`, string(all))
}

0 comments on commit c108212

Please sign in to comment.