[chef] Re: Re: Re: Re: Re: Custom LWRP undefined local variable or method `new_resource'


Chronological Thread 
  • From: Bryan Baugher < >
  • To:
  • Subject: [chef] Re: Re: Re: Re: Re: Custom LWRP undefined local variable or method `new_resource'
  • Date: Thu, 5 Jan 2012 15:07:03 -0600

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,

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 < " target="_blank"> > 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
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 < " target="_blank"> > 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, < " target="_blank"> (mailto: " target="_blank"> )> 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



Archive powered by MHonArc 2.6.16.

§