Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider only the last group of comments starting with '# --'. #99

Merged
merged 2 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/document/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
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 @@ -1299,3 +1299,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
}