nwops / puppet-retrospec

The only tool you need to generate puppet code, tests, modules, facts, types, providers, data and everything else.
https://www.retrospec-puppet.com/
GNU Affero General Public License v3.0
137 stars 27 forks source link

fact_generator.rb:68:in `block in generate_fact_spec_files': undefined method `facts' for nil:NilClass (NoMethodError) #41

Closed djjudas21 closed 8 years ago

djjudas21 commented 8 years ago

I'm hitting a problem with retrospec. When I run it against a Puppet module that contains custom facts, it does this:

[jg4461@puppet-prod puppet-freeradius]$ retrospec puppet
Successfully ran hook: /home/jg4461/.retrospec/repos/retrospec-puppet-templates/clone-hook
Already up-to-date.

Successfully ran hook: /home/jg4461/.retrospec/repos/retrospec-puppet-templates/pre-hook

!! /home/jg4461/git/puppet-freeradius/.bundle/config already exists
!! /home/jg4461/git/puppet-freeradius/.fixtures.yml already exists and differs from template
!! /home/jg4461/git/puppet-freeradius/.git/hooks/pre-commit already exists and differs from template
!! /home/jg4461/git/puppet-freeradius/.gitignore already exists and differs from template
!! /home/jg4461/git/puppet-freeradius/.puppet-lint.rc already exists
!! /home/jg4461/git/puppet-freeradius/.travis.yml already exists
!! /home/jg4461/git/puppet-freeradius/Gemfile already exists
!! /home/jg4461/git/puppet-freeradius/README.markdown already exists
!! /home/jg4461/git/puppet-freeradius/Rakefile already exists
!! /home/jg4461/git/puppet-freeradius/Vagrantfile already exists
 + /home/jg4461/git/puppet-freeradius/files/.gitkeep
!! /home/jg4461/git/puppet-freeradius/spec/shared_contexts.rb already exists and differs from template
!! /home/jg4461/git/puppet-freeradius/spec/spec_helper.rb already exists
 + /home/jg4461/git/puppet-freeradius/templates/.gitkeep
 + /home/jg4461/git/puppet-freeradius/tests/.gitkeep
/usr/local/share/gems/gems/puppet-retrospec-0.11.0/lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb:68:in `block in generate_fact_spec_files': undefined method `facts' for nil:NilClass (NoMethodError)
    from /usr/local/share/gems/gems/puppet-retrospec-0.11.0/lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb:66:in `each'
    from /usr/local/share/gems/gems/puppet-retrospec-0.11.0/lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb:66:in `generate_fact_spec_files'
    from /usr/local/share/gems/gems/puppet-retrospec-0.11.0/lib/retrospec/plugins/v1/plugin/puppet.rb:125:in `fact'
    from /usr/local/share/gems/gems/puppet-retrospec-0.11.0/lib/retrospec/plugins/v1/plugin/puppet.rb:174:in `create_files'
    from /usr/local/share/gems/gems/puppet-retrospec-0.11.0/lib/retrospec/plugins/v1/plugin/puppet.rb:131:in `run'
    from /usr/local/share/gems/gems/puppet-retrospec-0.11.0/lib/retrospec/plugins/v1/plugin/puppet.rb:109:in `run_cli'
    from /usr/local/share/gems/gems/retrospec-0.4.0/lib/retrospec/cli.rb:39:in `run'
    from /usr/local/share/gems/gems/retrospec-0.4.0/bin/retrospec:5:in `<top (required)>'
    from /usr/local/bin/retrospec:23:in `load'
    from /usr/local/bin/retrospec:23:in `<main>'

Basic version numbers:

[jg4461@puppet-prod ~]$ ruby -v
ruby 2.0.0p598 (2014-11-13) [x86_64-linux]

[jg4461@puppet-prod ~]$ gem list puppet-retrospec

*** LOCAL GEMS ***
puppet-retrospec (0.11.0)

[jg4461@puppet-prod ~]$ yum info rubygem-rspec.noarch
Installed Packages
Name        : rubygem-rspec
Arch        : noarch
Version     : 2.14.1
Release     : 1.el7
Size        : 158 k
Repo        : installed
From repo   : epel
Summary     : Behaviour driven development (BDD) framework for Ruby
URL         : http://rspec.info
Licence     : MIT
Description : RSpec is a behaviour driven development (BDD) framework for Ruby.

I'm not experienced at writing Ruby. Any hints on debugging this? Thanks.

logicminds commented 8 years ago

@djjudas21 is this your fact code? https://github.com/djjudas21/puppet-freeradius/blob/master/lib/facter/freeradius_maj_version.rb

Looking at the code it would seem retrospec is choking on your facter code because it cannot evaluate your code which has conditional logic around the Facter.add block which I have never seen before and would suggest you use a confine statement instead of what your currently doing. Its my understanding that if a fact evaluates to nil that fact is not added. Additionally I think nil is returned if Facter::Core::Execution.exec fails so there doesn't seem to be a need for the conditional block in your code.

About the only thing I can do is just fail with a better message

logicminds commented 8 years ago

It it wasn't clean, just remove the condition from your facter code and re-run retrospec and it will generate some fact unit tests for you.

djjudas21 commented 8 years ago

Thanks, that's helpful. Like I said I don't know Ruby well, and I tried to write that fact in a kinda-sorta-Perl way, which is probably a bad idea.