rspec-puppet 2.1.0 → 2.2.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.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NGRlMzY5NDE4OWNiN2Y2MTViMzE3YTA4N2MwMDJkN2FiNjljMjg1Mw==
5
- data.tar.gz: !binary |-
6
- MWExYmViYmQ5Y2RiN2E4ZjU2ZWRjODc1MTg4NmExN2VkYzI1ZmQxZg==
2
+ SHA1:
3
+ metadata.gz: 99267b05af6dbb02ad504453ec338ea2617e1768
4
+ data.tar.gz: ffb1e195adc74932acdbc6581eae52317018dc08
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- YzIzNjc1NzRmOTlmODU2Yjc5MGVhYTAxZTkzZjc2Mjc3Zjc5MmVhNzg2MWE1
10
- ZDJkNDNiM2U3YWNjYTlmMTY4ZDZmYTc1NDE1MzNlZDVkNThiOWI4NDU3M2Q2
11
- ZjJkZmMxMTJkMGU1NWYzZTUzMDRlZDY2MmFiODg0ODg2YWFiYzU=
12
- data.tar.gz: !binary |-
13
- MDNkNTU4MGYyNWYzZDUwYWRjM2I5MWZiMDgxYzE5NTE5YjJmYTY1NzVhYTYw
14
- MzE0YmQxNDk2NzgzNDI1MGY3YmJmNWZjZmZlZmQwZTI2YjkyZWI1OGE3ZWY5
15
- ZjI3Zjc3ZmQwZDI4N2Q1MDRkY2NhNDJjNmYxZmZhNzI4OGRkZjM=
6
+ metadata.gz: f1036132529e546b54f1e475c69f437402c994328795cf8d2828a619cd2e7cb2e1d7a6e51fc01dd95860e4623f650bac6983f3eaf432d8c664ea223370ba2ec3
7
+ data.tar.gz: 9d0e5709f38c09486749c1cd4bea517aa2757988e2bb55f430b241ed029d88bb2afa939392924378bf7f15924625d13a051fddef342b04052642241971cbb5bc
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
  All notable changes to this project will be documented in this file. This
3
3
  project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
+ ## [2.2.0]
6
+ ### Added
7
+ - Settings for ordering, strict_variables, stringify_facts, and trusted_node_data
8
+ - Expose the scope in function example groups
9
+
10
+ ### Fixed
11
+ - rspec-puppet-init now works with Puppet 4
12
+ - Several fixes and enhancements for the `run` matcher
13
+ - Recompile the catalog when the hiera config changes
14
+
5
15
  ## [2.1.0] - 2015-04-21
6
16
  ### Added
7
17
  - Puppet 4 support
data/lib/rspec-puppet.rb CHANGED
@@ -24,6 +24,10 @@ RSpec.configure do |c|
24
24
  c.add_setting :default_facts, :default => {}
25
25
  c.add_setting :hiera_config, :default => '/dev/null'
26
26
  c.add_setting :parser, :default => 'current'
27
+ c.add_setting :trusted_node_data, :default => false
28
+ c.add_setting :ordering, :default => 'title-hash'
29
+ c.add_setting :stringify_facts, :default => true
30
+ c.add_setting :strict_variables, :default => false
27
31
 
28
32
  if defined?(Puppet::Test::TestHelper)
29
33
  begin
@@ -9,8 +9,6 @@ module RSpec::Puppet
9
9
 
10
10
  vardir = setup_puppet
11
11
 
12
- node_name = nodename(:function)
13
-
14
12
  if Puppet.version.to_f >= 4.0
15
13
  env = Puppet::Node::Environment.create(environment, [File.join(Puppet[:environmentpath],'fixtures','modules')], File.join(Puppet[:environmentpath],'fixtures','manifests'))
16
14
  loader = Puppet::Pops::Loaders.new(env)
@@ -18,8 +16,6 @@ module RSpec::Puppet
18
16
  return func if func
19
17
  end
20
18
 
21
- function_scope = scope(compiler, node_name)
22
-
23
19
  # Return the method instance for the function. This can be used with
24
20
  # method.call
25
21
  if env
