Skip to content

Commit

Permalink
Merge pull request #99 from bmcustodio/bmcustodio-fix-comments
Browse files Browse the repository at this point in the history
Consider only the last group of comments starting with '# --'.
  • Loading branch information
skang0601 authored Jan 18, 2022
2 parents 384770d + 5ddabba commit 1d44345
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/document/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,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 != "" {
Expand Down
116 changes: 116 additions & 0 deletions pkg/document/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1308,3 +1308,119 @@ 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)
}

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)
}
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 1d44345

Please sign in to comment.