@@ -20,6 +20,8 @@ def initialize(*args, &block)
2020 @subscribes = [ ]
2121 @requires = [ ]
2222 @befores = [ ]
23+ @tagged = [ ]
24+ @not_tagged = [ ]
2325 end
2426
2527 def with ( *args , &block )
@@ -60,6 +62,16 @@ def that_comes_before(resource)
6062 self
6163 end
6264
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+
6375 def method_missing ( method , *args , &block )
6476 if method . to_s =~ /^with_/
6577 param = method . to_s . gsub ( /^with_/ , '' )
@@ -114,6 +126,8 @@ def matches?(catalogue)
114126
115127 check_params ( rsrc_hsh , @expected_params , :should ) if @expected_params . any?
116128 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?
117131 check_befores ( @catalogue , resource ) if @befores . any?
118132 check_requires ( @catalogue , resource ) if @requires . any?
119133 check_notifies ( @catalogue , resource ) if @notifies . any?
@@ -132,8 +146,8 @@ def failure_message_when_negated
132146 end
133147
134148 def description
149+ tests = [ ]
135150 values = [ ]
136- value_str_prefix = "with"
137151
138152 if @expected_params_count
139153 values << "exactly #{ @expected_params_count } parameters"
@@ -148,34 +162,39 @@ def description
148162 end
149163
150164 if @notifies . any?
151- value_str_prefix = "that notifies"
152- values = @notifies
165+ tests << english_list ( "that notifies" , @notifies )
153166 end
154167
155168 if @subscribes . any?
156- value_str_prefix = "that subscribes to"
157- values = @subscribes
169+ tests << english_list ( "that subscribes to" , @subscribes )
158170 end
159171
160172 if @requires . any?
161- value_str_prefix = "that requires"
162- values = @requires
173+ tests << english_list ( "that requires" , @requires )
163174 end
164175
165176 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" )
168186 end
169187
170188 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 )
176190 end
177191
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 } "
179198 end
180199
181200 def diffable?
@@ -225,6 +244,16 @@ def generate_param_list(list, type)
225244 output
226245 end
227246
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+
228257 def check_befores ( catalogue , resource )
229258 @befores . each do |ref |
230259 unless precedes? ( resource , canonicalize_resource ( ref ) )
@@ -385,6 +414,25 @@ def check_params(resource, list, type)
385414 end
386415 end
387416 end
417+ # @param resource [Puppet::Resource] The resource in the catalog
418+ # @param list [Array<String>] The expected tags for the resource
419+ # @param type [:should, :not] Whether the given tags should/not be present
420+ def check_tags ( resource , list , type )
421+ case type
422+ when :should
423+ list . each do |tag |
424+ unless resource . tags . include? tag
425+ @errors << "#{ tag } is not set"
426+ end
427+ end
428+ when :not
429+ list . each do |tag |
430+ if resource . tags . include? tag
431+ @errors << "#{ tag } is set"
432+ end
433+ end
434+ end
435+ end
388436 end
389437 end
390438end
0 commit comments