@@ -28,7 +24,11 @@ module RSpec::Puppet
28
24
  return nil unless Puppet::Parser::Functions.function(function_name)
29
25
  end
30
26
  FileUtils.rm_rf(vardir) if File.directory?(vardir)
31
- function_scope.method("function_#{function_name}".intern)
27
+ scope.method("function_#{function_name}".intern)
28
+ end
29
+
30
+ def scope
31
+ @scope ||= build_scope(compiler, nodename(:function))
32
32
  end
33
33
 
34
34
  def catalogue
@@ -17,42 +17,41 @@ module RSpec::Puppet
17
17
  end
18
18
  end
19
19
 
20
- unless @expected_error.nil?
21
- result = false
22
- begin
23
- @func.call
24
- rescue Exception => e
25
- @actual_error = e.class
26
- @actual_error_message = e.to_s
27
- if e.is_a?(@expected_error)
28
- case @expected_error_message
29
- when nil
30
- result = true
31
- when Regexp
32
- result = @expected_error_message =~ e.message
33
- else
34
- result = @expected_error_message == e.message
35
- end
20
+ begin
21
+ @actual_return = @func.call
22
+ @has_returned = true
23
+ rescue Exception => e
24
+ @actual_error = e
25
+ end
26
+
27
+ if @has_expected_error
28
+ if @has_returned
29
+ return false
30
+ elsif @actual_error.is_a?(@expected_error)
31
+ case @expected_error_message
32
+ when nil
33
+ return true
34
+ when Regexp
35
+ return @actual_error.message =~ @expected_error_message
36
+ else
37
+ return @actual_error.message == @expected_error_message
36
38
  end
39
+ else # error did not match
40
+ return false
37
41
  end
38
- result
39
- else
40
- unless @expected_return.nil?
41
- @actual_return = @func.call
42
+ elsif @has_expected_return
43
+ if !@has_returned
44
+ return false
45
+ else
42
46
  case @expected_return
43
47
  when Regexp
44
- @actual_return =~ @expected_return
48
+ return @actual_return =~ @expected_return
45
49
  else
46
- @actual_return == @expected_return
50
+ return @actual_return == @expected_return
47
51
  end
48
- else
49
- begin
50
- @func.call
51
- rescue
52
- false
53
- end
54
- true
55
52
  end
53
+ else
54
+ return @has_returned
56
55
  end
57
56
  end
58
57
 
@@ -65,6 +64,7 @@ module RSpec::Puppet
65
64
  end
66
65
 
67
66
  def and_return(value)
67
+ @has_expected_return = true
68
68
  @expected_return = value
69
69
  if value.is_a? Regexp
70
70
  @desc = "match #{value.inspect}"
@@ -75,6 +75,7 @@ module RSpec::Puppet
75
75
  end
76
76
 
77
77
  def and_raise_error(error_or_message, message=nil)
78
+ @has_expected_error = true
78
79
  case error_or_message
79
80
  when String, Regexp
80
81
  @expected_error, @expected_error_message = Exception, error_or_message
@@ -102,7 +103,11 @@ module RSpec::Puppet
102
103
  end
103
104
 
104
105
  def description
105
- "run #{func_name}(#{func_params}) and #{@desc}"
106
+ if @desc
107
+ "run #{func_name}(#{func_params}) and #{@desc}"
108
+ else
109
+ "run #{func_name}(#{func_params}) without error"
110
+ end
106
111
  end
107
112
 
108
113
  private
@@ -118,34 +123,41 @@ module RSpec::Puppet
118
123
  @func_args
119
124
  end
120
125
 
126
+ def failure_message_actual(type)
127
+ if type != :should
128
+ ''
129
+ elsif @actual_error
130
+ if @has_expected_return
131
+ " instead of raising #{@actual_error.class.inspect}(#{@actual_error})"
132
+ else
133
+ " instead of #{@actual_error.class.inspect}(#{@actual_error})"
134
+ end
135
+ else # function has returned
136
+ if @has_expected_error
137
+ " instead of returning #{@actual_return.inspect}"
138
+ else
139
+ " instead of #{@actual_return.inspect}"
140
+ end
141
+ end
142
+ end
143
+
121
144
  def failure_message_generic(type, func_obj)
