Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using provides with LWRPs fails #2552

Closed
jaym opened this issue Dec 6, 2014 · 3 comments · Fixed by #2554
Closed

Using provides with LWRPs fails #2552

jaym opened this issue Dec 6, 2014 · 3 comments · Fixed by #2554
Assignees

Comments

@jaym
Copy link
Contributor

jaym commented Dec 6, 2014

Modify spec/data/lwrp/resource/bar.rb to bo

provides 'lwrp_bar'
actions :pass_buck, :prepare_eyes, :watch_paint_dry

When it is loaded:

     Failure/Error: Chef::Resource::LWRPBase.build_from_file("lwrp", file, @run_context)
     NoMethodError:
       undefined method `split' for nil:NilClass
     # ./lib/chef/mixin/convert_to_class_name.rb:88:in `constantize'
     # ./lib/chef/resource.rb:788:in `provides'
     # ./spec/data/lwrp/resources/bar.rb:1:in `class_from_file'
     # ./lib/chef/mixin/from_file.rb:42:in `class_eval'
     # ./lib/chef/mixin/from_file.rb:42:in `class_from_file'
     # ./lib/chef/resource/lwrp_base.rb:51:in `build_from_file'
     # ./spec/unit/lwrp_spec.rb:271:in `block (4 levels) in <top (required)>'
     # ./spec/unit/lwrp_spec.rb:270:in `each'
     # ./spec/unit/lwrp_spec.rb:270:in `block (3 levels) in <top (required)>'
@tyler-ball
Copy link
Contributor

I'm trying to understand the cause of this - when in resource.rb:788, self refers to the LWRPBase class, correct (because the spec called LWRPBase.build_from_file)? So how is self.name nil?

@jaym
Copy link
Contributor Author

jaym commented Dec 6, 2014

self.name is actually referring to this thingy: https://github.com/opscode/chef/pull/2554/files#diff-88e9968725547cea59d31e1cde26d2b9R47, which is initially nil.
Doing the const_set makes it not nil through some ruby meta-programming magic I don't quite understand yet.
Here's an example I did to convince myself that is where things happen:

[6] pry(main)> a = Class.new(Rugged::Blame)
=> #<Class:0x00000001bcd288>
[7] pry(main)> a.name
=> nil
[8] pry(main)> Rugged::Blame.const_set("Foo", a)
=> Rugged::Blame::Foo
[9] pry(main)> a.name
=> "Rugged::Blame::Foo"

@jaym jaym closed this as completed in #2554 Dec 8, 2014
jaym added a commit that referenced this issue Dec 8, 2014
@tyler-ball
Copy link
Contributor

From Metaprogramming Ruby 2:

Ruby is cheating a little here. When you assign an anonymous class to a constant, Ruby understands that you're trying to give a name to the class...

@chef chef locked and limited conversation to collaborators Nov 16, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants