Skip to content

Commit

Permalink
hclsyntax: test for invalid whitespace heredoc
Browse files Browse the repository at this point in the history
A heredoc cannot be introduced by a line ending in whitespace.

For example, the first line of a heredoc whose delimeter is `FOO`
can be either '<<FOO' or '<<-FOO', but not '<<FOO ' or '<<-FOO `,
even if the last line ends in `FOO `.
  • Loading branch information
kmoe committed Dec 2, 2021
1 parent e5ecb58 commit fd6e06b
Showing 1 changed file with 166 additions and 0 deletions.
166 changes: 166 additions & 0 deletions hclsyntax/scan_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,172 @@ EOF
},
},
},
{
`<<EOF
hello
EOF
`,
// `EOF ` is not a valid identifier
// so `<<EOF ` is not a valid TokenOHeredoc
[]Token{
{
Type: TokenLessThan,
Bytes: []byte("<"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 0, Line: 1, Column: 1},
End: hcl.Pos{Byte: 1, Line: 1, Column: 2},
},
},
{
Type: TokenLessThan,
Bytes: []byte("<"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 1, Line: 1, Column: 2},
End: hcl.Pos{Byte: 2, Line: 1, Column: 3},
},
},
{
Type: TokenIdent,
Bytes: []byte("EOF"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 2, Line: 1, Column: 3},
End: hcl.Pos{Byte: 5, Line: 1, Column: 6},
},
},
{
Type: TokenNewline,
Bytes: []byte("\n"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 6, Line: 1, Column: 7},
End: hcl.Pos{Byte: 7, Line: 2, Column: 1},
},
},
{
Type: TokenIdent,
Bytes: []byte("hello"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 7, Line: 2, Column: 1},
End: hcl.Pos{Byte: 12, Line: 2, Column: 6},
},
},
{
Type: TokenNewline,
Bytes: []byte("\n"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 12, Line: 2, Column: 6},
End: hcl.Pos{Byte: 13, Line: 3, Column: 1},
},
},
{
Type: TokenIdent,
Bytes: []byte("EOF"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 13, Line: 3, Column: 1},
End: hcl.Pos{Byte: 16, Line: 3, Column: 4},
},
},

{
Type: TokenNewline,
Bytes: []byte("\n"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 16, Line: 3, Column: 4},
End: hcl.Pos{Byte: 17, Line: 4, Column: 1},
},
},
{
Type: TokenEOF,
Bytes: []byte{},
Range: hcl.Range{
Start: hcl.Pos{Byte: 17, Line: 4, Column: 1},
End: hcl.Pos{Byte: 17, Line: 4, Column: 1},
},
},
},
},
{
`<<EOF
hello
EOF
`,
// `EOF ` is not a valid identifier
// so `<<EOF ` is not a valid TokenOHeredoc
[]Token{
{
Type: TokenLessThan,
Bytes: []byte("<"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 0, Line: 1, Column: 1},
End: hcl.Pos{Byte: 1, Line: 1, Column: 2},
},
},
{
Type: TokenLessThan,
Bytes: []byte("<"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 1, Line: 1, Column: 2},
End: hcl.Pos{Byte: 2, Line: 1, Column: 3},
},
},
{
Type: TokenIdent,
Bytes: []byte("EOF"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 2, Line: 1, Column: 3},
End: hcl.Pos{Byte: 5, Line: 1, Column: 6},
},
},
{
Type: TokenNewline,
Bytes: []byte("\n"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 6, Line: 1, Column: 7},
End: hcl.Pos{Byte: 7, Line: 2, Column: 1},
},
},
{
Type: TokenIdent,
Bytes: []byte("hello"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 7, Line: 2, Column: 1},
End: hcl.Pos{Byte: 12, Line: 2, Column: 6},
},
},
{
Type: TokenNewline,
Bytes: []byte("\n"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 12, Line: 2, Column: 6},
End: hcl.Pos{Byte: 13, Line: 3, Column: 1},
},
},
{
Type: TokenIdent,
Bytes: []byte("EOF"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 13, Line: 3, Column: 1},
End: hcl.Pos{Byte: 16, Line: 3, Column: 4},
},
},

{
Type: TokenNewline,
Bytes: []byte("\n"),
Range: hcl.Range{
Start: hcl.Pos{Byte: 17, Line: 3, Column: 5},
End: hcl.Pos{Byte: 18, Line: 4, Column: 1},
},
},
{
Type: TokenEOF,
Bytes: []byte{},
Range: hcl.Range{
Start: hcl.Pos{Byte: 18, Line: 4, Column: 1},
End: hcl.Pos{Byte: 18, Line: 4, Column: 1},
},
},
},
},

// Combinations
{
Expand Down

0 comments on commit fd6e06b

Please sign in to comment.