Skip to content

Commit

Permalink
Merge pull request #71 from hashicorp/b-dos-line-endings
Browse files Browse the repository at this point in the history
Fix scanner for Windows line endings and add tests
  • Loading branch information
phinze committed Dec 1, 2015
2 parents cc4142e + 665d74b commit c40ec20
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hcl/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ func (s *Scanner) scanHeredoc() {
return
}

// Ignore the '\r' in Windows line endings
if ch == '\r' {
if s.peek() == '\n' {
ch = s.next()
}
}

// If we didn't reach a newline then that is also not good
if ch != '\n' {
s.err("invalid characters in heredoc anchor")
Expand Down
39 changes: 39 additions & 0 deletions hcl/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/hashicorp/hcl/hcl/token"
"strings"
)

var f100 = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
Expand Down Expand Up @@ -308,6 +309,44 @@ func TestFloat(t *testing.T) {
testTokenList(t, tokenLists["float"])
}

func TestWindowsLineEndings(t *testing.T) {
hcl := `// This should have Windows line endings
resource "aws_instance" "foo" {
user_data=<<HEREDOC
test script
HEREDOC
}`
hclWindowsEndings := strings.Replace(hcl, "\n", "\r\n", -1)

literals := []struct {
tokenType token.Type
literal string
}{
{token.COMMENT, "// This should have Windows line endings\r"},
{token.IDENT, `resource`},
{token.STRING, `"aws_instance"`},
{token.STRING, `"foo"`},
{token.LBRACE, `{`},
{token.IDENT, `user_data`},
{token.ASSIGN, `=`},
{token.HEREDOC, "<<HEREDOC\r\n test script\r\nHEREDOC\r\n"},
{token.RBRACE, `}`},
}

s := New([]byte(hclWindowsEndings))
for _, l := range literals {
tok := s.Scan()

if l.tokenType != tok.Type {
t.Errorf("got: %s want %s for %s\n", tok, l.tokenType, tok.String())
}

if l.literal != tok.Text {
t.Errorf("got:\n%v\nwant:\n%v\n", []byte(tok.Text), []byte(l.literal))
}
}
}

func TestRealExample(t *testing.T) {
complexHCL := `// This comes from Terraform, as a test
variable "foo" {
Expand Down

0 comments on commit c40ec20

Please sign in to comment.