From f38f80a108071b4e26166d0725812a372a063a04 Mon Sep 17 00:00:00 2001 From: Eric Beard Date: Mon, 4 Mar 2024 15:17:25 -0800 Subject: [PATCH 1/2] empty string and null are the same --- cft/diff/compare.go | 6 +++++ cft/format/format.go | 2 -- cft/format/format_test.go | 48 +++++++++++++++++++++++++++++++++ cft/format/transform.go | 2 ++ test/templates/getazs-null.yaml | 13 +++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 test/templates/getazs-null.yaml diff --git a/cft/diff/compare.go b/cft/diff/compare.go index 9526947f..3e3b16ec 100644 --- a/cft/diff/compare.go +++ b/cft/diff/compare.go @@ -14,6 +14,12 @@ func New(a, b cft.Template) Diff { func compareValues(old, new interface{}) Diff { if reflect.TypeOf(old) != reflect.TypeOf(new) { + + // In YAML there is no difference between "" and null + if old == "" && new == nil { + return value{new, Unchanged} + } + return value{new, Changed} } diff --git a/cft/format/format.go b/cft/format/format.go index 213ca644..4b045ef9 100644 --- a/cft/format/format.go +++ b/cft/format/format.go @@ -33,8 +33,6 @@ func CheckMultilineBegin(s string) bool { func String(t cft.Template, opt Options) string { node := t.Node - // config.Debugf("%v", rnode.ToSJson(node)) - buf := strings.Builder{} enc := yaml.NewEncoder(&buf) enc.SetIndent(2) diff --git a/cft/format/format_test.go b/cft/format/format_test.go index c59d1919..e49065d4 100644 --- a/cft/format/format_test.go +++ b/cft/format/format_test.go @@ -7,6 +7,7 @@ import ( "github.com/aws-cloudformation/rain/cft/format" "github.com/aws-cloudformation/rain/cft/parse" + "github.com/aws-cloudformation/rain/internal/config" "github.com/google/go-cmp/cmp" ) @@ -717,3 +718,50 @@ Parameters: } } + +func TestFnGetAZs(t *testing.T) { + input := ` +Resources: + PrivateSubnet1Subnet: + Type: AWS::EC2::Subnet + Properties: + AvailabilityZone: !Select + - 0 + - !GetAZs "" +` + + // Parse the template + source, err := parse.String(string(input)) + if err != nil { + t.Fatal(err) + } + + config.Debug = true + + output := format.String(source, format.Options{ + JSON: false, + Unsorted: true, + }) + + config.Debugf("TestFnGetAZs output: %s", output) + + // Verify the output is valid + if err = parse.Verify(source, output); err != nil { + t.Fatal(err) + } + + expected := ` +Resources: + PrivateSubnet1Subnet: + Type: AWS::EC2::Subnet + Properties: + AvailabilityZone: !Select + - 0 + - !GetAZs +` + + if strings.TrimSpace(output) != strings.TrimSpace(expected) { + t.Fatalf("Got:\n%s\n\nExpected:\n%s", output, expected) + } + +} diff --git a/cft/format/transform.go b/cft/format/transform.go index 433b32f1..9bc5cb15 100644 --- a/cft/format/transform.go +++ b/cft/format/transform.go @@ -28,6 +28,7 @@ func formatNode(n *yaml.Node) *yaml.Node { if n.Kind == yaml.MappingNode { // Does it have just one key/value pair? if len(n.Content) == 2 { + // Is the key relevant? for tag, funcName := range cft.Tags { if n.Content[0].Value == funcName { @@ -85,6 +86,7 @@ func formatNode(n *yaml.Node) *yaml.Node { n.Style = newNode.Content[0].Style } } + } } diff --git a/test/templates/getazs-null.yaml b/test/templates/getazs-null.yaml new file mode 100644 index 00000000..76f7c781 --- /dev/null +++ b/test/templates/getazs-null.yaml @@ -0,0 +1,13 @@ +Parameters: + VpcId: + Type: String + +Resources: + + PrivateSubnet1Subnet: + Type: AWS::EC2::Subnet + Properties: + AvailabilityZone: !Select + - 0 + - !GetAZs "" + VpcId: !Ref VpcId From 3ffd1ab6b7361925696e51b556281106c7d38eed Mon Sep 17 00:00:00 2001 From: Eric Beard Date: Mon, 4 Mar 2024 15:20:17 -0800 Subject: [PATCH 2/2] Remove debugs --- cft/format/format_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cft/format/format_test.go b/cft/format/format_test.go index e49065d4..ef15537a 100644 --- a/cft/format/format_test.go +++ b/cft/format/format_test.go @@ -7,7 +7,6 @@ import ( "github.com/aws-cloudformation/rain/cft/format" "github.com/aws-cloudformation/rain/cft/parse" - "github.com/aws-cloudformation/rain/internal/config" "github.com/google/go-cmp/cmp" ) @@ -736,15 +735,11 @@ Resources: t.Fatal(err) } - config.Debug = true - output := format.String(source, format.Options{ JSON: false, Unsorted: true, }) - config.Debugf("TestFnGetAZs output: %s", output) - // Verify the output is valid if err = parse.Verify(source, output); err != nil { t.Fatal(err)