@@ -20,6 +20,8 @@ def initialize(*args, &block)
20
20
@subscribes = [ ]
21
21
@requires = [ ]
22
22
@befores = [ ]
23
+ @tagged = [ ]
24
+ @not_tagged = [ ]
23
25
end
24
26
25
27
def with ( *args , &block )
@@ -60,6 +62,16 @@ def that_comes_before(resource)
60
62
self
61
63
end
62
64
65
+ def tagged ( resource )
66
+ @tagged . concat ( Array ( resource ) )
67
+ self
68
+ end
69
+
70
+ def not_tagged ( resource )
71
+ @not_tagged . concat ( Array ( resource ) )
72
+ self
73
+ end
74
+
63
75
def method_missing ( method , *args , &block )
64
76
if method . to_s =~ /^with_/
65
77
param = method . to_s . gsub ( /^with_/ , '' )
@@ -114,6 +126,8 @@ def matches?(catalogue)
114
126
115
127
check_params ( rsrc_hsh , @expected_params , :should ) if @expected_params . any?
116
128
check_params ( rsrc_hsh , @expected_undef_params , :not ) if @expected_undef_params . any?
129
+ check_tags ( resource , @tagged , :should ) if @tagged . any?
130
+ check_tags ( resource , @not_tagged , :not ) if @not_tagged . any?
117
131
check_befores ( @catalogue , resource ) if @befores . any?
118
132
check_requires ( @catalogue , resource ) if @requires . any?
119
133
check_notifies ( @catalogue , resource ) if @notifies . any?
@@ -132,8 +146,8 @@ def failure_message_when_negated
132
146
end
133
147
134
148
def description
149
+ tests = [ ]
135
150
values = [ ]
136
- value_str_prefix = "with"
137
151
138
152
if @expected_params_count
139
153
values << "exactly #{ @expected_params_count } parameters"
@@ -148,34 +162,39 @@ def description
148
162
end
149
163
150
164
if @notifies . any?
151
- value_str_prefix = "that notifies"
152
- values = @notifies
165
+ tests << english_list ( "that notifies" , @notifies )
153
166
end
154
167
155
168
if @subscribes . any?
156
- value_str_prefix = "that subscribes to"
157
- values = @subscribes
169
+ tests << english_list ( "that subscribes to" , @subscribes )
158
170
end
159
171
160
172
if @requires . any?
161
- value_str_prefix = "that requires"
162
- values = @requires
173
+ tests << english_list ( "that requires" , @requires )
163
174
end
164
175
165
176
if @befores . any?
166
- value_str_prefix = "that comes before"
167
- values = @befores
177
+ tests << english_list ( "that comes before" , @befores )
178
+ end
179
+
180
+ if @tagged . any?
181
+ tests << english_list ( "that is tagged" , @tagged )
182
+ end
183
+
184
+ if @not_tagged . any?
185
+ tests << english_list ( "that is not tagged" , @not_tagged , "nor" )
168
186
end
169
187
170
188
unless values . empty?
171
- if values . length == 1
172
- value_str = " #{ value_str_prefix } #{ values . first } "
173
- else
174
- value_str = " #{ value_str_prefix } #{ values [ 0 ..-2 ] . join ( ", " ) } and #{ values [ -1 ] } "
175
- end
189
+ tests << english_list ( "with" , values )
176
190
end
177
191
178
- "contain #{ @referenced_type } [#{ @title } ]#{ value_str } "
192
+ tests_str = ""
193
+ unless tests . empty?
194
+ tests_str = english_list ( "" , tests , "and" , true )
195
+ end
196
+
197
+ "contain #{ @referenced_type } [#{ @title } ]#{ tests_str } "
179
198
end
180
199
181
200
def diffable?
@@ -225,6 +244,16 @@ def generate_param_list(list, type)
225
244
output
226
245
end
227
246
247
+ def english_list ( value_str_prefix , values , joiner = 'and' , oxford_comma = false )
248
+ if values . length == 1
249
+ "#{ value_str_prefix } #{ values . first } "
250
+ elsif oxford_comma
251
+ "#{ value_str_prefix } #{ values [ 0 ..-2 ] . join ( ", " ) } , #{ joiner } #{ values [ -1 ] } "
252
+ else
253
+ "#{ value_str_prefix } #{ values [ 0 ..-2 ] . join ( ", " ) } #{ joiner } #{ values [ -1 ] } "
254
+ end
255
+ end
256
+
228
257
def check_befores ( catalogue , resource )
229
258
@befores . each do |ref |
230
259
unless precedes? ( resource , canonicalize_resource ( ref ) )
@@ -382,6 +411,25 @@ def check_params(resource, list, type)
382
411
end
383
412
end
384
413
end
414
+ # @param resource [Puppet::Resource] The resource in the catalog
415
+ # @param list [Array<String>] The expected tags for the resource
416
+ # @param type [:should, :not] Whether the given tags should/not be present
417
+ def check_tags ( resource , list , type )
418
+ case type
419
+ when :should
420
+ list . each do |tag |
421
+ unless resource . tags . include? tag
422
+ @errors << "#{ tag } is not set"
423
+ end
424
+ end
425
+ when :not
426
+ list . each do |tag |
427
+ if resource . tags . include? tag
428
+ @errors << "#{ tag } is set"
429
+ end
430
+ end
431
+ end
432
+ end
385
433
end
386
434
end
387
435
end
0 commit comments