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

Fix format bug with greater than in multi-line string #222

Merged
merged 1 commit into from
Dec 11, 2023
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
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
Loading