From 70c729d6ca350581046c85bcb818f6a6f0fd7b60 Mon Sep 17 00:00:00 2001 From: rustyclock Date: Mon, 8 May 2023 19:43:59 +0900 Subject: [PATCH] Fix multiline comment parsing Signed-off-by: rustyclock --- editor/sink_attribute_get.go | 3 ++- editor/sink_attribute_get_test.go | 33 +++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/editor/sink_attribute_get.go b/editor/sink_attribute_get.go index c728f74..7ca282d 100644 --- a/editor/sink_attribute_get.go +++ b/editor/sink_attribute_get.go @@ -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) } diff --git a/editor/sink_attribute_get_test.go b/editor/sink_attribute_get_test.go index 4f65184..8dd052a 100644 --- a/editor/sink_attribute_get_test.go +++ b/editor/sink_attribute_get_test.go @@ -2,6 +2,8 @@ package editor import ( "testing" + + "github.com/google/go-cmp/cmp" ) func TestAttributeGetSink(t *testing.T) { @@ -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: ` @@ -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) } }) }