puppet-syntax 0.0.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,15 @@
1
+ ---
1
2
  language: ruby
3
+ script: bundle exec rake
2
4
  rvm:
5
+ - 1.8.7
3
6
  - 1.9.3
7
+ env:
8
+ - PUPPET_VERSION="~> 2.7.0"
9
+ - PUPPET_VERSION="~> 3.0.0"
10
+ - PUPPET_VERSION="~> 3.1.0"
11
+ - PUPPET_VERSION="~> 3.2.0"
12
+ - PUPPET_VERSION=">= 0"
13
+ matrix:
14
+ allow_failures:
15
+ - env: PUPPET_VERSION=">= 0"
@@ -0,0 +1,21 @@
1
+ 2013-07-04 Release 1.0.0
2
+ - Refactor code to make it easier to test.
3
+ - Implement spec tests for syntax checks.
4
+ - Pending spec tests for FileList matching.
5
+ - Matrix tests for other Ruby/Puppet versions.
6
+ - Improve usage example in README.
7
+
8
+ 2013-06-14 Release 0.0.4
9
+ - Fix `$confdir` error for Puppet 3.x
10
+
11
+ 2013-06-11 Release 0.0.3
12
+ - List rake as a dependency.
13
+ - Output names of tasks to STDERR.
14
+ - Match template paths correctly.
15
+ - Add pending spec tests, not yet working.
16
+
17
+ 2013-06-10 Release 0.0.2
18
+ - Fix namespacing of rake tasks.
19
+
20
+ 2013-06-10 Release 0.0.1
21
+ - Initial release
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in puppet-syntax.gemspec
4
4
  gemspec
5
+
6
+ # Override gemspec for CI matrix builds.
7
+ if ENV['PUPPET_VERSION']
8
+ gem 'puppet', ENV['PUPPET_VERSION']
9
+ end
data/README.md CHANGED
@@ -8,6 +8,23 @@ Include the following in your `Rakefile`:
8
8
 
9
9
  require 'puppet-syntax/tasks/puppet-syntax'
10
10
 
11
+ Test all manifests and templates relative to your `Rakefile`:
12
+
13
+ ➜ puppet git:(master) bundle exec rake syntax
14
+ ---> syntax:manifests
15
+ ---> syntax:templates
16
+
17
+ A non-zero exit code and error message will be returned for any failures:
18
+
19
+ ➜ puppet git:(master) bundle exec rake syntax
20
+ ---> syntax:manifests
21
+ rake aborted!
22
+ Could not parse for environment production: Syntax error at end of file at demo.pp:2
23
+ Tasks: TOP => syntax => syntax:manifests
24
+ (See full trace by running task with --trace)
25
+
26
+ ## Configuration
27
+
11
28
  Paths can be excluded with:
12
29
 
13
30
  PuppetSyntax.exclude_paths = ["vendor/**/*"]
@@ -1,14 +1,11 @@
1
- require 'rake'
2
1
  require 'puppet'
3
2
  require 'puppet/face'
4
3
 
5
4
  module PuppetSyntax
6
5
  class Manifests
7
- def validate_manifest(file)
8
- Puppet::Face[:parser, '0.0.1'].validate(file)
9
- end
6
+ def check(filelist)
7
+ raise "Expected an array of files" unless filelist.is_a?(Array)
10
8
 
11
- def check
12
9
  errors = []
13
10
 
14
11
  # FIXME: We shouldn't need to do this. puppet/face should. See:
@@ -22,8 +19,7 @@ module PuppetSyntax
22
19
  Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(errors))
23
20
  Puppet::Util::Log.level = :warning
24
21
 
25
- matched_files = FileList["**/*.pp"].exclude(*PuppetSyntax.exclude_paths)
26
- matched_files.each do |puppet_file|
22
+ filelist.each do |puppet_file|
27
23
  begin
28
24
  validate_manifest(puppet_file)
29
25
  rescue => error
@@ -31,13 +27,20 @@ module PuppetSyntax
31
27
  end
32
28
  end
33
29
 
34
- # Exported resources will raise warnings when outside a puppetmaster.
35
30
  Puppet::Util::Log.close_all
31
+ errors.map! { |e| e.to_s }
32
+
33
+ # Exported resources will raise warnings when outside a puppetmaster.
36
34
  errors.reject! { |e|
37
- e.to_s =~ /^You cannot collect( exported resources)? without storeconfigs being set/
35
+ e =~ /^You cannot collect( exported resources)? without storeconfigs being set/
38
36
  }
