rspec-puppet 2.12.0 → 3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +140 -480
- data/README.md +18 -5
- data/bin/rspec-puppet-init +4 -3
- data/lib/rspec-puppet/adapters.rb +67 -61
- data/lib/rspec-puppet/cache.rb +3 -2
- data/lib/rspec-puppet/consts.rb +16 -14
- data/lib/rspec-puppet/coverage.rb +37 -42
- data/lib/rspec-puppet/errors.rb +6 -6
- data/lib/rspec-puppet/example/application_example_group.rb +3 -1
- data/lib/rspec-puppet/example/class_example_group.rb +3 -1
- data/lib/rspec-puppet/example/define_example_group.rb +3 -1
- data/lib/rspec-puppet/example/function_example_group.rb +28 -23
- data/lib/rspec-puppet/example/host_example_group.rb +3 -1
- data/lib/rspec-puppet/example/provider_example_group.rb +2 -0
- data/lib/rspec-puppet/example/type_alias_example_group.rb +4 -2
- data/lib/rspec-puppet/example/type_example_group.rb +3 -1
- data/lib/rspec-puppet/example.rb +6 -7
- data/lib/rspec-puppet/facter_impl.rb +11 -10
- data/lib/rspec-puppet/matchers/allow_value.rb +10 -10
- data/lib/rspec-puppet/matchers/compile.rb +54 -61
- data/lib/rspec-puppet/matchers/count_generic.rb +18 -18
- data/lib/rspec-puppet/matchers/create_generic.rb +66 -78
- data/lib/rspec-puppet/matchers/dynamic_matchers.rb +13 -2
- data/lib/rspec-puppet/matchers/include_class.rb +5 -4
- data/lib/rspec-puppet/matchers/parameter_matcher.rb +11 -12
- data/lib/rspec-puppet/matchers/raise_error.rb +5 -1
- data/lib/rspec-puppet/matchers/run.rb +41 -44
- data/lib/rspec-puppet/matchers/type_matchers.rb +37 -48
- data/lib/rspec-puppet/matchers.rb +2 -0
- data/lib/rspec-puppet/monkey_patches/win32/registry.rb +7 -5
- data/lib/rspec-puppet/monkey_patches/win32/taskscheduler.rb +3 -1
- data/lib/rspec-puppet/monkey_patches/windows/taskschedulerconstants.rb +208 -205
- data/lib/rspec-puppet/monkey_patches.rb +56 -56
- data/lib/rspec-puppet/rake_task.rb +6 -4
- data/lib/rspec-puppet/raw_string.rb +2 -0
- data/lib/rspec-puppet/sensitive.rb +9 -7
- data/lib/rspec-puppet/setup.rb +43 -48
- data/lib/rspec-puppet/spec_helper.rb +2 -0
- data/lib/rspec-puppet/support.rb +133 -134
- data/lib/rspec-puppet/tasks/release_test.rb +8 -5
- data/lib/rspec-puppet/version.rb +5 -0
- data/lib/rspec-puppet.rb +43 -51
- metadata +9 -7
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'set'
|
2
4
|
require 'rspec-puppet/matchers/parameter_matcher'
|
3
5
|
|
@@ -22,21 +24,21 @@ module RSpec::Puppet
|
|
22
24
|
@befores = []
|
23
25
|
end
|
24
26
|
|
25
|
-
def with(*args
|
27
|
+
def with(*args)
|
26
28
|
params = args.shift
|
27
|
-
@expected_params
|
29
|
+
@expected_params |= params.to_a
|
28
30
|
self
|
29
31
|
end
|
30
32
|
|
31
33
|
def only_with(*args, &block)
|
32
34
|
params = args.shift
|
33
|
-
@expected_params_count = (@expected_params_count || 0) + params.
|
34
|
-
|
35
|
+
@expected_params_count = (@expected_params_count || 0) + params.compact.size
|
36
|
+
with(params, &block)
|
35
37
|
end
|
36
38
|
|
37
|
-
def without(*args
|
39
|
+
def without(*args)
|
38
40
|
params = args.shift
|
39
|
-
@expected_undef_params
|
41
|
+
@expected_undef_params |= Array(params)
|
40
42
|
self
|
41
43
|
end
|
42
44
|
|
@@ -61,16 +63,17 @@ module RSpec::Puppet
|
|
61
63
|
end
|
62
64
|
|
63
65
|
def method_missing(method, *args, &block)
|
64
|
-
|
66
|
+
case method.to_s
|
67
|
+
when /^with_/
|
65
68
|
param = method.to_s.gsub(/^with_/, '')
|
66
69
|
@expected_params << [param, args[0]]
|
67
70
|
self
|
68
|
-
|
71
|
+
when /^only_with_/
|
69
72
|
param = method.to_s.gsub(/^only_with_/, '')
|
70
73
|
@expected_params_count = (@expected_params_count || 0) + 1
|
71
74
|
@expected_params << [param, args[0]]
|
72
75
|
self
|
73
|
-
|
76
|
+
when /^without_/
|
74
77
|
param = method.to_s.gsub(/^without_/, '')
|
75
78
|
@expected_undef_params << [param, args[0]]
|
76
79
|
self
|
@@ -95,21 +98,19 @@ module RSpec::Puppet
|
|
95
98
|
end
|
96
99
|
end
|
97
100
|
|
98
|
-
if resource.builtin_type?
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
101
|
+
namevar = if resource.builtin_type?
|
102
|
+
resource.resource_type.key_attributes.first.to_s
|
103
|
+
else
|
104
|
+
'name'
|
105
|
+
end
|
103
106
|
|
104
|
-
|
105
|
-
rsrc_hsh.delete(namevar.to_sym)
|
107
|
+
if @expected_params.none? { |param| param.first.to_s == namevar } && rsrc_hsh.key?(namevar.to_sym)
|
108
|
+
rsrc_hsh.delete(namevar.to_sym)
|
106
109
|
end
|
107
110
|
|
108
|
-
if @expected_params_count
|
109
|
-
|
110
|
-
|
111
|
-
(@errors ||= []) << "exactly #{@expected_params_count} parameters but the catalogue contains #{rsrc_hsh.size}"
|
112
|
-
end
|
111
|
+
if @expected_params_count && rsrc_hsh.size != @expected_params_count
|
112
|
+
ret = false
|
113
|
+
(@errors ||= []) << "exactly #{@expected_params_count} parameters but the catalogue contains #{rsrc_hsh.size}"
|
113
114
|
end
|
114
115
|
|
115
116
|
check_params(rsrc_hsh, @expected_params, :should) if @expected_params.any?
|
@@ -133,46 +134,40 @@ module RSpec::Puppet
|
|
133
134
|
|
134
135
|
def description
|
135
136
|
values = []
|
136
|
-
value_str_prefix =
|
137
|
+
value_str_prefix = 'with'
|
137
138
|
|
138
|
-
if @expected_params_count
|
139
|
-
values << "exactly #{@expected_params_count} parameters"
|
140
|
-
end
|
139
|
+
values << "exactly #{@expected_params_count} parameters" if @expected_params_count
|
141
140
|
|
142
|
-
if @expected_params.any?
|
143
|
-
values.concat(generate_param_list(@expected_params, :should))
|
144
|
-
end
|
141
|
+
values.concat(generate_param_list(@expected_params, :should)) if @expected_params.any?
|
145
142
|
|
146
|
-
if @expected_undef_params.any?
|
147
|
-
values.concat(generate_param_list(@expected_undef_params, :not))
|
148
|
-
end
|
143
|
+
values.concat(generate_param_list(@expected_undef_params, :not)) if @expected_undef_params.any?
|
149
144
|
|
150
145
|
if @notifies.any?
|
151
|
-
value_str_prefix =
|
146
|
+
value_str_prefix = 'that notifies'
|
152
147
|
values = @notifies
|
153
148
|
end
|
154
149
|
|
155
150
|
if @subscribes.any?
|
156
|
-
value_str_prefix =
|
151
|
+
value_str_prefix = 'that subscribes to'
|
157
152
|
values = @subscribes
|
158
153
|
end
|
159
154
|
|
160
155
|
if @requires.any?
|
161
|
-
value_str_prefix =
|
156
|
+
value_str_prefix = 'that requires'
|
162
157
|
values = @requires
|
163
158
|
end
|
164
159
|
|
165
160
|
if @befores.any?
|
166
|
-
value_str_prefix =
|
161
|
+
value_str_prefix = 'that comes before'
|
167
162
|
values = @befores
|
168
163
|
end
|
169
164
|
|
170
165
|
unless values.empty?
|
171
|
-
if values.length == 1
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
166
|
+
value_str = if values.length == 1
|
167
|
+
" #{value_str_prefix} #{values.first}"
|
168
|
+
else
|
169
|
+
" #{value_str_prefix} #{values[0..-2].join(', ')} and #{values[-1]}"
|
170
|
+
end
|
176
171
|
end
|
177
172
|
|
178
173
|
"contain #{@referenced_type}[#{@title}]#{value_str}"
|
@@ -191,41 +186,42 @@ module RSpec::Puppet
|
|
191
186
|
end
|
192
187
|
|
193
188
|
def expected
|
194
|
-
@errors.
|
189
|
+
@errors.filter_map { |e| e.expected if e.respond_to?(:expected) }.join("\n\n")
|
195
190
|
end
|
196
191
|
|
197
192
|
def actual
|
198
|
-
@errors.
|
193
|
+
@errors.filter_map { |e| e.actual if e.respond_to?(:actual) }.join("\n\n")
|
199
194
|
end
|
200
195
|
|
201
196
|
private
|
197
|
+
|
202
198
|
def referenced_type(type)
|
203
|
-
type.split('__').map
|
199
|
+
type.split('__').map(&:capitalize).join('::')
|
204
200
|
end
|
205
201
|
|
206
202
|
def errors
|
207
|
-
@errors.empty? ?
|
203
|
+
@errors.empty? ? '' : " with #{@errors.join(', and parameter ')}"
|
208
204
|
end
|
209
205
|
|
210
206
|
def generate_param_list(list, type)
|
211
207
|
output = []
|
212
208
|
list.each do |param, value|
|
213
209
|
if value.nil?
|
214
|
-
output << "#{param
|
210
|
+
output << "#{param} #{type == :not ? 'un' : ''}defined"
|
215
211
|
else
|
216
212
|
a = type == :not ? '!' : '='
|
217
213
|
b = value.is_a?(Regexp) ? '~' : '>'
|
218
|
-
if param.to_s == 'content'
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
214
|
+
output << if (param.to_s == 'content') && value.is_a?(String)
|
215
|
+
"#{param} #{type == :not ? 'not ' : ''} supplied string"
|
216
|
+
else
|
217
|
+
"#{param} #{a}#{b} #{value.inspect}"
|
218
|
+
end
|
223
219
|
end
|
224
220
|
end
|
225
221
|
output
|
226
222
|
end
|
227
223
|
|
228
|
-
def check_befores(
|
224
|
+
def check_befores(_catalogue, resource)
|
229
225
|
@befores.each do |ref|
|
230
226
|
unless precedes?(resource, canonicalize_resource(ref))
|
231
227
|
@errors << BeforeRelationshipError.new(resource.to_ref, ref)
|
@@ -233,7 +229,7 @@ module RSpec::Puppet
|
|
233
229
|
end
|
234
230
|
end
|
235
231
|
|
236
|
-
def check_requires(
|
232
|
+
def check_requires(_catalogue, resource)
|
237
233
|
@requires.each do |ref|
|
238
234
|
unless precedes?(canonicalize_resource(ref), resource)
|
239
235
|
@errors << RequireRelationshipError.new(resource.to_ref, ref)
|
@@ -241,7 +237,7 @@ module RSpec::Puppet
|
|
241
237
|
end
|
242
238
|
end
|
243
239
|
|
244
|
-
def check_notifies(
|
240
|
+
def check_notifies(_catalogue, resource)
|
245
241
|
@notifies.each do |ref|
|
246
242
|
unless notifies?(resource, canonicalize_resource(ref))
|
247
243
|
@errors << NotifyRelationshipError.new(resource.to_ref, ref)
|
@@ -249,7 +245,7 @@ module RSpec::Puppet
|
|
249
245
|
end
|
250
246
|
end
|
251
247
|
|
252
|
-
def check_subscribes(
|
248
|
+
def check_subscribes(_catalogue, resource)
|
253
249
|
@subscribes.each do |ref|
|
254
250
|
unless notifies?(canonicalize_resource(ref), resource)
|
255
251
|
@errors << SubscribeRelationshipError.new(resource.to_ref, ref)
|
@@ -269,13 +265,13 @@ module RSpec::Puppet
|
|
269
265
|
res = resource_from_ref(resource_ref(resource))
|
270
266
|
if res.nil?
|
271
267
|
resource = Struct.new(:type, :title).new(*@catalogue.title_key_for_ref(resource)) if resource.is_a?(String)
|
272
|
-
res = @catalogue.resource_keys.select
|
268
|
+
res = @catalogue.resource_keys.select do |type, _name|
|
273
269
|
type == resource.type
|
274
|
-
|
270
|
+
end.filter_map do |type, name|
|
275
271
|
@catalogue.resource(type, name)
|
276
|
-
|
272
|
+
end.find do |cat_res|
|
277
273
|
cat_res.builtin_type? && cat_res.uniqueness_key.first == resource.title
|
278
|
-
|
274
|
+
end
|
279
275
|
end
|
280
276
|
res
|
281
277
|
end
|
@@ -290,25 +286,23 @@ module RSpec::Puppet
|
|
290
286
|
return results unless resource
|
291
287
|
|
292
288
|
# guard to prevent infinite recursion
|
293
|
-
if visited.include?(resource.object_id)
|
294
|
-
|
295
|
-
|
296
|
-
visited << resource.object_id
|
297
|
-
end
|
289
|
+
return [canonicalize_resource_ref(resource)] if visited.include?(resource.object_id)
|
290
|
+
|
291
|
+
visited << resource.object_id
|
298
292
|
|
299
293
|
Array[resource[type]].flatten.compact.each do |r|
|
300
294
|
results << canonicalize_resource_ref(r)
|
301
295
|
results << relationship_refs(r, type, visited)
|
302
296
|
|
303
297
|
res = canonicalize_resource(r)
|
304
|
-
if res
|
298
|
+
if res&.builtin_type?
|
305
299
|
results << res.to_ref
|
306
300
|
results << "#{res.type.to_s.capitalize}[#{res.uniqueness_key.first}]"
|
307
301
|
end
|
308
302
|
end
|
309
303
|
|
310
304
|
# Add auto* (autorequire etc) if any
|
311
|
-
if [
|
305
|
+
if %i[before notify require subscribe].include?(type)
|
312
306
|
func = "eachauto#{type}".to_sym
|
313
307
|
if resource.resource_type.respond_to?(func)
|
314
308
|
resource.resource_type.send(func) do |t, b|
|
@@ -316,7 +310,7 @@ module RSpec::Puppet
|
|
316
310
|
next if dep.nil?
|
317
311
|
|
318
312
|
res = "#{t.to_s.capitalize}[#{dep}]"
|
319
|
-
if r = relationship_refs(res, type, visited)
|
313
|
+
if (r = relationship_refs(res, type, visited))
|
320
314
|
results << res
|
321
315
|
results << r
|
322
316
|
end
|
@@ -347,7 +341,7 @@ module RSpec::Puppet
|
|
347
341
|
end
|
348
342
|
|
349
343
|
# Nothing found
|
350
|
-
|
344
|
+
false
|
351
345
|
end
|
352
346
|
|
353
347
|
def notifies?(first, second)
|
@@ -358,14 +352,12 @@ module RSpec::Puppet
|
|
358
352
|
notify_refs = relationship_refs(u, :notify)
|
359
353
|
subscribe_refs = relationship_refs(v, :subscribe)
|
360
354
|
|
361
|
-
if notify_refs.include?(v.to_ref) || subscribe_refs.include?(u.to_ref)
|
362
|
-
return true
|
363
|
-
end
|
355
|
+
return true if notify_refs.include?(v.to_ref) || subscribe_refs.include?(u.to_ref)
|
364
356
|
end
|
365
357
|
end
|
366
358
|
|
367
359
|
# Nothing found
|
368
|
-
|
360
|
+
false
|
369
361
|
end
|
370
362
|
|
371
363
|
# @param resource [Hash<Symbol, Object>] The resource in the catalog
|
@@ -375,15 +367,11 @@ module RSpec::Puppet
|
|
375
367
|
list.each do |param, value|
|
376
368
|
param = param.to_sym
|
377
369
|
|
378
|
-
if value.nil?
|
379
|
-
unless resource[param].nil?
|
380
|
-
@errors << "#{param} undefined but it is set to #{resource[param].inspect}"
|
381
|
-
end
|
370
|
+
if value.nil?
|
371
|
+
@errors << "#{param} undefined but it is set to #{resource[param].inspect}" unless resource[param].nil?
|
382
372
|
else
|
383
373
|
m = ParameterMatcher.new(param, value, type)
|
384
|
-
unless m.matches?(resource)
|
385
|
-
@errors.concat m.errors
|
386
|
-
end
|
374
|
+
@errors.concat m.errors unless m.matches?(resource)
|
387
375
|
end
|
388
376
|
end
|
389
377
|
end
|
@@ -1,9 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RSpec::Puppet
|
2
4
|
module ManifestMatchers
|
3
5
|
def method_missing(method, *args, &block)
|
4
|
-
|
5
|
-
|
6
|
+
if /^(create|contain)_/.match?(method.to_s)
|
7
|
+
return RSpec::Puppet::ManifestMatchers::CreateGeneric.new(method, *args,
|
8
|
+
&block)
|
9
|
+
end
|
10
|
+
if /^have_.+_count$/.match?(method.to_s)
|
11
|
+
return RSpec::Puppet::ManifestMatchers::CountGeneric.new(nil, args[0],
|
12
|
+
method)
|
13
|
+
end
|
6
14
|
return RSpec::Puppet::ManifestMatchers::Compile.new if method == :compile
|
15
|
+
|
7
16
|
super
|
8
17
|
end
|
9
18
|
end
|
@@ -11,6 +20,7 @@ module RSpec::Puppet
|
|
11
20
|
module FunctionMatchers
|
12
21
|
def method_missing(method, *args, &block)
|
13
22
|
return RSpec::Puppet::FunctionMatchers::Run.new if method == :run
|
23
|
+
|
14
24
|
super
|
15
25
|
end
|
16
26
|
end
|
@@ -18,6 +28,7 @@ module RSpec::Puppet
|
|
18
28
|
module TypeMatchers
|
19
29
|
def method_missing(method, *args, &block)
|
20
30
|
return RSpec::Puppet::TypeMatchers::CreateGeneric.new(method, *args, &block) if method == :be_valid_type
|
31
|
+
|
21
32
|
super
|
22
33
|
end
|
23
34
|
end
|
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RSpec::Puppet
|
2
4
|
module ManifestMatchers
|
3
5
|
extend RSpec::Matchers::DSL
|
4
6
|
|
5
7
|
matcher :include_class do |expected_class|
|
6
8
|
match do |catalogue|
|
7
|
-
RSpec.deprecate('include_class()', :
|
9
|
+
RSpec.deprecate('include_class()', replacement: 'contain_class()')
|
8
10
|
catalogue.call.classes.include?(expected_class)
|
9
11
|
end
|
10
12
|
|
@@ -13,11 +15,11 @@ module RSpec::Puppet
|
|
13
15
|
end
|
14
16
|
|
15
17
|
if RSpec::Version::STRING < '3'
|
16
|
-
failure_message_for_should do |
|
18
|
+
failure_message_for_should do |_actual|
|
17
19
|
"expected that the catalogue would include Class[#{expected_class}]"
|
18
20
|
end
|
19
21
|
else
|
20
|
-
failure_message do |
|
22
|
+
failure_message do |_actual|
|
21
23
|
"expected that the catalogue would include Class[#{expected_class}]"
|
22
24
|
end
|
23
25
|
end
|
@@ -30,6 +32,5 @@ module RSpec::Puppet
|
|
30
32
|
true
|
31
33
|
end
|
32
34
|
end
|
33
|
-
|
34
35
|
end
|
35
36
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RSpec::Puppet
|
2
4
|
module ManifestMatchers
|
3
5
|
class ParameterMatcher
|
@@ -7,7 +9,9 @@ module RSpec::Puppet
|
|
7
9
|
# @param value [Object] The expected data to match the parameter against
|
8
10
|
# @param type [:should, :not] Whether the given parameter should match
|
9
11
|
def initialize(parameter, value, type)
|
10
|
-
@parameter
|
12
|
+
@parameter = parameter
|
13
|
+
@value = value
|
14
|
+
@type = type
|
11
15
|
|
12
16
|
@should_match = (type == :should)
|
13
17
|
|
@@ -31,9 +35,7 @@ module RSpec::Puppet
|
|
31
35
|
|
32
36
|
retval = check(expected, actual)
|
33
37
|
|
34
|
-
unless retval
|
35
|
-
@errors << MatchError.new(@parameter, expected, actual, !@should_match)
|
36
|
-
end
|
38
|
+
@errors << MatchError.new(@parameter, expected, actual, !@should_match) unless retval
|
37
39
|
|
38
40
|
retval
|
39
41
|
end
|
@@ -53,6 +55,7 @@ module RSpec::Puppet
|
|
53
55
|
# @return [true, false] If the resource matched
|
54
56
|
def check(expected, actual)
|
55
57
|
return false if !expected.is_a?(Proc) && actual.nil? && !expected.nil?
|
58
|
+
|
56
59
|
case expected
|
57
60
|
when Proc
|
58
61
|
check_proc(expected, actual)
|
@@ -84,16 +87,14 @@ module RSpec::Puppet
|
|
84
87
|
# key in the expected hash, there's a stringified key in the actual hash
|
85
88
|
# with a matching value.
|
86
89
|
def check_hash(expected, actual)
|
87
|
-
op = @should_match ? :
|
90
|
+
op = @should_match ? :'==' : :'!='
|
88
91
|
|
89
92
|
unless actual.class.send(op, expected.class)
|
90
93
|
@errors << MatchError.new(@parameter, expected, actual, !@should_match)
|
91
94
|
return false
|
92
95
|
end
|
93
96
|
|
94
|
-
unless expected.keys.size.send(op, actual.keys.size)
|
95
|
-
return false
|
96
|
-
end
|
97
|
+
return false unless expected.keys.size.send(op, actual.keys.size)
|
97
98
|
|
98
99
|
expected.keys.all? do |key|
|
99
100
|
check(expected[key], actual[key])
|
@@ -101,16 +102,14 @@ module RSpec::Puppet
|
|
101
102
|
end
|
102
103
|
|
103
104
|
def check_array(expected, actual)
|
104
|
-
op = @should_match ? :
|
105
|
+
op = @should_match ? :'==' : :'!='
|
105
106
|
|
106
107
|
unless actual.class.send(op, expected.class)
|
107
108
|
@errors << MatchError.new(@parameter, expected, actual, !@should_match)
|
108
109
|
return false
|
109
110
|
end
|
110
111
|
|
111
|
-
unless expected.size.send(op, actual.size)
|
112
|
-
return false
|
113
|
-
end
|
112
|
+
return false unless expected.size.send(op, actual.size)
|
114
113
|
|
115
114
|
(0...expected.size).all? do |index|
|
116
115
|
check(expected[index], actual[index])
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RSpec::Puppet
|
2
4
|
module GenericMatchers
|
3
5
|
# Due to significant code base depending on the
|
@@ -16,7 +18,9 @@ module RSpec::Puppet
|
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
def raise_error(
|
21
|
+
def raise_error(
|
22
|
+
error = defined?(RSpec::Matchers::BuiltIn::RaiseError::UndefinedValue) ? RSpec::Matchers::BuiltIn::RaiseError::UndefinedValue : nil, message = nil, &block
|
23
|
+
)
|
20
24
|
RaiseError.new(error, message, &block)
|
21
25
|
end
|
22
26
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RSpec::Puppet
|
2
4
|
module FunctionMatchers
|
3
5
|
class Run
|
@@ -15,34 +17,33 @@ module RSpec::Puppet
|
|
15
17
|
|
16
18
|
if @has_expected_error
|
17
19
|
if @has_returned
|
18
|
-
|
20
|
+
false
|
19
21
|
elsif @actual_error.is_a?(@expected_error)
|
20
22
|
case @expected_error_message
|
21
23
|
when nil
|
22
|
-
|
24
|
+
true
|
23
25
|
when Regexp
|
24
|
-
|
26
|
+
!(@actual_error.message =~ @expected_error_message).nil?
|
25
27
|
else
|
26
|
-
|
28
|
+
@actual_error.message == @expected_error_message
|
27
29
|
end
|
28
30
|
else # error did not match
|
29
|
-
|
31
|
+
false
|
30
32
|
end
|
31
33
|
elsif @has_expected_return
|
32
|
-
|
33
|
-
|
34
|
+
return false unless @has_returned
|
35
|
+
|
36
|
+
case @expected_return
|
37
|
+
when Regexp
|
38
|
+
!(@actual_return =~ @expected_return).nil?
|
39
|
+
when RSpec::Mocks::ArgumentMatchers::KindOf, RSpec::Matchers::AliasedMatcher
|
40
|
+
@expected_return === @actual_return
|
34
41
|
else
|
35
|
-
|
36
|
-
when Regexp
|
37
|
-
return !!(@actual_return =~ @expected_return)
|
38
|
-
when RSpec::Mocks::ArgumentMatchers::KindOf, RSpec::Matchers::AliasedMatcher
|
39
|
-
return @expected_return === @actual_return
|
40
|
-
else
|
41
|
-
return @actual_return == @expected_return
|
42
|
-
end
|
42
|
+
@actual_return == @expected_return
|
43
43
|
end
|
44
|
+
|
44
45
|
else
|
45
|
-
|
46
|
+
@has_returned
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
@@ -62,30 +63,30 @@ module RSpec::Puppet
|
|
62
63
|
def and_return(value)
|
63
64
|
@has_expected_return = true
|
64
65
|
@expected_return = value
|
65
|
-
if value.is_a? Regexp
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
66
|
+
@desc = if value.is_a? Regexp
|
67
|
+
"match #{value.inspect}"
|
68
|
+
else
|
69
|
+
"return #{value.inspect}"
|
70
|
+
end
|
70
71
|
self
|
71
72
|
end
|
72
73
|
|
73
|
-
def and_raise_error(error_or_message, message=nil)
|
74
|
+
def and_raise_error(error_or_message, message = nil)
|
74
75
|
@has_expected_error = true
|
75
76
|
case error_or_message
|
76
77
|
when String, Regexp
|
77
|
-
@expected_error
|
78
|
+
@expected_error = Exception
|
79
|
+
@expected_error_message = error_or_message
|
78
80
|
else
|
79
|
-
@expected_error
|
81
|
+
@expected_error = error_or_message
|
82
|
+
@expected_error_message = message
|
80
83
|
end
|
81
84
|
|
82
85
|
if @expected_error_message.is_a? Regexp
|
83
86
|
@desc = "raise an #{@expected_error} with the message matching #{@expected_error_message.inspect}"
|
84
87
|
else
|
85
88
|
@desc = "raise an #{@expected_error}"
|
86
|
-
unless @expected_error_message.nil?
|
87
|
-
@desc += "with the message #{@expected_error_message.inspect}"
|
88
|
-
end
|
89
|
+
@desc += "with the message #{@expected_error_message.inspect}" unless @expected_error_message.nil?
|
89
90
|
end
|
90
91
|
self
|
91
92
|
end
|
@@ -115,6 +116,7 @@ module RSpec::Puppet
|
|
115
116
|
end
|
116
117
|
|
117
118
|
private
|
119
|
+
|
118
120
|
def func_name
|
119
121
|
@func_obj.func_name
|
120
122
|
end
|
@@ -132,30 +134,25 @@ module RSpec::Puppet
|
|
132
134
|
else
|
133
135
|
" instead of #{@actual_error.class.inspect}(#{@actual_error})\n#{@actual_error.backtrace.join("\n")}"
|
134
136
|
end
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
" instead of #{@actual_return.inspect}"
|
140
|
-
end
|
137
|
+
elsif @has_expected_error # function has returned
|
138
|
+
" instead of returning #{@actual_return.inspect}"
|
139
|
+
else
|
140
|
+
" instead of #{@actual_return.inspect}"
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
144
|
-
def failure_message_generic(type,
|
145
|
-
message
|
146
|
-
message
|
144
|
+
def failure_message_generic(type, _func_obj)
|
145
|
+
# message is mutable
|
146
|
+
message = +"expected #{func_name}(#{func_params}) to "
|
147
|
+
message << 'not ' if type == :should_not
|
147
148
|
|
148
149
|
if @has_expected_return
|
149
150
|
message << "have returned #{@expected_return.inspect}"
|
151
|
+
elsif @has_expected_error
|
152
|
+
message << "have raised #{@expected_error.inspect}"
|
153
|
+
message << " matching #{@expected_error_message.inspect}" if @expected_error_message
|
150
154
|
else
|
151
|
-
|
152
|
-
message << "have raised #{@expected_error.inspect}"
|
153
|
-
if @expected_error_message
|
154
|
-
message << " matching #{@expected_error_message.inspect}"
|
155
|
-
end
|
156
|
-
else
|
157
|
-
message << "have run successfully"
|
158
|
-
end
|
155
|
+
message << 'have run successfully'
|
159
156
|
end
|
160
157
|
message << failure_message_actual(type)
|
161
158
|
end
|