rspec-puppet 2.7.0 → 2.12.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.
@@ -0,0 +1,49 @@
1
+ module RSpec::Puppet
2
+
3
+ # Implements a simple hash-based version of Facter to be used in module tests
4
+ # that use rspec-puppet.
5
+ class FacterTestImpl
6
+ def initialize
7
+ @facts = {}
8
+ end
9
+
10
+ def value(fact_name)
11
+ @facts[fact_name.to_s]
12
+ end
13
+
14
+ def clear
15
+ @facts.clear
16
+ end
17
+
18
+ def to_hash
19
+ @facts
20
+ end
21
+
22
+ def add(name, options = {}, &block)
23
+ raise 'Facter.add expects a block' unless block_given?
24
+ @facts[name.to_s] = instance_eval(&block)
25
+ end
26
+
27
+ # noop methods
28
+ def debugging(arg); end
29
+
30
+ def reset; end
31
+
32
+ def search(*paths); end
33
+
34
+ def setup_logging; end
35
+
36
+ private
37
+
38
+ def setcode(string = nil, &block)
39
+ if block_given?
40
+ value = block.call
41
+ else
42
+ value = string
43
+ end
44
+
45
+ value
46
+ end
47
+ end
48
+ end
49
+
@@ -34,6 +34,14 @@ module RSpec::Puppet
34
34
  def failure_message_when_negated
35
35
  "expected that the type alias would not " + description + " but it does"
36
36
  end
37
+
38
+ def supports_block_expectations
39
+ true
40
+ end
41
+
42
+ def supports_value_expectations
43
+ true
44
+ end
37
45
  end
38
46
 
39
47
  def allow_value(*values)
@@ -78,6 +78,14 @@ module RSpec::Puppet
78
78
  end
79
79
  end
80
80
 
81
+ def supports_block_expectations
82
+ true
83
+ end
84
+
85
+ def supports_value_expectations
86
+ true
87
+ end
88
+
81
89
  private
82
90
  def missing_dependencies?
83
91
  retval = false
@@ -7,13 +7,15 @@ module RSpec::Puppet
7
7
  'Stage[main]',
8
8
  ].freeze
9
9
 
10
+ attr_reader :resource_type
11
+
10
12
  def initialize(type, count, *method)
11
13
  if type.nil?
12
14
  @type = method[0].to_s.gsub(/^have_(.+)_resource_count$/, '\1')
13
15
  else
14
16
  @type = type
15
17
  end
16
- @referenced_type = referenced_type(@type)
18
+ @resource_type = referenced_type(@type)
17
19
  @expected_number = count.to_i
18
20
  end
19
21
 
@@ -30,7 +32,7 @@ module RSpec::Puppet
30
32
  end
31
33
  else
32
34
  resources.count do |res|
33
- res.type == @referenced_type
35
+ res.type == @resource_type
34
36
  end
35
37
  end
36
38
 
@@ -45,7 +47,7 @@ module RSpec::Puppet
45
47
  desc << "#{@expected_number == 1 ? "class" : "classes" }"
46
48
  else
47
49
  unless @type == "resource"
48
- desc << "#{@referenced_type}"
50
+ desc << "#{@resource_type}"
49
51
  end
50
52
  desc << "#{@expected_number == 1 ? "resource" : "resources" }"
51
53
  end
@@ -61,6 +63,14 @@ module RSpec::Puppet
61
63
  "expected that the catalogue would not " + description + " but it does"
62
64
  end
63
65
 
66
+ def supports_block_expectations
67
+ true
68
+ end
69
+
70
+ def supports_value_expectations
71
+ true
72
+ end
73
+
64
74
  private
65
75
 
66
76
  def referenced_type(type)
@@ -89,6 +89,11 @@ module RSpec::Puppet
89
89
  else
90
90
  RSpec::Puppet::Coverage.cover!(resource)
91
91
  rsrc_hsh = resource.to_hash
92
+ if resource.respond_to?(:sensitive_parameters)
93
+ resource.sensitive_parameters.each do |s_param|
94
+ rsrc_hsh[s_param] = ::Puppet::Pops::Types::PSensitiveType::Sensitive.new(rsrc_hsh[s_param])
95
+ end
96
+ end
92
97
 
93
98
  if resource.builtin_type?
94
99
  namevar = resource.resource_type.key_attributes.first.to_s
@@ -177,6 +182,14 @@ module RSpec::Puppet
177
182
  true
