Skip to content

Commit

Permalink
Consider only the last group of comments starting with '# --'.
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Miguel Custódio <brunomcustodio@gmail.com>
  • Loading branch information
bmcustodio committed May 20, 2021
1 parent aaec4ec commit 8abd6f4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
74 changes: 74 additions & 0 deletions pkg/document/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
20 changes: 18 additions & 2 deletions pkg/helm/comment.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand All @@ -32,6 +49,5 @@ func ParseComment(commentLines []string) (string, ChartValueDescription) {
continue
}
}

return valueKey, c
}

0 comments on commit 8abd6f4

Please sign in to comment.