diff --git a/cli-plugins/hooks/template.go b/cli-plugins/hooks/template.go index eca5ce4f7227..d41c1c3f4540 100644 --- a/cli-plugins/hooks/template.go +++ b/cli-plugins/hooks/template.go @@ -40,7 +40,7 @@ func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) { } out = b.String() } - if n := strings.Count(out, "\n"); n > maxMessages { + if n := strings.Count(out, "\n") + 1; n > maxMessages { return nil, fmt.Errorf("hook template contains too many messages (%d): maximum is %d", n, maxMessages) } return strings.SplitN(out, "\n", maxMessages), nil diff --git a/cli-plugins/hooks/template_test.go b/cli-plugins/hooks/template_test.go index 28b290696f1d..982742c7d836 100644 --- a/cli-plugins/hooks/template_test.go +++ b/cli-plugins/hooks/template_test.go @@ -1,6 +1,7 @@ package hooks_test import ( + "strings" "testing" "github.com/docker/cli/cli-plugins/hooks" @@ -123,3 +124,10 @@ func TestParseTemplate(t *testing.T) { }) } } + +func TestParseTemplateTooManyMessages(t *testing.T) { + testCmd := &cobra.Command{Use: "pull"} + + _, err := hooks.ParseTemplate(strings.Repeat("line\n", 10)+"line", testCmd) + assert.Error(t, err, "hook template contains too many messages (11): maximum is 10") +}