From 591e82618ea071b6509467c21fcc5a590b9cdfde Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 19 Dec 2016 14:59:06 +0100 Subject: [PATCH] commands: fix opt.Description panic when desc was empty License: MIT Signed-off-by: Jakub Sztandera --- commands/option.go | 5 ++++- commands/option_test.go | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/commands/option.go b/commands/option.go index cf376e796e6..604c4e7fbf0 100644 --- a/commands/option.go +++ b/commands/option.go @@ -43,7 +43,10 @@ func (o *option) Type() reflect.Kind { } func (o *option) Description() string { - if o.description[len(o.description)-1] != '.' { + if len(o.description) == 0 { + return "" + } + if !strings.HasSuffix(o.description, ".") { o.description += "." } if o.defaultVal != nil { diff --git a/commands/option_test.go b/commands/option_test.go index 8a3d98f2300..99005cf4b99 100644 --- a/commands/option_test.go +++ b/commands/option_test.go @@ -1,6 +1,9 @@ package commands -import "testing" +import ( + "strings" + "testing" +) func TestOptionValueExtractBoolNotFound(t *testing.T) { t.Log("ensure that no error is returned when value is not found") @@ -27,3 +30,16 @@ func TestOptionValueExtractWrongType(t *testing.T) { t.Fatal("No error returned. Failure.") } } + +func TestLackOfDescriptionOfOptionDoesNotPanic(t *testing.T) { + opt := BoolOption("a", "") + opt.Description() +} + +func TestDotIsAddedInDescripton(t *testing.T) { + opt := BoolOption("a", "desc without dot") + dest := opt.Description() + if !strings.HasSuffix(dest, ".") { + t.Fatal("dot should have been added at the end of description") + } +}