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) +} diff --git a/pkg/helm/comment.go b/pkg/helm/comment.go index de93d0e..a32fe3b 100644 --- a/pkg/helm/comment.go +++ b/pkg/helm/comment.go @@ -11,7 +11,7 @@ func ParseComment(commentLines []string) (string, ChartValueDescription) { // Work around https://github.com/norwoodj/helm-docs/issues/96 by considering only // the last "group" of comment lines starting with '# --'. - lastIndex := 0 + lastIndex := -1 for i, v := range commentLines { if strings.HasPrefix(v, "# --") { lastIndex = i @@ -21,6 +21,10 @@ func ParseComment(commentLines []string) (string, ChartValueDescription) { // If there's a non-zero last index, consider that alone. return ParseComment(commentLines[lastIndex:]) } + if lastIndex == -1 { + // There are no lines starting with '# --', so we assume there's no documentation for the current field. + return "", ChartValueDescription{} + } for i := range commentLines { match := valuesDescriptionRegex.FindStringSubmatch(commentLines[i])