Skip to content
This repository has been archived by the owner on Sep 19, 2020. It is now read-only.

Commit

Permalink
Match notifications with parentheses.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Crump committed Jul 28, 2012
1 parent b25d95c commit f4e58a1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/foodcritic/notifications.rb
Expand Up @@ -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]`.
Expand Down Expand Up @@ -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)
Expand Down
76 changes: 76 additions & 0 deletions spec/foodcritic/api_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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{
Expand Down

0 comments on commit f4e58a1

Please sign in to comment.