diff --git a/lib/foodcritic/notifications.rb b/lib/foodcritic/notifications.rb index e9cd2f1c..8e27163a 100644 --- a/lib/foodcritic/notifications.rb +++ b/lib/foodcritic/notifications.rb @@ -64,8 +64,8 @@ def new_style_notification(notify) # Given `notifies :restart, "service[foo]"` the target is the # `"service[foo]"` string portion. - target = notify.xpath('args_add_block/args_add/ - descendant::tstring_content/@value').to_s + target_path = 'args_add_block/args_add/descendant::tstring_content/@value' + target = notify.xpath("arg_paren/#{target_path} | #{target_path}").to_s # Test the target string against the standard syntax for a new-style # notification: `resource_type[resource_name]`. @@ -127,12 +127,13 @@ def notification_action(notify) end def notification_nodes(ast, &block) - ast.xpath('descendant::command[ident/@value="notifies" or - ident/@value="subscribes"]') + type_path = '[ident/@value="notifies" or ident/@value="subscribes"]' + ast.xpath("descendant::command#{type_path} | + descendant::method_add_arg[fcall#{type_path}]") end def notification_type(notify) - notify.xpath('ident/@value[1]').to_s.to_sym + notify.xpath('ident/@value[1] | fcall/ident/@value[1]').to_s.to_sym end def resource_hash_references(ast) diff --git a/spec/foodcritic/api_spec.rb b/spec/foodcritic/api_spec.rb index f75bcd16..80fb3040 100644 --- a/spec/foodcritic/api_spec.rb +++ b/spec/foodcritic/api_spec.rb @@ -554,6 +554,25 @@ def parse_ast(str) }] ) end + it "understands old-style notifications with added parentheses" do + api.notifications(parse_ast(%q{ + template "/etc/nscd.conf" do + source "nscd.conf" + owner "root" + group "root" + notifies(:restart, resources(:service => "nscd")) + end + })).must_equal( + [{ + :type => :notifies, + :action => :restart, + :resource_type => :service, + :resource_name => 'nscd', + :timing => :delayed, + :style => :old + }] + ) + end it "understands the old-style subscriptions" do api.notifications(parse_ast(%q{ template "/etc/nscd.conf" do @@ -573,6 +592,25 @@ def parse_ast(str) }] ) end + it "understands old-style subscriptions with added parentheses" do + api.notifications(parse_ast(%q{ + template "/etc/nscd.conf" do + source "nscd.conf" + owner "root" + group "root" + subscribes(:restart, resources(:service => "nscd")) + end + })).must_equal( + [{ + :type => :subscribes, + :action => :restart, + :resource_type => :service, + :resource_name => 'nscd', + :timing => :delayed, + :style => :old + }] + ) + end it "understands the new-style notifications" do api.notifications(parse_ast(%q{ template "/etc/nscd.conf" do @@ -592,6 +630,25 @@ def parse_ast(str) }] ) end + it "understands new-style notifications with added parentheses" do + api.notifications(parse_ast(%q{ + template "/etc/nscd.conf" do + source "nscd.conf" + owner "root" + group "root" + notifies(:restart, "service[nscd]") + end + })).must_equal( + [{ + :type => :notifies, + :action => :restart, + :resource_type => :service, + :resource_name => 'nscd', + :timing => :delayed, + :style => :new + }] + ) + end it "understands the new-style subscriptions" do api.notifications(parse_ast(%q{ template "/etc/nscd.conf" do @@ -611,6 +668,25 @@ def parse_ast(str) }] ) end + it "understands new-style subscriptions with added parentheses" do + api.notifications(parse_ast(%q{ + template "/etc/nscd.conf" do + source "nscd.conf" + owner "root" + group "root" + subscribes(:restart, "service[nscd]") + end + })).must_equal( + [{ + :type => :subscribes, + :action => :restart, + :resource_type => :service, + :resource_name => 'nscd', + :timing => :delayed, + :style => :new + }] + ) + end describe "supports a resource both notifying and subscribing" do it "old-style notifications" do api.notifications(parse_ast(%q{