178
183
  end
179
184
 
185
+ def supports_block_expectations
186
+ true
187
+ end
188
+
189
+ def supports_value_expectations
190
+ true
191
+ end
192
+
180
193
  def expected
181
194
  @errors.map {|e| e.expected if e.respond_to?(:expected)}.compact.join("\n\n")
182
195
  end
@@ -294,14 +307,19 @@ module RSpec::Puppet
294
307
  end
295
308
  end
296
309
 
297
- # Add autorequires if any
298
- if type == :require and resource.resource_type.respond_to? :eachautorequire
299
- resource.resource_type.eachautorequire do |t, b|
300
- Array(resource.to_ral.instance_eval(&b)).each do |dep|
301
- res = "#{t.to_s.capitalize}[#{dep}]"
302
- if r = relationship_refs(res, type, visited)
303
- results << res
304
- results << r
310
+ # Add auto* (autorequire etc) if any
311
+ if [:before, :notify, :require, :subscribe].include?(type)
312
+ func = "eachauto#{type}".to_sym
313
+ if resource.resource_type.respond_to?(func)
314
+ resource.resource_type.send(func) do |t, b|
315
+ Array(resource.to_ral.instance_eval(&b)).each do |dep|
316
+ next if dep.nil?
317
+
318
+ res = "#{t.to_s.capitalize}[#{dep}]"
319
+ if r = relationship_refs(res, type, visited)
320
+ results << res
321
+ results << r
322
+ end
305
323
  end
306
324
  end
307
325
  end
@@ -4,7 +4,7 @@ module RSpec::Puppet
4
4
 
5
5
  matcher :include_class do |expected_class|
6
6
  match do |catalogue|
7
- RSpec.deprecate(:include_class, :replacement => :contain_class)
7
+ RSpec.deprecate('include_class()', :replacement => 'contain_class()')
8
8
  catalogue.call.classes.include?(expected_class)
9
9
  end
10
10
 
@@ -22,6 +22,13 @@ module RSpec::Puppet
22
22
  end
23
23
  end
24
24
 
25
+ def supports_block_expectations
26
+ true
27
+ end
28
+
29
+ def supports_value_expectations
30
+ true
31
+ end
25
32
  end
26
33
 
27
34
  end
@@ -21,10 +21,7 @@ module RSpec::Puppet
21
21
  #
22
22
  # @return [true, false]
23
23
  def matches?(resource)
24
-
25
- @resource = resource
26
-
27
- actual = @resource[@parameter]
24
+ actual = resource[@parameter]
28
25
  expected = @value
29
26
 
30
27
  # Puppet flattens an array with a single value into just the value and
@@ -65,6 +62,8 @@ module RSpec::Puppet
65
62
  check_hash(expected, actual)
66
63
  when Array
67
64
  check_array(expected, actual)
65
+ when RSpec::Puppet::Sensitive
66
+ expected == actual
68
67
  else
69
68
  check_string(expected, actual)
70
69
  end
@@ -78,7 +77,7 @@ module RSpec::Puppet
78
77
  end
79
78
 
80
79
  def check_regexp(expected, actual)
81
- !!(munge_line_endings(actual.to_s).match expected) == @should_match
80
+ !!(actual.to_s.match expected) == @should_match
82
81
  end
83
82
 
84
83
  # Ensure that two hashes have the same number of keys, and that for each
@@ -119,17 +118,7 @@ module RSpec::Puppet
119
118
  end
120
119
 
121
120
  def check_string(expected, actual)
122
- (expected.to_s == munge_line_endings(actual.to_s)) == @should_match
123
- end
124
-
125
- def munge_line_endings(value)
126
- return value unless value.respond_to?(:encode)
127
-
128
- if Puppet::Util::Platform.windows?
129
- value.encode(:crlf_newline => true)
130
- else
131
- value.encode(:universal_newline => true)
132
- end
121
+ (expected.to_s == actual.to_s) == @should_match
133
122
  end
134
123
  end
135
124
  end
