From 8abd6f48f696a31971f393f0b29089af958d6167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Miguel=20Cust=C3=B3dio?= Date: Thu, 20 May 2021 17:33:27 +0100 Subject: [PATCH 1/2] Consider only the last group of comments starting with '# --'. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bruno Miguel Custódio --- pkg/document/values_test.go | 74 +++++++++++++++++++++++++++++++++++++ pkg/helm/comment.go | 20 +++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/pkg/document/values_test.go b/pkg/document/values_test.go index 98899fa..ce1839c 100644 --- a/pkg/document/values_test.go +++ b/pkg/document/values_test.go @@ -1299,3 +1299,77 @@ animals: assert.Equal(t, "", valuesRows[0].Default) assert.Equal(t, "I mean, dogs are quite nice too...", valuesRows[0].Description) } + +func TestMulticomment1(t *testing.T) { + helmValues := parseYamlValues(` +# -- before desc +before: 1 + +# -- commented desc +#commented: + +# -- after desc +after: 3 +`) + + valuesRows, err := getSortedValuesTableRows(helmValues, make(map[string]helm.ChartValueDescription)) + + assert.Nil(t, err) + assert.Len(t, valuesRows, 2) + + assert.Equal(t, "before", valuesRows[1].Key) + assert.Equal(t, intType, valuesRows[1].Type, intType) + assert.Equal(t, "`1`", valuesRows[1].Default) + assert.Equal(t, "", valuesRows[1].AutoDefault) + assert.Equal(t, "", valuesRows[1].Description) + assert.Equal(t, "before desc", valuesRows[1].AutoDescription) + + assert.Equal(t, "after", valuesRows[0].Key) + assert.Equal(t, intType, valuesRows[0].Type) + assert.Equal(t, "`3`", valuesRows[0].Default) + assert.Equal(t, "", valuesRows[0].AutoDefault) + assert.Equal(t, "", valuesRows[0].Description) + assert.Equal(t, "after desc", valuesRows[0].AutoDescription) +} + +func TestMulticomment2(t *testing.T) { + helmValues := parseYamlValues(` +# -- before desc +before: 1 + +# -- this should show up +hasInnerComment: {} + # -- this should not + # show up + # innerField: 1 + +# -- after desc +after: 3 +`) + + valuesRows, err := getSortedValuesTableRows(helmValues, make(map[string]helm.ChartValueDescription)) + + assert.Nil(t, err) + assert.Len(t, valuesRows, 3) + + assert.Equal(t, "before", valuesRows[1].Key) + assert.Equal(t, intType, valuesRows[1].Type, intType) + assert.Equal(t, "`1`", valuesRows[1].Default) + assert.Equal(t, "", valuesRows[1].AutoDefault) + assert.Equal(t, "", valuesRows[1].Description) + assert.Equal(t, "before desc", valuesRows[1].AutoDescription) + + assert.Equal(t, "hasInnerComment", valuesRows[2].Key) + assert.Equal(t, objectType, valuesRows[2].Type, intType) + assert.Equal(t, "`{}`", valuesRows[2].Default) + assert.Equal(t, "", valuesRows[2].AutoDefault) + assert.Equal(t, "", valuesRows[2].Description) + assert.Equal(t, "this should show up", valuesRows[2].AutoDescription) + + assert.Equal(t, "after", valuesRows[0].Key) + assert.Equal(t, intType, valuesRows[0].Type) + assert.Equal(t, "`3`", valuesRows[0].Default) + assert.Equal(t, "", valuesRows[0].AutoDefault) + assert.Equal(t, "", valuesRows[0].Description) + assert.Equal(t, "after desc", valuesRows[0].AutoDescription) +} diff --git a/pkg/helm/comment.go b/pkg/helm/comment.go index 2e45cad..de93d0e 100644 --- a/pkg/helm/comment.go +++ b/pkg/helm/comment.go @@ -1,10 +1,27 @@ package helm +import ( + "strings" +) + func ParseComment(commentLines []string) (string, ChartValueDescription) { var valueKey string var c ChartValueDescription var docStartIdx int + // Work around https://github.com/norwoodj/helm-docs/issues/96 by considering only + // the last "group" of comment lines starting with '# --'. + lastIndex := 0 + for i, v := range commentLines { + if strings.HasPrefix(v, "# --") { + lastIndex = i + } + } + if lastIndex > 0 { + // If there's a non-zero last index, consider that alone. + return ParseComment(commentLines[lastIndex:]) + } + for i := range commentLines { match := valuesDescriptionRegex.FindStringSubmatch(commentLines[i]) if len(match) < 3 { @@ -17,7 +34,7 @@ func ParseComment(commentLines []string) (string, ChartValueDescription) { break } - for _, line := range commentLines[docStartIdx+ 1:] { + for _, line := range commentLines[docStartIdx+1:] { defaultCommentMatch := defaultValueRegex.FindStringSubmatch(line) if len(defaultCommentMatch) > 1 { @@ -32,6 +49,5 @@ func ParseComment(commentLines []string) (string, ChartValueDescription) { continue } } - return valueKey, c } From 5ddabba9570907b451c624419a8d6d534574f450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Miguel=20Cust=C3=B3dio?= Date: Tue, 8 Jun 2021 08:57:42 +0100 Subject: [PATCH 2/2] Ignore comment nodes not containing '# --'. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bruno Miguel Custódio --- pkg/document/values.go | 3 +++ pkg/document/values_test.go | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/pkg/document/values.go b/pkg/document/values.go index 55a0ba7..90a76bf 100644 --- a/pkg/document/values.go +++ b/pkg/document/values.go @@ -128,6 +128,9 @@ func getDescriptionFromNode(node *yaml.Node) helm.ChartValueDescription { return helm.ChartValueDescription{} } + if !strings.Contains(node.HeadComment, "# --") { + return helm.ChartValueDescription{} + } commentLines := strings.Split(node.HeadComment, "\n") keyFromComment, c := helm.ParseComment(commentLines) if keyFromComment != "" { diff --git a/pkg/document/values_test.go b/pkg/document/values_test.go index ce1839c..6a6ef93 100644 --- a/pkg/document/values_test.go +++ b/pkg/document/values_test.go @@ -1373,3 +1373,45 @@ after: 3 assert.Equal(t, "", valuesRows[0].Description) assert.Equal(t, "after desc", valuesRows[0].AutoDescription) } + +func TestIgnoreCommentedFields(t *testing.T) { + helmValues := parseYamlValues(` +# -- Qux! +qux: 1 + +# must not appear +# must not appear too + +foo: + # -- Bar! + bar: true + # -- Baz! + baz: false +`) + + valuesRows, err := getSortedValuesTableRows(helmValues, make(map[string]helm.ChartValueDescription)) + + assert.Nil(t, err) + assert.Len(t, valuesRows, 3) + + assert.Equal(t, "qux", valuesRows[2].Key) + assert.Equal(t, intType, valuesRows[2].Type) + assert.Equal(t, "`1`", valuesRows[2].Default) + assert.Equal(t, "", valuesRows[2].AutoDefault) + assert.Equal(t, "", valuesRows[2].Description) + assert.Equal(t, "Qux!", valuesRows[2].AutoDescription) + + assert.Equal(t, "foo.baz", valuesRows[1].Key) + assert.Equal(t, boolType, valuesRows[1].Type) + assert.Equal(t, "`false`", valuesRows[1].Default) + assert.Equal(t, "", valuesRows[1].AutoDefault) + assert.Equal(t, "", valuesRows[1].Description) + assert.Equal(t, "Baz!", valuesRows[1].AutoDescription) + + assert.Equal(t, "foo.bar", valuesRows[0].Key) + assert.Equal(t, boolType, valuesRows[0].Type) + assert.Equal(t, "`true`", valuesRows[0].Default) + assert.Equal(t, "", valuesRows[0].AutoDefault) + assert.Equal(t, "", valuesRows[0].Description) + assert.Equal(t, "Bar!", valuesRows[0].AutoDescription) +}