Skip to content

Commit

Permalink
Switch check_strings plugin to using the lexer
Browse files Browse the repository at this point in the history
Had to monkey patch the lexer a bit to differentiate between single quoted
and double quoted strings, so we might have issues here down the track
supporting multiple versions of Puppet (although hopefully the lexer doesn't
change that much).

Closes #22
Closes #24
  • Loading branch information
rodjek committed Sep 9, 2011
1 parent 60e6869 commit 111dd5d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
33 changes: 19 additions & 14 deletions lib/puppet-lint/plugins/check_strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,26 @@ def test(data)
token = tokens[token_idx]

if token.first == :STRING
contents = token.last[:value]
line_no = token.last[:line]
variable_found = false

contents.scan(/.\$./) do |w|
if w.start_with? '\\'
next
elsif w.end_with? '{'
variable_found = true
else
warn "variable not enclosed in {} on line #{line_no}"
warn "double quoted string containing no variables on line #{token.last[:line]}"
end

if token.first == :DQPRE and token.last[:value] == ""
if tokens[token_idx + 1].first == :VARIABLE
if tokens[token_idx + 2].first == :DQPOST and tokens[token_idx + 2].last[:value] == ""
warn "string containing only a variable on line #{tokens[token_idx + 1].last[:line]}"
end
end
unless variable_found
warn "double quoted string containing no variables on line #{line_no}"
end

if token.first == :DQPRE
end_of_string_idx = tokens[token_idx..-1].index { |r| r.first == :DQPOST }
tokens[token_idx..end_of_string_idx].each do |t|
if t.first == :VARIABLE
line = data.split("\n")[t.last[:line] - 1]
if line.is_a? String and line.include? "$#{t.last[:value]}"
warn "variable not enclosed in {} on line #{t.last[:line]}"
end
end
end
end

Expand All @@ -48,7 +53,7 @@ def test(data)
line_no = token.last[:line]

if contents.include? '${'
error "single quoted string containing a variable found on line #{line_no}"
error "single quoted string containing a variable found on line #{token.last[:line]}"
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions spec/puppet-lint/check_strings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,25 @@
its(:warnings) { should include "string containing only a variable on line 1" }
its(:errors) { should be_empty }
end

describe 'variable not enclosed in {}' do
let(:code) { '" $gronk"' }

its(:warnings) { should include "variable not enclosed in {} on line 1" }
its(:errors) { should be_empty }
end

describe 'double quoted string nested in a single quoted string' do
let(:code) { "'grep \"status=sent\" /var/log/mail.log'" }

its(:warnings) { should be_empty }
its(:errors) { should be_empty }
end

describe 'double quoted string after a comment' do
let(:code) { "service { 'foo': } # \"bar\"" }

its(:warnings) { should be_empty }
its(:errors) { should be_empty }
end
end

0 comments on commit 111dd5d

Please sign in to comment.