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 }