Merge pull request #71 from hashicorp/b-dos-line-endings
Fix scanner for Windows line endings and add tests
This commit is contained in:
commit
c40ec20b12
@ -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")
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/hcl/hcl/token"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var f100 = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
@ -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" {
|
||||
|
Loading…
Reference in New Issue
Block a user