@@ -0,0 +1,23 @@
1
+ module RSpec::Puppet
2
+ module GenericMatchers
3
+ # Due to significant code base depending on the
4
+ #
5
+ # is_expected.to raise_error Puppet::Error
6
+ #
7
+ # syntax, and removal of this syntax from RSpec, extend RSpec's built-in
8
+ # `raise_error` matcher to accept a value target, e.g. a subject defined
9
+ # as a lambda, e.g.:
10
+ #
11
+ # subject(:catalogue) { lambda { load_catalogue } }
12
+ #
13
+ class RaiseError < RSpec::Matchers::BuiltIn::RaiseError
14
+ def supports_value_expectations?
15
+ true
16
+ end
17
+ end
18
+
19
+ def raise_error(error=defined?(RSpec::Matchers::BuiltIn::RaiseError::UndefinedValue) ? RSpec::Matchers::BuiltIn::RaiseError::UndefinedValue : nil, message=nil, &block)
20
+ RaiseError.new(error, message, &block)
21
+ end
22
+ end
23
+ end
@@ -35,6 +35,8 @@ module RSpec::Puppet
35
35
  case @expected_return
36
36
  when Regexp
37
37
  return !!(@actual_return =~ @expected_return)
38
+ when RSpec::Mocks::ArgumentMatchers::KindOf, RSpec::Matchers::AliasedMatcher
39
+ return @expected_return === @actual_return
38
40
  else
39
41
  return @actual_return == @expected_return
40
42
  end
@@ -104,6 +106,14 @@ module RSpec::Puppet
104
106
  end
105
107
  end
106
108
 
109
+ def supports_block_expectations
110
+ true
111
+ end
112
+
113
+ def supports_value_expectations
114
+ true
115
+ end
116
+
107
117
  private
108
118
  def func_name
109
119
  @func_obj.func_name
@@ -52,9 +52,6 @@ module RSpec::Puppet
52
52
  self
53
53
  end
54
54
 
55
- #def with_autorequires(autorequires))
56
- #end
57
-
58
55
  #
59
56
  # this is the method that drives all of the validation
60
57
  #
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ module Puppet
5
+ module ManifestMatchers
6
+ extend RSpec::Matchers::DSL
7
+
8
+ matcher :have_unique_values_for_all do |type, attribute|
9
+ match do |catalogue|
10
+ catalogue.call.resources.select { |rsrc| rsrc.type == type.capitalize }
11
+ .group_by { |rsrc| rsrc[attribute.to_sym] }
12
+ .all? { |_, group| group.size == 1 }
13
+ end
14
+
15
+ description do
16
+ "have unique attribute values for #{type.capitalize}[#{attribute.to_sym}]"
17
+ end
18
+
19
+ if RSpec::Version::STRING < '3'
20
+ failure_message_for_should do |_actual|
21
+ "expected that the catalogue would have no duplicate values for #{type.capitalize}[#{attribute.to_sym}]"
22
+ end
23
+ else
24
+ failure_message do |_actual|
25
+ "expected that the catalogue would have no duplicate values for #{type.capitalize}[#{attribute.to_sym}]"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -6,3 +6,5 @@ require 'rspec-puppet/matchers/count_generic'
6
6
  require 'rspec-puppet/matchers/dynamic_matchers'
7
7
  require 'rspec-puppet/matchers/type_matchers'
8
8
  require 'rspec-puppet/matchers/allow_value'
9
+ require 'rspec-puppet/matchers/raise_error'
10
+ require 'rspec-puppet/matchers/unique_values'
@@ -132,6 +132,16 @@ module Puppet
132
132
  end
133
133
 
134
134
  module Util
135
+ # Fix for removal of default_env function
136
+ # Bug: https://github.com/rodjek/rspec-puppet/issues/796
137
+ # Upstream: https://github.com/puppetlabs/puppet/commit/94df3c1a3992d89b2d7d5db8a70373c135bdd86b
138
+ if !respond_to?(:default_env)
139
+ def default_env()
140
+ DEFAULT_ENV
141
+ end
142
+ module_function :default_env
143
+ end
144
+
135
145
  if respond_to?(:get_env)
136
146
  alias :old_get_env :get_env
137
147
  module_function :old_get_env
@@ -213,7 +223,7 @@ module Puppet
213
223
  end
214
224
 
215
225
  class Autoload
216
- if singleton_class.respond_to?(:load_file)
226
+ if respond_to?(:load_file)
217
227
  singleton_class.send(:alias_method, :old_load_file, :load_file)
218
228
 
219
229
  def self.load_file(*args)
@@ -258,6 +268,40 @@ module Puppet
258
268
  end
259
269
  end
260
270
  end
