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 scanner for Windows line endings and add tests #71

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