122
145
  message = "expected #{func_name}(#{func_params}) to "
123
146
  message << "not " if type == :should_not
124
147
 
125
- if @expected_return
148
+ if @has_expected_return
126
149
  message << "have returned #{@expected_return.inspect}"
127
- if type == :should
128
- message << " instead of #{@actual_return.inspect}"
129
- end
130
- elsif @expected_error
131
- message << "have raised #{@expected_error.inspect}"
132
- if @expected_error_message
133
- message << " matching #{@expected_error_message.inspect}"
134
- end
135
- if type == :should
136
- if @actual_error
137
- message << " instead of raising #{@actual_error.inspect}"
138
- if @expected_error_message
139
- message << "(#{@actual_error_message})"
140
- end
141
- elsif @actual_return
142
- message << " instead of returning #{@actual_return.inspect}"
150
+ else
151
+ if @has_expected_error
152
+ message << "have raised #{@expected_error.inspect}"
153
+ if @expected_error_message
154
+ message << " matching #{@expected_error_message.inspect}"
143
155
  end
156
+ else
157
+ message << "have run successfully"
144
158
  end
145
- else
146
- message << "have run successfully"
147
159
  end
148
- message
160
+ message << failure_message_actual(type)
149
161
  end
150
162
  end
151
163
  end
@@ -55,7 +55,11 @@ module RSpec::Puppet
55
55
  end
56
56
 
57
57
  def self.get_module_name_from_file(file)
58
- p = Puppet::Parser::Lexer.new
58
+ if Puppet.version.to_f >= 4.0
59
+ p = Puppet::Pops::Parser::Lexer2.new
60
+ else
61
+ p = Puppet::Parser::Lexer.new
62
+ end
59
63
  module_name = nil
60
64
  p.string = File.read(file)
61
65
  tokens = p.fullscan
@@ -117,6 +121,7 @@ fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
117
121
  RSpec.configure do |c|
118
122
  c.module_path = File.join(fixture_path, 'modules')
119
123
  c.manifest_dir = File.join(fixture_path, 'manifests')
124
+ c.environmentpath = File.join(Dir.pwd, 'spec')
120
125
  end
121
126
  EOF
122
127
  safe_create_file('spec/spec_helper.rb', content)
@@ -140,7 +145,8 @@ require 'rspec/core/rake_task'
140
145
 
141
146
  desc "Run all RSpec code examples"
142
147
  RSpec::Core::RakeTask.new(:rspec) do |t|
143
- t.rspec_opts = File.read("spec/spec.opts").chomp || ""
148
+ File.exist?('spec/spec.opts') ? opts = File.read("spec/spec.opts").chomp : opts = ""
149
+ t.rspec_opts = opts
144
150
  end
145
151
 
146
152
  SPEC_SUITES = (Dir.entries('spec') - ['.', '..','fixtures']).select {|e| File.directory? "spec/#{e}" }
@@ -149,7 +155,8 @@ namespace :rspec do
149
155
  desc "Run #{suite} RSpec code examples"
150
156
  RSpec::Core::RakeTask.new(suite) do |t|
151
157
  t.pattern = "spec/#{suite}/**/*_spec.rb"
152
- t.rspec_opts = File.read("spec/spec.opts").chomp || ""
158
+ File.exist?('spec/spec.opts') ? opts = File.read("spec/spec.opts").chomp : opts = ""
159
+ t.rspec_opts = opts
153
160
  end
154
161
  end
155
162
  end
@@ -22,7 +22,9 @@ module RSpec::Puppet
22
22
 
23
23
  node_name = nodename(type)
24
24
 
25
- catalogue = build_catalog(node_name, facts_hash(node_name), code)
25
+ hiera_config_value = self.respond_to?(:hiera_config) ? hiera_config : nil
26
+
27
+ catalogue = build_catalog(node_name, facts_hash(node_name), hiera_config_value, code)
26
28
 
27
29
  test_module = class_name.split('::').first
28
30
  RSpec::Puppet::Coverage.add_filter(type.to_s, self.class.description)
