Skip to content

Commit edc03ca

Browse files
committed
Fix CRLF line endings causing extra blank lines between comments
1 parent fabc4bd commit edc03ca

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

cft/format/crlf_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package format_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/aws-cloudformation/rain/cft/format"
7+
"github.com/aws-cloudformation/rain/cft/parse"
8+
)
9+
10+
func TestCRLFComments(t *testing.T) {
11+
// Issue #479: Windows CRLF line endings cause extra blank lines between comments
12+
// Build input with CRLF line endings
13+
input := "AWSTemplateFormatVersion: 2010-09-09\r\n" +
14+
"\r\n" +
15+
"# Comment line 1\r\n" +
16+
"# Comment line 2\r\n" +
17+
"# Comment line 3\r\n" +
18+
"\r\n" +
19+
"Description: Hello World\r\n"
20+
21+
// Same input with LF line endings for comparison
22+
inputLF := "AWSTemplateFormatVersion: 2010-09-09\n" +
23+
"\n" +
24+
"# Comment line 1\n" +
25+
"# Comment line 2\n" +
26+
"# Comment line 3\n" +
27+
"\n" +
28+
"Description: Hello World\n"
29+
30+
tmplCRLF, err := parse.String(input)
31+
if err != nil {
32+
t.Fatalf("Failed to parse CRLF input: %v", err)
33+
}
34+
35+
tmplLF, err := parse.String(inputLF)
36+
if err != nil {
37+
t.Fatalf("Failed to parse LF input: %v", err)
38+
}
39+
40+
outputCRLF := format.String(tmplCRLF, format.Options{})
41+
outputLF := format.String(tmplLF, format.Options{})
42+
43+
t.Logf("CRLF output:\n---\n%s\n---", outputCRLF)
44+
t.Logf("LF output:\n---\n%s\n---", outputLF)
45+
46+
if outputCRLF != outputLF {
47+
t.Errorf("CRLF and LF outputs differ.\nCRLF output:\n%s\nLF output:\n%s", outputCRLF, outputLF)
48+
}
49+
}

cft/parse/parse.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func Map(input map[string]interface{}) (*cft.Template, error) {
5858

5959
// String returns a cft.Template parsed from a string
6060
func String(input string) (*cft.Template, error) {
61+
input = strings.ReplaceAll(input, "\r", "")
6162
var n yaml.Node
6263
err := yaml.Unmarshal([]byte(input), &n)
6364
if err != nil {

0 commit comments

Comments
 (0)