Skip to content

Commit

Permalink
Merge pull request #10 from seanmil/fix_heredoc_detection
Browse files Browse the repository at this point in the history
Fix invalid handling of heredoc strings
  • Loading branch information
ekohl authored Sep 26, 2018
2 parents 0753217 + da087fe commit 04e4678
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/puppet-lint/plugins/check_trailing_comma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def check_elem(elem, except_type)
if lbo_token && lbo_token.type != except_type && \
elem[:tokens][-1].type != :SEMIC && \
lbo_token.type != :COMMA && \
lbo_token.type != :HEREDOC && \
lbo_token.next_token.type == :NEWLINE
notify :warning, {
:message => 'missing trailing comma after last element',
Expand All @@ -94,6 +95,17 @@ def check_elem(elem, except_type)
:token => lbo_token.next_token,
}
end
if lbo_token && lbo_token.type == :HEREDOC && \
lbo_token.prev_code_token.type != :COMMA && \
lbo_token.prev_code_token.next_token.type == :NEWLINE
notify :warning, {
:message => 'missing trailing comma after last element',
:line => lbo_token.prev_code_token.next_token.line,
:column => lbo_token.prev_code_token.next_token.column,
:token => lbo_token.prev_code_token.next_token,
}
end

end

def check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,48 @@ class { '::apache':
expect(problems).to contain_warning(msg).on_line(41).in_column(25)
end
end

context 'with heredoc' do
context 'with trailing comma' do
let(:code) {
<<-EOS
file { '/tmp/test.txt':
ensure => 'file',
content => @(EOT),
Hello
World
EOT
}
EOS
}

it 'should not detect any problems' do
expect(problems).to have(0).problems
end
end

context 'without trailing comma' do
let(:code) {
<<-EOS
file { '/tmp/test.txt':
ensure => 'file',
content => @(EOT)
Hello
World
EOT
}
EOS
}

it 'should detect a problem' do
expect(problems).to have(1).problems
end

it 'should create a warning' do
expect(problems).to contain_warning(msg).on_line(3).in_column(30)
end
end
end
end

context 'with fix enabled' do
Expand Down Expand Up @@ -337,5 +379,65 @@ class { '::apache':
)
end
end

context 'with heredoc' do
context 'with trailing comma' do
let(:code) {
<<-EOS
file { '/tmp/test.txt':
ensure => 'file',
content => @(EOT),
Hello
World
EOT
}
EOS
}

it 'should not detect any problems' do
expect(problems).to have(0).problems
end

it 'should not modify the manifest' do
expect(manifest).to eq(code)
end
end

context 'without trailing comma' do
let(:code) {
<<-EOS
file { '/tmp/test.txt':
ensure => 'file',
content => @(EOT)
Hello
World
EOT
}
EOS
}

it 'should detect a problem' do
expect(problems).to have(1).problems
end

it 'should create a warning' do
expect(problems).to contain_fixed(msg).on_line(3).in_column(30)
end

it 'should add trailing commas' do
expect(manifest).to eq(
<<-EOS
file { '/tmp/test.txt':
ensure => 'file',
content => @(EOT),
Hello
World
EOT
}
EOS
)
end
end
end
end
end

0 comments on commit 04e4678

Please sign in to comment.