Skip to content

Commit

Permalink
Fix multiline comment parsing
Browse files Browse the repository at this point in the history
Signed-off-by: rustyclock <[email protected]>
  • Loading branch information
rustycl0ck committed May 8, 2023
1 parent 1b1e5c2 commit 70c729d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion editor/sink_attribute_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ func GetAttributeValueAsString(attr *hclwrite.Attribute) (string, error) {
var valueTokens hclwrite.Tokens
for _, t := range exprTokens {
if t.Type == hclsyntax.TokenComment {
break
t.Bytes = []byte("\n")
t.SpacesBefore = 0
}
valueTokens = append(valueTokens, t)
}
Expand Down
33 changes: 31 additions & 2 deletions editor/sink_attribute_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package editor

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestAttributeGetSink(t *testing.T) {
Expand Down Expand Up @@ -52,6 +54,33 @@ a1 = v1
ok: true,
want: "v0\n",
},
{
name: "multiline attribute with comments",
src: `
// attr comment
a0 = v0
a1 = [
"val1",
"val2", // inline comment
"val3", # another comment
"val4",
# a ocmment line
"val5",
]
a2 = v2
`,
address: "a1",
ok: true,
want: `[
"val1",
"val2",
"val3",
"val4",
"val5",
]
`,
},
{
name: "duplicated attributes should be error",
src: `
Expand Down Expand Up @@ -209,8 +238,8 @@ b1 "l1" "l2" {
t.Fatalf("expected to return an error, but no error, outStream: \n%s", got)
}

if got != tc.want {
t.Fatalf("got:\n%s\nwant:\n%s", got, tc.want)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatalf("got:\n%s\nwant:\n%s\ndiff(-want +got):\n%v", got, tc.want, diff)
}
})
}
Expand Down

0 comments on commit 70c729d

Please sign in to comment.