Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Include blocks in resource_attributes, refs #23.

  • Loading branch information...
commit dee7822746c3fe56592e5d1986388ee1fa263eb1 1 parent 6ffd0e2
@acrmp authored
Showing with 80 additions and 0 deletions.
  1. +7 −0 lib/foodcritic/api.rb
  2. +73 −0 spec/foodcritic/api_spec.rb
View
7 lib/foodcritic/api.rb
@@ -224,6 +224,13 @@ def resource_attributes(resource)
end
atts[att.xpath('string(ident/@value)')] = att_value
end
+ resource.xpath("do_block/descendant::method_add_block[
+ count(ancestor::do_block) = 1][brace_block | do_block]").each do |batt|
+ att_name = batt.xpath('string(method_add_arg/fcall/ident/@value)')
+ if att_name and ! att_name.empty? and batt.children.length > 1
+ atts[att_name] = batt.children[1]
+ end
+ end
atts
end
View
73 spec/foodcritic/api_spec.rb
@@ -325,6 +325,79 @@ def xpath(str)
resource = MiniTest::Mock.new.expect :xpath, [], [String]
api.resource_attributes(resource).must_equal({})
end
+ describe "block attributes" do
+ def str_to_atts(str)
+ ast = api.send(:build_xml, Ripper::SexpBuilder.new(str).parse)
+ api.resource_attributes(api.find_resources(ast).first)
+ end
+ it "includes attributes with brace block values in the result" do
+ atts = str_to_atts(%q{
+ file "/etc/foo" do
+ mode "0600"
+ action :create
+ only_if { File.exists?("/etc/bar") }
+ end
+ })
+ atts['only_if'].wont_be_nil
+ atts['only_if'].must_respond_to :xpath
+ atts['only_if'].name.must_equal 'brace_block'
+ end
+ it "includes attributes with do block values in the result" do
+ atts = str_to_atts(%q{
+ file "/etc/foo" do
+ mode "0600"
+ action :create
+ only_if do
+ !File.exists?(foo) || (File.exists?(bar) &&
+ File.mtime(baz) < last_changedate)
+ end
+ end
+ })
+ atts['only_if'].wont_be_nil
+ atts['only_if'].must_respond_to :xpath
+ atts['only_if'].name.must_equal 'do_block'
+ end
+ it "supports multiple block attributes" do
+ atts = str_to_atts(%q{
+ file "/etc/foo" do
+ mode "0600"
+ action :create
+ only_if { false }
+ not_if { true }
+ end
+ })
+ atts['only_if'].wont_be_nil
+ atts['only_if'].must_respond_to :xpath
+ atts['only_if'].name.must_equal 'brace_block'
+ atts['not_if'].wont_be_nil
+ atts['not_if'].must_respond_to :xpath
+ atts['not_if'].name.must_equal 'brace_block'
+ end
+ it "includes top-level blocks only" do
+ atts = str_to_atts(%q{
+ ruby_block "example" do
+ block do
+ foo do |bar|
+ Chef::Log.info(bar)
+ end
+ end
+ only_if { true }
+ not_if { false }
+ end
+ })
+ atts.keys.wont_include 'foo'
+ atts['block'].wont_be_nil
+ atts['block'].must_respond_to :xpath
+ atts['block'].name.must_equal 'do_block'
+ atts['only_if'].wont_be_nil
+ atts['only_if'].must_respond_to :xpath
+ atts['only_if'].name.must_equal 'brace_block'
+ atts['not_if'].wont_be_nil
+ atts['not_if'].must_respond_to :xpath
+ atts['not_if'].name.must_equal 'brace_block'
+ end
+
+ end
end
describe "#resource_attributes_by_type" do
Please sign in to comment.
Something went wrong with that request. Please try again.