Skip to content

Commit

Permalink
Merge pull request #222 from ericzbeard/fix-211
Browse files Browse the repository at this point in the history
Fix format bug with greater than in multi-line string
  • Loading branch information
ericzbeard authored Dec 11, 2023
2 parents 1e406cd + ffdad7d commit a65c915
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cft/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func String(t cft.Template, opt Options) string {
}
}
trimmedRight := strings.TrimRight(part, " ")
if CheckMultilineBegin(trimmedRight) {
if !isMultiline && CheckMultilineBegin(trimmedRight) {
startMultilineIndent = indent
}

Expand Down
182 changes: 182 additions & 0 deletions cft/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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"
)

Expand Down Expand Up @@ -453,3 +454,184 @@ func TestFormatDefault(t *testing.T) {
checkMultilineBlockHeaders(t, correctMultilineBlockHeaders, true)
checkMultilineBlockHeaders(t, wrongMultilineBlockHeaders, false)
}

func TestFindInMap(t *testing.T) {
yaml := `
AWSTemplateFormatVersion: '2010-09-09'
Description: Reproduce "semantic difference after formatting" error
Parameters:
EnvironmentParam:
Default: dev
Type: String
AllowedValues:
- dev
- prod
Mappings:
EnvironmentMap:
MappedParam:
dev: my-dev-topic
prod: my-prod-topic
Resources:
Topic:
Type: AWS::SNS::Topic
Properties:
TopicName: !FindInMap [EnvironmentMap, MappedParam, !Ref EnvironmentParam]
`

expect := `AWSTemplateFormatVersion: "2010-09-09"
Description: Reproduce "semantic difference after formatting" error
Parameters:
EnvironmentParam:
Default: dev
Type: String
AllowedValues:
- dev
- prod
Mappings:
EnvironmentMap:
MappedParam:
dev: my-dev-topic
prod: my-prod-topic
Resources:
Topic:
Type: AWS::SNS::Topic
Properties:
TopicName: !FindInMap [EnvironmentMap, MappedParam, !Ref EnvironmentParam]
`

template, err := parse.String(yaml)
if err != nil {
t.Fatal(err)
}

actual := format.String(template, format.Options{
Unsorted: true,
})

if d := cmp.Diff(expect, actual); d != "" {
t.Fatalf(d)
}
}

func TestZipLines(t *testing.T) {
yaml := `
AWSTemplateFormatVersion: "2010-09-09"
Description: Example AWS CloudFormation template snippet.
Resources:
Test:
Type: AWS::Lambda::Function
Properties:
Role: arn:aws:iam::755952356119:role/lambda-basic
Runtime: python3.7
Handler: index.handler
Code:
ZipFile: |
import boto3
def handler:
"""Example."""
print('hello')
`

expect := `AWSTemplateFormatVersion: "2010-09-09"
Description: Example AWS CloudFormation template snippet.
Resources:
Test:
Type: AWS::Lambda::Function
Properties:
Role: arn:aws:iam::755952356119:role/lambda-basic
Runtime: python3.7
Handler: index.handler
Code:
ZipFile: "import boto3\n\ndef handler: \n\n \"\"\"Example.\"\"\"\n\n print('hello')\n"
`

template, err := parse.String(yaml)
if err != nil {
t.Fatal(err)
}

actual := format.String(template, format.Options{
Unsorted: true,
})

if d := cmp.Diff(expect, actual); d != "" {
t.Fatalf(d)
}
}

func TestMultiWithGT(t *testing.T) {
yaml := `
AWSTemplateFormatVersion: "2010-09-09"
Description: Example AWS CloudFormation template snippet.
Resources:
Test:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
"""Example."""
import boto3
breaks = """
>
"""
TEST = 1
`

expect := `AWSTemplateFormatVersion: "2010-09-09"
Description: Example AWS CloudFormation template snippet.
Resources:
Test:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
"""Example."""
import boto3
breaks = """
>
"""
TEST = 1
`

template, err := parse.String(yaml)
if err != nil {
t.Fatal(err)
}

actual := format.String(template, format.Options{
Unsorted: true,
})

config.Debug = true
config.Debugf(actual)

if d := cmp.Diff(expect, actual); d != "" {
t.Fatalf(d)
}
}
19 changes: 19 additions & 0 deletions test/templates/fmtmultiwithgt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
AWSTemplateFormatVersion: "2010-09-09"

Description: Example AWS CloudFormation template snippet.

Resources:
Test:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
"""Example."""
import boto3
breaks = """
>
"""
TEST = 1

0 comments on commit a65c915

Please sign in to comment.