Custom LWRP undefined local variable or method `new_resource'


#1

I created a custom resource that tries to mimic ruby_block’s block. This
resource was for Rspec and I wanted to follow the typical style of writing
rspec with the format,

rspec “myApp” do
it “should …” do

end
end

So I have written my resource as follows,

//

def initialize(*args)
super
@action = :describe
end

def it(s, &it)
if s
should(s)
end

if block_given? and it
@it = it
else
@it
end
end

actions :describe

attribute :name, :kind_of => String, :name_attribute => true
attribute :should, :kind_of => String, :default => “RSpec Test”

\

And my provider as,

//

action :describe do
run_context.include_recipe “rspec”

ruby_block “Rspec:#{new_resource.name}” do
block do
require ‘rspec/autorun’

  describe new_resource.name do
it new_resource.should do
  new_resource.it.call
end
  end 
end

end

new_resource.updated_by_last_action(true)
end

\

Chef is telling me that new_resource is undefined in my provider at,

‘it new_resource.should do’

Any thoughts?

Stacktrace:
Generated at Wed Jan 04 11:46:02 -0600 2012
NameError: ruby_block[Rspec:Tomcat]
(/var/cache/chef/cookbooks/rspec/providers/default.rb line 5) had an error:
NameError: undefined local variable or method new_resource' for #<Class:0x2b5af4c31610> /var/cache/chef/cookbooks/rspec/providers/default.rb:10:inclass_from_file’
/usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:154:in
module_eval' /usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:154:insubclass’
/usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:141:in
describe' /usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/dsl.rb:5:indescribe’
/var/cache/chef/cookbooks/rspec/providers/default.rb:9:in class_from_file' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/provider/ruby_block.rb:28:incall’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/provider/ruby_block.rb:28:in
action_create' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource.rb:440:insend’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource.rb:440:in
run_action' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:45:inrun_action’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/runner.rb:81:in
converge' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:81:ineach’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/runner.rb:81:in
converge' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection.rb:94:inexecute_each_resource’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:116:in
call' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection/stepable_iterator.rb:116:incall_iterator_block’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:85:in
step' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection/stepable_iterator.rb:104:initerate’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:55:in
each_with_index' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection.rb:92:inexecute_each_resource’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/runner.rb:76:in
converge' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/client.rb:312:inconverge’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/client.rb:160:in
run' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/application/client.rb:239:inrun_application’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/application/client.rb:229:in
loop' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/application/client.rb:229:inrun_application’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/application.rb:67:in
run' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/chef-client:26 /usr/bin/chef-client:19:inload’
/usr/bin/chef-client:19


#2

try @new_resource … it’s an instance variable.

On Wed, Jan 4, 2012 at 12:45 PM, bjbq4d@gmail.com wrote:

I created a custom resource that tries to mimic ruby_block’s block. This
resource was for Rspec and I wanted to follow the typical style of writing
rspec with the format,

rspec “myApp” do
it “should …” do

end
end

So I have written my resource as follows,

//

def initialize(*args)
super
@action = :describe
end

def it(s, &it)
if s
should(s)
end

if block_given? and it
@it = it
else
@it
end
end

actions :describe

attribute :name, :kind_of => String, :name_attribute => true
attribute :should, :kind_of => String, :default => “RSpec Test”

\

And my provider as,

//

action :describe do
run_context.include_recipe “rspec”

ruby_block “Rspec:#{new_resource.name}” do
block do
require ‘rspec/autorun’

 describe new_resource.name do
   it new_resource.should do
     new_resource.it.call
   end
 end

end
end

new_resource.updated_by_last_action(true)
end

\

Chef is telling me that new_resource is undefined in my provider at,

‘it new_resource.should do’

Any thoughts?

Stacktrace:
Generated at Wed Jan 04 11:46:02 -0600 2012
NameError: ruby_block[Rspec:Tomcat]
(/var/cache/chef/cookbooks/rspec/providers/default.rb line 5) had an error:
NameError: undefined local variable or method new_resource' for #<Class:0x2b5af4c31610> /var/cache/chef/cookbooks/rspec/providers/default.rb:10:inclass_from_file’

/usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:154:in
`module_eval’

/usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:154:in
`subclass’

/usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:141:in
describe' /usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/dsl.rb:5:indescribe’
/var/cache/chef/cookbooks/rspec/providers/default.rb:9:in `class_from_file’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/provider/ruby_block.rb:28:in
`call’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/provider/ruby_block.rb:28:in
`action_create’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource.rb:440:in
`send’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource.rb:440:in
run_action' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:45:inrun_action’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/runner.rb:81:in
converge' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:81:ineach’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/runner.rb:81:in
`converge’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection.rb:94:in
`execute_each_resource’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:116:in
`call’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:116:in
`call_iterator_block’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:85:in
`step’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:104:in
`iterate’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:55:in
`each_with_index’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection.rb:92:in
execute_each_resource' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:76:inconverge’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/client.rb:312:in
converge' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/client.rb:160:inrun’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/application/client.rb:239:in
`run_application’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/application/client.rb:229:in
`loop’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/application/client.rb:229:in
`run_application’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/application.rb:67:in
run' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/chef-client:26 /usr/bin/chef-client:19:inload’
/usr/bin/chef-client:19


#3

Using @new_resource gives me a ‘undefined method `name’ for nil:NilClass’
at ‘describe @new_resource.name do’. I don’t think the ruby_block likes the
@new_resource.

On Wed, Jan 4, 2012 at 2:35 PM, andi abes andi.abes@gmail.com wrote:

try @new_resource … it’s an instance variable.

On Wed, Jan 4, 2012 at 12:45 PM, bjbq4d@gmail.com wrote:

I created a custom resource that tries to mimic ruby_block’s block. This
resource was for Rspec and I wanted to follow the typical style of writing
rspec with the format,

rspec “myApp” do
it “should …” do

end
end

So I have written my resource as follows,

//

def initialize(*args)
super
@action = :describe
end

def it(s, &it)
if s
should(s)
end

if block_given? and it
@it = it
else
@it
end
end

actions :describe

attribute :name, :kind_of => String, :name_attribute => true
attribute :should, :kind_of => String, :default => “RSpec Test”

\

And my provider as,

//

action :describe do
run_context.include_recipe “rspec”

ruby_block “Rspec:#{new_resource.name}” do
block do
require ‘rspec/autorun’

 describe new_resource.name do
   it new_resource.should do
     new_resource.it.call
   end
 end

end
end

new_resource.updated_by_last_action(true)
end

\

Chef is telling me that new_resource is undefined in my provider at,

‘it new_resource.should do’

Any thoughts?

Stacktrace:
Generated at Wed Jan 04 11:46:02 -0600 2012
NameError: ruby_block[Rspec:Tomcat]
(/var/cache/chef/cookbooks/rspec/providers/default.rb line 5) had an
error:
NameError: undefined local variable or method new_resource' for #<Class:0x2b5af4c31610> /var/cache/chef/cookbooks/rspec/providers/default.rb:10:inclass_from_file’

/usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:154:in
`module_eval’

/usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:154:in
`subclass’

/usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:141:in
describe' /usr/lib64/ruby/gems/1.8/gems/rspec-core-2.7.1/lib/rspec/core/dsl.rb:5:indescribe’
/var/cache/chef/cookbooks/rspec/providers/default.rb:9:in
`class_from_file’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/provider/ruby_block.rb:28:in
`call’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/provider/ruby_block.rb:28:in
`action_create’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource.rb:440:in
`send’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource.rb:440:in
run_action' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:45:inrun_action’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/runner.rb:81:in
converge' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:81:ineach’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/runner.rb:81:in
`converge’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection.rb:94:in
`execute_each_resource’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:116:in
`call’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:116:in
`call_iterator_block’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:85:in
`step’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:104:in
`iterate’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection/stepable_iterator.rb:55:in
`each_with_index’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/resource_collection.rb:92:in
execute_each_resource' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:76:inconverge’
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/client.rb:312:in
converge' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/client.rb:160:inrun’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/application/client.rb:239:in
`run_application’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/application/client.rb:229:in
`loop’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/application/client.rb:229:in
`run_application’

/usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/…/lib/chef/application.rb:67:in
run' /usr/lib64/ruby/gems/1.8/gems/chef-0.10.8/bin/chef-client:26 /usr/bin/chef-client:19:inload’
/usr/bin/chef-client:19


-Bryan


#4

On Wednesday, January 4, 2012 at 12:35 PM, andi abes wrote:

try @new_resource … it’s an instance variable.

On Wed, Jan 4, 2012 at 12:45 PM, <bjbq4d@gmail.com (mailto:bjbq4d@gmail.com)> wrote:

I created a custom resource that tries to mimic ruby_block’s block. This
resource was for Rspec and I wanted to follow the typical style of writing
rspec with the format,

rspec “myApp” do
it “should …” do

end
end

So I have written my resource as follows,

//

def initialize(*args)
super
@action = :describe
end

def it(s, &it)
if s
should(s)
end

if block_given? and it
@it = it
else
@it
end
end

actions :describe

attribute :name, :kind_of => String, :name_attribute => true
attribute :should, :kind_of => String, :default => “RSpec Test”

\

And my provider as,

//

action :describe do
run_context.include_recipe “rspec”

ruby_block “Rspec:#{new_resource.name (http://new_resource.name)}” do
block do
require ‘rspec/autorun’

describe new_resource.name (http://new_resource.name) do
it new_resource.should do
new_resource.it.call
end
end
end
end

new_resource.updated_by_last_action(true)
end

\

Chef is telling me that new_resource is undefined in my provider at,

‘it new_resource.should do’

Any thoughts?

Probably RSpec is changing the execution context. Throw a puts self.inspect in there to see.


Dan DeLeo


#5

Yes, you are right. ‘puts self.inspect’ gives '#Class:0x2b2aba647728
inside the describe block, where as outside ‘put self.inspect’ gives a
large blob of chef info.

I am still pretty new to ruby, is there a way to pass new_resource into
this block?

On Wed, Jan 4, 2012 at 4:05 PM, Daniel DeLeo dan@kallistec.com wrote:

On Wednesday, January 4, 2012 at 12:35 PM, andi abes wrote:

try @new_resource … it’s an instance variable.

On Wed, Jan 4, 2012 at 12:45 PM, <bjbq4d@gmail.com (mailto:
bjbq4d@gmail.com)> wrote:

I created a custom resource that tries to mimic ruby_block’s block.
This

resource was for Rspec and I wanted to follow the typical style of
writing

rspec with the format,

rspec “myApp” do
it “should …” do

end
end

So I have written my resource as follows,

//

def initialize(*args)
super
@action = :describe
end

def it(s, &it)
if s
should(s)
end

if block_given? and it
@it = it
else
@it
end
end

actions :describe

attribute :name, :kind_of => String, :name_attribute => true
attribute :should, :kind_of => String, :default => “RSpec Test”

\

And my provider as,

//

action :describe do
run_context.include_recipe “rspec”

ruby_block “Rspec:#{new_resource.name (http://new_resource.name)}” do
block do
require ‘rspec/autorun’

describe new_resource.name (http://new_resource.name) do
it new_resource.should do
new_resource.it.call
end
end
end
end

new_resource.updated_by_last_action(true)
end

\

Chef is telling me that new_resource is undefined in my provider at,

‘it new_resource.should do’

Any thoughts?

Probably RSpec is changing the execution context. Throw a puts self.inspect in there to see.


Dan DeLeo


-Bryan


#6

Why use a ruby block in your action? The action code already executes in the converge phase. You could try to use ruby’s ’ eval’ method directly to call into rspec

On Jan 4, 2012, at 5:16 PM, Bryan Baugher bjbq4d@gmail.com wrote:

Yes, you are right. ‘puts self.inspect’ gives ‘#Class:0x2b2aba647728’ inside the describe block, where as outside ‘put self.inspect’ gives a large blob of chef info.

I am still pretty new to ruby, is there a way to pass new_resource into this block?

On Wed, Jan 4, 2012 at 4:05 PM, Daniel DeLeo dan@kallistec.com wrote:

On Wednesday, January 4, 2012 at 12:35 PM, andi abes wrote:

try @new_resource … it’s an instance variable.

On Wed, Jan 4, 2012 at 12:45 PM, <bjbq4d@gmail.com (mailto:bjbq4d@gmail.com)> wrote:

I created a custom resource that tries to mimic ruby_block’s block. This
resource was for Rspec and I wanted to follow the typical style of writing
rspec with the format,

rspec “myApp” do
it “should …” do

end
end

So I have written my resource as follows,

//

def initialize(*args)
super
@action = :describe
end

def it(s, &it)
if s
should(s)
end

if block_given? and it
@it = it
else
@it
end
end

actions :describe

attribute :name, :kind_of => String, :name_attribute => true
attribute :should, :kind_of => String, :default => “RSpec Test”

\

And my provider as,

//

action :describe do
run_context.include_recipe “rspec”

ruby_block “Rspec:#{new_resource.name (http://new_resource.name)}” do
block do
require ‘rspec/autorun’

describe new_resource.name (http://new_resource.name) do
it new_resource.should do
new_resource.it.call
end
end
end
end

new_resource.updated_by_last_action(true)
end

\

Chef is telling me that new_resource is undefined in my provider at,

‘it new_resource.should do’

Any thoughts?

Probably RSpec is changing the execution context. Throw a puts self.inspect in there to see.


Dan DeLeo


-Bryan


#7

I am using a ruby_block because I don’t want the rspec require or testing
to occur before the ‘run_context.include_recipe “rspec”’ as that will
ensure that rspec is installed.

That helped me get pointed in the right direction, but didn’t quite work
for me since I could not turn my proc into ruby code.

I found doing this works almost works,

block = new_resource.it
should = new_resource.should

describe new_resource.name do
it should do
Rspec.class_exec{ block.call }
end
end

But the block still doesn’t know about Rspec, giving a ‘undefined method
exception for raise_error’ a Rspec method.

On Thu, Jan 5, 2012 at 8:20 AM, Andiabes andi.abes@gmail.com wrote:

Why use a ruby block in your action? The action code already executes in
the converge phase. You could try to use ruby’s ’ eval’ method directly to
call into rspec

On Jan 4, 2012, at 5:16 PM, Bryan Baugher bjbq4d@gmail.com wrote:

Yes, you are right. ‘puts self.inspect’ gives '#Class:0x2b2aba647728
inside the describe block, where as outside ‘put self.inspect’ gives a
large blob of chef info.

I am still pretty new to ruby, is there a way to pass new_resource into
this block?

On Wed, Jan 4, 2012 at 4:05 PM, Daniel DeLeo dan@kallistec.com wrote:

On Wednesday, January 4, 2012 at 12:35 PM, andi abes wrote:

try @new_resource … it’s an instance variable.

On Wed, Jan 4, 2012 at 12:45 PM, <bjbq4d@gmail.com (mailto:
bjbq4d@gmail.com)> wrote:

I created a custom resource that tries to mimic ruby_block’s block.
This

resource was for Rspec and I wanted to follow the typical style of
writing

rspec with the format,

rspec “myApp” do
it “should …” do

end
end

So I have written my resource as follows,

//

def initialize(*args)
super
@action = :describe
end

def it(s, &it)
if s
should(s)
end

if block_given? and it
@it = it
else
@it
end
end

actions :describe

attribute :name, :kind_of => String, :name_attribute => true
attribute :should, :kind_of => String, :default => “RSpec Test”

\

And my provider as,

//

action :describe do
run_context.include_recipe “rspec”

ruby_block “Rspec:#{new_resource.name (http://new_resource.name)}” do
block do
require ‘rspec/autorun’

describe new_resource.name (http://new_resource.name) do
it new_resource.should do
new_resource.it.call
end
end
end
end

new_resource.updated_by_last_action(true)
end

\

Chef is telling me that new_resource is undefined in my provider at,

‘it new_resource.should do’

Any thoughts?

Probably RSpec is changing the execution context. Throw a puts self.inspect in there to see.


Dan DeLeo


-Bryan


-Bryan


#8

Have a look at this, sounds like you are re-implementing some of the
stuff that I did for the MiniTest cookbook:

https://github.com/fujin/minitest-cookbook/blob/develop/cookbooks/minitest/providers/unit_testcase.rb#L38-48

Cheers,

AJ

On 6 January 2012 10:07, Bryan Baugher bjbq4d@gmail.com wrote:

I am using a ruby_block because I don’t want the rspec require or testing to
occur before the ‘run_context.include_recipe “rspec”’ as that will ensure
that rspec is installed.

That helped me get pointed in the right direction, but didn’t quite work for
me since I could not turn my proc into ruby code.

I found doing this works almost works,

block = new_resource.it
should = new_resource.should

describe new_resource.name do
it should do
Rspec.class_exec{ block.call }
end
end

But the block still doesn’t know about Rspec, giving a ‘undefined method
exception for raise_error’ a Rspec method.

On Thu, Jan 5, 2012 at 8:20 AM, Andiabes andi.abes@gmail.com wrote:

Why use a ruby block in your action? The action code already executes in
the converge phase. You could try to use ruby’s ’ eval’ method directly to
call into rspec

On Jan 4, 2012, at 5:16 PM, Bryan Baugher bjbq4d@gmail.com wrote:

Yes, you are right. ‘puts self.inspect’ gives '#Class:0x2b2aba647728
inside the describe block, where as outside ‘put self.inspect’ gives a large
blob of chef info.

I am still pretty new to ruby, is there a way to pass new_resource into
this block?

On Wed, Jan 4, 2012 at 4:05 PM, Daniel DeLeo dan@kallistec.com wrote:

On Wednesday, January 4, 2012 at 12:35 PM, andi abes wrote:

try @new_resource … it’s an instance variable.

On Wed, Jan 4, 2012 at 12:45 PM, <bjbq4d@gmail.com
(mailto:bjbq4d@gmail.com)> wrote:

I created a custom resource that tries to mimic ruby_block’s block.
This
resource was for Rspec and I wanted to follow the typical style of
writing
rspec with the format,

rspec “myApp” do
it “should …” do

end
end

So I have written my resource as follows,

//

def initialize(*args)
super
@action = :describe
end

def it(s, &it)
if s
should(s)
end

if block_given? and it
@it = it
else
@it
end
end

actions :describe

attribute :name, :kind_of => String, :name_attribute => true
attribute :should, :kind_of => String, :default => “RSpec Test”

\

And my provider as,

//

action :describe do
run_context.include_recipe “rspec”

ruby_block “Rspec:#{new_resource.name (http://new_resource.name)}” do
block do
require ‘rspec/autorun’

describe new_resource.name (http://new_resource.name) do
it new_resource.should do
new_resource.it.call
end
end
end
end

new_resource.updated_by_last_action(true)
end

\

Chef is telling me that new_resource is undefined in my provider at,

‘it new_resource.should do’

Any thoughts?

Probably RSpec is changing the execution context. Throw a puts self.inspect in there to see.


Dan DeLeo


-Bryan


-Bryan