271
+
272
+ if Puppet::Util::Package.versioncmp(Puppet.version, '4.9.0') >= 0
273
+ class Module
274
+ old_hiera_conf_file = instance_method(:hiera_conf_file)
275
+ define_method(:hiera_conf_file) do
276
+ if RSpec::Puppet.rspec_puppet_example?
277
+ if RSpec.configuration.disable_module_hiera
278
+ return nil
279
+ elsif RSpec.configuration.fixture_hiera_configs.key?(name)
280
+ config = RSpec.configuration.fixture_hiera_configs[name]
281
+ config = File.absolute_path(config, path) unless config.nil?
282
+ return config
283
+ elsif RSpec.configuration.use_fixture_spec_hiera
284
+ config = RSpec::Puppet.current_example.fixture_spec_hiera_conf(self)
285
+ return config unless config.nil? && RSpec.configuration.fallback_to_default_hiera
286
+ end
287
+ end
288
+ old_hiera_conf_file.bind(self).call
289
+ end
290
+ end
291
+
292
+ class Pops::Lookup::ModuleDataProvider
293
+ old_configuration_path = instance_method(:configuration_path)
294
+ define_method(:configuration_path) do |lookup_invocation|
295
+ if RSpec::Puppet.rspec_puppet_example?
296
+ env = lookup_invocation.scope.environment
297
+ mod = env.module(module_name)
298
+ raise Puppet::DataBinding::LookupError, _("Environment '%{env}', cannot find module '%{module_name}'") % { :env => env.name, :module_name => module_name } unless mod
299
+ return Pathname.new(mod.hiera_conf_file)
300
+ end
301
+ old_configuration_path.bind(self).call(lookup_invocation)
302
+ end
303
+ end
304
+ end
261
305
  end
262
306
 
263
307
  class Pathname
@@ -292,6 +336,24 @@ class Pathname
292
336
  end
293
337
  end
294
338
 
339
+ # Puppet loads init.pp, then foo.pp, to find class "mod::foo". If
340
+ # class "mod" has been mocked using pre_condition when testing
341
+ # "mod::foo", this causes duplicate declaration for "mod".
342
+ # This monkey patch only loads "init.pp" if "foo.pp" does not exist.
343
+ class Puppet::Module
344
+ if [:match_manifests, 'match_manifests'].any? { |r| instance_methods.include?(r) }
345
+ old_match_manifests = instance_method(:match_manifests)
346
+
347
+ define_method(:match_manifests) do |rest|
348
+ result = old_match_manifests.bind(self).call(rest)
349
+ if result.length > 1 && File.basename(result[0]) == 'init.pp'
350
+ result.shift
351
+ end
352
+ result
353
+ end
354
+ end
355
+ end
356
+
295
357
  # Prevent the File type from munging paths (which uses File.expand_path to
296
358
  # normalise paths, which does very bad things to *nix paths on Windows.
297
359
  file_path_munge = Puppet::Type.type(:file).paramclass(:path).instance_method(:unsafe_munge)
@@ -0,0 +1,51 @@
1
+ module RSpec::Puppet
2
+ if defined?(::Puppet::Pops::Types::PSensitiveType::Sensitive)
3
+ # A wrapper representing Sensitive data type, eg. in class params.
4
+ class Sensitive < ::Puppet::Pops::Types::PSensitiveType::Sensitive
5
+ # Create a new Sensitive object
6
+ # @param [Object] value to wrap
7
+ def initialize(value)
8
+ @value = value
9
+ end
10
+
11
+ # @return the wrapped value
12
+ def unwrap
13
+ @value
14
+ end
15
+
16
+ # @return true
17
+ def sensitive?
18
+ true
19
+ end
20
+
21
+ # @return inspect of the wrapped value, inside Sensitive()
22
+ def inspect
23
+ "Sensitive(#{@value.inspect})"
24
+ end
25
+
26
+ # Check for equality with another value.
27
+ # If compared to Puppet Sensitive type, it compares the wrapped values.
28
+
29
+ # @param other [#unwrap, Object] value to compare to
30
+ def == other
31
+ if other.respond_to? :unwrap
32
+ if unwrap.kind_of?(Regexp)
33
+ return unwrap =~ other.unwrap
34
+ else
35
+ return unwrap == other.unwrap
36
+ end
37
+ else
38
+ super
39
+ end
40
+ end
41
+ end
42
+ else
43
+ #:nocov:
44
+ class Sensitive
45
+ def initialize(value)
46
+ raise 'The use of the Sensitive data type is not supported by this Puppet version'
47
+ end
48
+ end
49
+ #:nocov:
50
+ end
51
+ end