39
37
 
40
38
  errors
41
39
  end
40
+
41
+ private
42
+ def validate_manifest(file)
43
+ Puppet::Face[:parser, '0.0.1'].validate(file)
44
+ end
42
45
  end
43
46
  end
@@ -15,16 +15,24 @@ module PuppetSyntax
15
15
  desc 'Syntax check Puppet manifests'
16
16
  task :manifests do |t|
17
17
  $stderr.puts "---> #{t.name}"
18
+ files = FileList["**/*.pp"]
19
+ files.reject! { |f| File.directory?(f) }
20
+ files = files.exclude(*PuppetSyntax.exclude_paths)
21
+
18
22
  c = PuppetSyntax::Manifests.new
19
- errors = c.check
23
+ errors = c.check(files)
20
24
  fail errors.join("\n") unless errors.empty?
21
25
  end
22
26
 
23
27
  desc 'Syntax check Puppet templates'
24
28
  task :templates do |t|
25
29
  $stderr.puts "---> #{t.name}"
30
+ files = FileList["**/templates/**/*"]
31
+ files.reject! { |f| File.directory?(f) }
32
+ files = files.exclude(*PuppetSyntax.exclude_paths)
33
+
26
34
  c = PuppetSyntax::Templates.new
27
- errors = c.check
35
+ errors = c.check(files)
28
36
  fail errors.join("\n") unless errors.empty?
29
37
  end
30
38
  end
@@ -1,18 +1,16 @@
1
- require 'rake'
2
1
  require 'erb'
3
2
  require 'stringio'
4
3
 
5
4
  module PuppetSyntax
6
5
  class Templates
7
- def check
6
+ def check(filelist)
7
+ raise "Expected an array of files" unless filelist.is_a?(Array)
8
+
8
9
  # We now have to redirect STDERR in order to capture warnings.
9
10
  $stderr = warnings = StringIO.new()
10
11
  errors = []
11
12
 
12
- # Templates don't have to have a .erb extension
13
- matched_files = FileList["**/templates/**/*"].exclude(*PuppetSyntax.exclude_paths)
14
- matched_files.reject! { |f| File.directory?(f) }
15
- matched_files.each do |erb_file|
13
+ filelist.each do |erb_file|
16
14
  begin
17
15
  erb = ERB.new(File.read(erb_file), nil, '-')
18
16
  erb.filename = erb_file
@@ -20,13 +18,15 @@ module PuppetSyntax
20
18
  rescue NameError
21
19
  # This is normal because we don't have the variables that would
22
20
  # ordinarily be bound by the parent Puppet manifest.
23
- rescue StandardError, SyntaxError => error
21
+ rescue SyntaxError => error
24
22
  errors << error
25
23
  end
26
24
  end
27
25
 
28
26
  $stderr = STDERR
29
27
  errors << warnings.string unless warnings.string.empty?
28
+ errors.map! { |e| e.to_s }
29
+
30
30
  errors
31
31
  end
32
32
  end
@@ -1,3 +1,3 @@
1
1
  module PuppetSyntax
2
- VERSION = "0.0.4"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,3 @@
1
+ class failing_puppet_module {
2
+ this is not a valid resource name
3
+ }
@@ -0,0 +1,5 @@
1
+ class warning_puppet_module {
2
+ notify { 'this should raise a warning':
3
+ message => "because of \[\] escape characters",
4
+ }
5
+ }
@@ -0,0 +1,5 @@
1
+ class normal_puppet_module {
2
+ notify { 'should should pass':
3
+ message => 'with flying colours',
4
+ }
5
+ }
@@ -0,0 +1,3 @@
1
+ class uses_storeconfigs {
2
+ @@notify { 'exported': }
3
+ }
@@ -0,0 +1,3 @@
1
+ This is plain text
2
+ <% This is not valid Ruby %>
3
+ This is also plain text
@@ -0,0 +1,2 @@
1
+ Assignment in condition should warn.
2
+ <%= "oh yes" if foo = true %>
@@ -0,0 +1,2 @@
1
+ <% valid = 'valid' -%>
2
+ This is a <%= valid -%> template.
@@ -0,0 +1 @@
1
+ <%= this_variable_has_not_been_bound %>
@@ -1,29 +1,57 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe PuppetSyntax::Manifests do
4
+ let(:subject) { PuppetSyntax::Manifests.new }
5
+
6
+ it 'should expect an array of files' do
7
+ expect { subject.check(nil) }.to raise_error(/Expected an array of files/)
8
+ end
9
+
4
10
  it 'should return nothing from a valid file' do
5
- pending
11
+ files = fixture_manifests('pass.pp')
12
+ res = subject.check(files)
13
+
14
+ res.should == []
6
15
  end
7
16
 
8
17
  it 'should return an error from an invalid file' do
9
- pending
18
+ files = fixture_manifests('fail_error.pp')
19
+ res = subject.check(files)
20
+
21
+ res.should have(1).items
22
+ res.first.should match(/Syntax error at '\}' .*:3$/)
10
23
  end
11
24
 
12
25
  it 'should return a warning from an invalid file' do
13
- # "Unrecognised escape sequence \[\]"
14
- pending
26
+ files = fixture_manifests('fail_warning.pp')
27
+ res = subject.check(files)
28
+
29
+ res.should have(2).items
30
+ res[0].should match(/Unrecognised escape sequence '\\\[' .* at line 3$/)
31
+ res[1].should match(/Unrecognised escape sequence '\\\]' .* at line 3$/)
15
32
  end
16
33
 
17
34
  it 'should ignore warnings about storeconfigs' do
18
- # @@notify { 'foo': }
19
- pending
35
+ files = fixture_manifests('pass_storeconfigs.pp')
36
+ res = subject.check(files)
37
+
38
+ res.should == []
20
39
  end
21
40
 
22
41
  it 'should read more than one valid file' do
23
- pending
42
+ files = fixture_manifests(['pass.pp', 'pass_storeconfigs.pp'])
43
+ res = subject.check(files)
44
+
45
+ res.should == []
24
46
  end
25
47
 
26
48
  it 'should continue after finding an error in the first file' do
27
- pending
49
+ files = fixture_manifests(['fail_error.pp', 'fail_warning.pp'])
50
+ res = subject.check(files)
51
+
52
+ res.should have(3).items
53
+ res[0].should match(/Syntax error at '\}' .*:3$/)
54
+ res[1].should match(/Unrecognised escape sequence '\\\[' .* at line 3$/)
55
+ res[2].should match(/Unrecognised escape sequence '\\\]' .* at line 3$/)
28
56
  end
29
57
  end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'PuppetSyntax rake tasks' do
4
+ it 'should generate FileList of manifests relative to Rakefile' do
5
+ pending
6
+ end
7
+
8
+ it 'should generate FileList of templates relative to Rakefile' do
9
+ pending
10
+ end
11
+ end
@@ -1,32 +1,55 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe PuppetSyntax::Templates do
4
+ let(:subject) { PuppetSyntax::Templates.new }
5
+
6
+ it 'should expect an array of files' do
7
+ expect { subject.check(nil) }.to raise_error(/Expected an array of files/)
8
+ end
9
+
4
10
  it 'should return nothing from a valid file' do
5
- pending
11
+ files = fixture_templates('pass.erb')
12
+ res = subject.check(files)
13
+
14
+ res.should == []
6
15
  end
7
16
 
8
17
  it 'should ignore NameErrors from unbound variables' do
9
- pending
10
- end
18
+ files = fixture_templates('pass_unbound_var.erb')
19
+ res = subject.check(files)
11
20
 
12
- it 'should catch StandardError' do
13
- pending
21
+ res.should == []
14
22
  end
15
23
 
16
24
  it 'should catch SyntaxError' do
17
- pending
25
+ files = fixture_templates('fail_error.erb')
26
+ res = subject.check(files)
27
+
28
+ res.should have(1).items
29
+ res.first.should match(/2: syntax error, unexpected/)
18
30
  end
19
31
 
20
32
  it 'should catch Ruby warnings' do
21
- # <%= if true -%>
22
- pending
33
+ files = fixture_templates('fail_warning.erb')
34
+ res = subject.check(files)
35
+
36
+ res.should have(1).items
37
+ res.first.should match(/2: warning: found = in conditional/)
23
38
  end
24
39
 
25
40
  it 'should read more than one valid file' do
26
- pending
41
+ files = fixture_templates(['pass.erb', 'pass_unbound_var.erb'])
42
+ res = subject.check(files)
43
+
44
+ res.should == []
27
45
  end
28
46
 
29
47
  it 'should continue after finding an error in the first file' do
30
- pending
48
+ files = fixture_templates(['fail_error.erb', 'fail_warning.erb'])
49
+ res = subject.check(files)
50
+
51
+ res.should have(2).items
52
+ res[0].should match(/2: syntax error, unexpected/)
53
+ res[1].should match(/2: warning: found = in conditional/)
31
54
  end
32
55
  end
@@ -1,6 +1,19 @@
1
1
  require 'rspec'
2
2
  require 'puppet-syntax'
3
3
 
4
+ def fixture_templates(list)
5
+ fixture_files(list, 'templates')
6
+ end
7
+
8
+ def fixture_manifests(list)
9
+ fixture_files(list, 'manifests')
10
+ end
11
+
12
+ def fixture_files(list, path)
13
+ list = [list].flatten
14
+ list.map { |f| File.expand_path("../fixtures/test_module/#{path}/#{f}", __FILE__) }
15
+ end
16
+
4
17
  RSpec.configure do |config|
5
18
  config.color_enabled = true
6
19
  config.formatter = 'documentation'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-syntax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-14 00:00:00.000000000 Z
12
+ date: 2013-07-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -100,6 +100,7 @@ extra_rdoc_files: []
100
100
  files:
101
101
  - .gitignore
102
102
  - .travis.yml
103
+ - CHANGELOG
103
104
  - Gemfile
104
105
  - LICENSE.txt
105
106
  - README.md
@@ -110,7 +111,16 @@ files:
110
111
  - lib/puppet-syntax/templates.rb
111
112
  - lib/puppet-syntax/version.rb
112
113
  - puppet-syntax.gemspec
114
+ - spec/fixtures/test_module/manifests/fail_error.pp
115
+ - spec/fixtures/test_module/manifests/fail_warning.pp
116
+ - spec/fixtures/test_module/manifests/pass.pp
117
+ - spec/fixtures/test_module/manifests/pass_storeconfigs.pp
118
+ - spec/fixtures/test_module/templates/fail_error.erb
119
+ - spec/fixtures/test_module/templates/fail_warning.erb
120
+ - spec/fixtures/test_module/templates/pass.erb
121
+ - spec/fixtures/test_module/templates/pass_unbound_var.erb
113
122
  - spec/puppet-syntax/manifests_spec.rb
123
+ - spec/puppet-syntax/tasks/puppet-syntax_spec.rb
114
124
  - spec/puppet-syntax/templates_spec.rb
115
125
  - spec/puppet-syntax_spec.rb
116
126
  - spec/spec_helper.rb
@@ -129,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
139
  version: '0'
130
140
  segments:
131
141
  - 0
132
- hash: -2044382737602392428
142
+ hash: -368354706311963489
133
143
  required_rubygems_version: !ruby/object:Gem::Requirement
134
144
  none: false
135
145
  requirements:
@@ -138,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
148
  version: '0'
139
149
  segments:
140
150
  - 0
141
- hash: -2044382737602392428
151
+ hash: -368354706311963489
142
152
  requirements: []
143
153
  rubyforge_project:
144
154
  rubygems_version: 1.8.23
@@ -146,7 +156,16 @@ signing_key:
146
156
  specification_version: 3
147
157
  summary: ''
148
158
  test_files:
159
+ - spec/fixtures/test_module/manifests/fail_error.pp
160
+ - spec/fixtures/test_module/manifests/fail_warning.pp
161
+ - spec/fixtures/test_module/manifests/pass.pp
162
+ - spec/fixtures/test_module/manifests/pass_storeconfigs.pp
163
+ - spec/fixtures/test_module/templates/fail_error.erb
164
+ - spec/fixtures/test_module/templates/fail_warning.erb
165
+ - spec/fixtures/test_module/templates/pass.erb
166
+ - spec/fixtures/test_module/templates/pass_unbound_var.erb
149
167
  - spec/puppet-syntax/manifests_spec.rb
168
+ - spec/puppet-syntax/tasks/puppet-syntax_spec.rb
150
169
  - spec/puppet-syntax/templates_spec.rb
151
170
  - spec/puppet-syntax_spec.rb
152
171
  - spec/spec_helper.rb