@@ -127,12 +129,14 @@ module RSpec::Puppet
127
129
 
128
130
  if Puppet.version.to_f >= 4.0
129
131
  settings = [
132
+ [:modulepath, :module_path],
130
133
  [:environmentpath, :environmentpath],
131
134
  [:config, :config],
132
135
  [:confdir, :confdir],
133
136
  [:hiera_config, :hiera_config],
137
+ [:strict_variables, :strict_variables],
134
138
  ]
135
- else
139
+ elsif Puppet.version.to_f >= 3.0
136
140
  settings = [
137
141
  [:modulepath, :module_path],
138
142
  [:manifestdir, :manifest_dir],
@@ -142,6 +146,19 @@ module RSpec::Puppet
142
146
  [:confdir, :confdir],
143
147
  [:hiera_config, :hiera_config],
144
148
  [:parser, :parser],
149
+ [:trusted_node_data, :trusted_node_data],
150
+ [:ordering, :ordering],
151
+ [:stringify_facts, :stringify_facts],
152
+ [:strict_variables, :strict_variables],
153
+ ]
154
+ else
155
+ settings = [
156
+ [:modulepath, :module_path],
157
+ [:manifestdir, :manifest_dir],
158
+ [:manifest, :manifest],
159
+ [:templatedir, :template_dir],
160
+ [:config, :config],
161
+ [:confdir, :confdir],
145
162
  ]
146
163
  end
147
164
  settings.each do |a,b|
@@ -153,12 +170,24 @@ module RSpec::Puppet
153
170
  end
154
171
  end
155
172
 
156
- # This line is wrong. libdir should never be more than a single path
157
- Puppet[:libdir] = Dir["#{Puppet[:modulepath]}/*/lib"].entries.join(File::PATH_SEPARATOR)
173
+ Dir["#{Puppet[:modulepath]}/*/lib"].entries.each do |lib|
174
+ $LOAD_PATH << lib
175
+ end
176
+
158
177
  vardir
159
178
  end
160
179
 
161
- def build_catalog_without_cache(nodename, facts_val, code)
180
+ def build_catalog_without_cache(nodename, facts_val, hiera_config_val, code)
181
+
182
+ # If we're going to rebuild the catalog, we should clear the cached instance
183
+ # of Hiera that Puppet is using. This opens the possibility of the catalog
184
+ # now being rebuilt against a differently configured Hiera (i.e. :hiera_config
185
+ # set differently in one example group vs. another).
186
+ # It would be nice if Puppet offered a public API for invalidating their
187
+ # cached instance of Hiera, but que sera sera. We will go directly against
188
+ # the implementation out of absolute necessity.
189
+ HieraPuppet.instance_variable_set('@hiera', nil) if defined? HieraPuppet
190
+
162
191
  Puppet[:code] = code
163
192
 
164
193
  stub_facts! facts_val
@@ -204,7 +233,7 @@ module RSpec::Puppet
204
233
  string
205
234
  end
206
235
 
207
- def scope(compiler, node_name)
236
+ def build_scope(compiler, node_name)
208
237
  if Puppet.version =~ /^2\.[67]/
209
238
  # loadall should only be necessary prior to 3.x
210
239
  # Please note, loadall needs to happen first when creating a scope, otherwise
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-puppet
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Sharpe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-22 00:00:00.000000000 Z
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  description: RSpec tests for your Puppet manifests
@@ -62,19 +62,18 @@ require_paths:
62
62
  - lib
63
63
  required_ruby_version: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - ! '>='
65
+ - - ">="
66
66
  - !ruby/object:Gem::Version
67
67
  version: '0'
68
68
  required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  requirements:
70
- - - ! '>='
70
+ - - ">="
71
71
  - !ruby/object:Gem::Version
72
72
  version: '0'
73
73
  requirements: []
74
74
  rubyforge_project:
75
- rubygems_version: 2.4.5
75
+ rubygems_version: 2.2.2
76
76
  signing_key:
77
77
  specification_version: 4
78
78
  summary: RSpec tests for your Puppet manifests
79
79
  test_files: []
80
- has_rdoc: