Skip to content

Commit a7f368f

Browse files
committed
cli-plugins/hooks: fix max-message off-by-one
1 parent 9f16882 commit a7f368f

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

cli-plugins/hooks/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) {
4040
}
4141
out = b.String()
4242
}
43-
if n := strings.Count(out, "\n"); n > maxMessages {
43+
if n := strings.Count(out, "\n") + 1; n > maxMessages {
4444
return nil, fmt.Errorf("hook template contains too many messages (%d): maximum is %d", n, maxMessages)
4545
}
4646
return strings.SplitN(out, "\n", maxMessages), nil

cli-plugins/hooks/template_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hooks_test
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/docker/cli/cli-plugins/hooks"
@@ -123,3 +124,10 @@ func TestParseTemplate(t *testing.T) {
123124
})
124125
}
125126
}
127+
128+
func TestParseTemplateTooManyMessages(t *testing.T) {
129+
testCmd := &cobra.Command{Use: "pull"}
130+
131+
_, err := hooks.ParseTemplate(strings.Repeat("line\n", 10)+"line", testCmd)
132+
assert.Error(t, err, "hook template contains too many messages (11): maximum is 10")
133+
}

0 commit comments

Comments
 (0)