puppet-lint 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Puppet-lint
2
2
 
3
3
  The goal of this project is to implement as many of the recommended Puppet
4
- style guidelines from the [Puppetlabs style
4
+ style guidelines from the [Puppet Labs style
5
5
  guide](http://docs.puppetlabs.com/guides/style_guide.html) as practical.
6
6
 
7
7
  ## Installation
@@ -67,7 +67,7 @@ At the moment, the following tests have been implemented:
67
67
  * Classes should not inherit between namespaces.
68
68
  * Required parameters in class & defined type definitions should be listed
69
69
  before optional parameters.
70
- # When using top-scope variables, including facts, Puppet modules should
70
+ * When using top-scope variables, including facts, Puppet modules should
71
71
  explicitly specify the empty namespace.
72
72
 
73
73
  ## Reporting bugs or incorrect results
data/lib/puppet-lint.rb CHANGED
@@ -5,7 +5,7 @@ require 'puppet'
5
5
  class PuppetLint::NoCodeError < StandardError; end
6
6
 
7
7
  class PuppetLint
8
- VERSION = '0.1.1'
8
+ VERSION = '0.1.2'
9
9
 
10
10
  attr_reader :code, :file
11
11
 
@@ -1,40 +1,61 @@
1
+ require 'puppet'
2
+
1
3
  class PuppetLint::Plugins::CheckStrings < PuppetLint::CheckPlugin
4
+ class ::Puppet::Parser::Lexer
5
+ class TokenList
6
+ def del_token(token)
7
+ @tokens.delete(token)
8
+ end
9
+ end
10
+
11
+ TOKENS.add_tokens("<single quotes string>" => :SSTRING)
12
+ TOKENS.del_token(:SQUOTE)
13
+
14
+ TOKENS.add_token :SQUOTE, "'" do |lexer, value|
15
+ [TOKENS[:SSTRING], lexer.slurpstring(value,["'"],:ignore_invalid_escapes).first ]
16
+ end
17
+ end
18
+
2
19
  def test(data)
3
- line_no = 0
4
- data.each_line do |line|
5
- line_no += 1
6
- line.match(/"([^\\"]|\\\\|\\")*"/).to_a.each do |s|
7
- if s.is_a? String and s.start_with? '"'
8
- variable_found = false
9
- s.scan(/.\$./) do |w|
10
- if w.start_with? '\\'
11
- next
12
- elsif w.end_with? '{'
13
- variable_found = true
14
- else
15
- warn "variable not enclosed in {} on line #{line_no}"
16
- end
17
- end
18
- unless variable_found
19
- warn "double quoted string containing no variables on line #{line_no}"
20
- end
21
- if s =~ /^"\$\{[\w\:]+\}"$/
22
- warn "string containing only a variable on line #{line_no}"
23
- end
20
+ l = Puppet::Parser::Lexer.new
21
+ l.string = data
22
+ tokens = l.fullscan
23
+
24
+ tokens.each_index do |token_idx|
25
+ token = tokens[token_idx]
26
+
27
+ if token.first == :STRING
28
+ warn "double quoted string containing no variables on line #{token.last[:line]}"
29
+ end
24
30
 
25
- line = line[line.index('"', line.index('"')+1)..-1]
31
+ if token.first == :DQPRE and token.last[:value] == ""
32
+ if tokens[token_idx + 1].first == :VARIABLE
33
+ if tokens[token_idx + 2].first == :DQPOST and tokens[token_idx + 2].last[:value] == ""
34
+ warn "string containing only a variable on line #{tokens[token_idx + 1].last[:line]}"
35
+ end
26
36
  end
27
37
  end
28
38
 
29
- line.match(/'.+?'/).to_a.each do |s|
30
- if s.start_with? "'"
31
- s.scan(/\$./) do |w|
32
- if w.end_with? '{'
33
- error "single quoted string containing a variable found on line #{line_no}"
39
+ if token.first == :DQPRE
40
+ end_of_string_idx = tokens[token_idx..-1].index { |r| r.first == :DQPOST }
41
+ tokens[token_idx..end_of_string_idx].each do |t|
42
+ if t.first == :VARIABLE
43
+ line = data.split("\n")[t.last[:line] - 1]
44
+ if line.is_a? String and line.include? "$#{t.last[:value]}"
45
+ warn "variable not enclosed in {} on line #{t.last[:line]}"
34
46
  end
35
47
  end
36
48
  end
37
49
  end
50
+
51
+ if token.first == :SSTRING
52
+ contents = token.last[:value]
53
+ line_no = token.last[:line]
54
+
55
+ if contents.include? '${'
56
+ error "single quoted string containing a variable found on line #{token.last[:line]}"
57
+ end
58
+ end
38
59
  end
39
60
  end
40
61
  end
data/puppet-lint.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'puppet-lint'
3
- s.version = '0.1.1'
3
+ s.version = '0.1.2'
4
4
  s.homepage = 'https://github.com/rodjek/puppet-lint/'
5
5
  s.summary = 'Ensure your Puppet manifests conform with the Puppetlabs style guide'
6
6
  s.description = 'Checks your Puppet manifests against the Puppetlabs
@@ -27,4 +27,25 @@ describe PuppetLint::Plugins::CheckStrings do
27
27
  its(:warnings) { should include "string containing only a variable on line 1" }
28
28
  its(:errors) { should be_empty }
29
29
  end
30
+
31
+ describe 'variable not enclosed in {}' do
32
+ let(:code) { '" $gronk"' }
33
+
34
+ its(:warnings) { should include "variable not enclosed in {} on line 1" }
35
+ its(:errors) { should be_empty }
36
+ end
37
+
38
+ describe 'double quoted string nested in a single quoted string' do
39
+ let(:code) { "'grep \"status=sent\" /var/log/mail.log'" }
40
+
41
+ its(:warnings) { should be_empty }
42
+ its(:errors) { should be_empty }
43
+ end
44
+
45
+ describe 'double quoted string after a comment' do
46
+ let(:code) { "service { 'foo': } # \"bar\"" }
47
+
48
+ its(:warnings) { should be_empty }
49
+ its(:errors) { should be_empty }
50
+ end
30
51
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tim Sharpe
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-07 00:00:00 Z
18
+ date: 2011-09-09 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec