rodjek / rspec-puppet

RSpec tests for your Puppet manifests
http://rspec-puppet.com
MIT License
361 stars 202 forks source link

monkey-patch match_manifests to not load init.pp when not necessary #784

Closed kjetilho closed 4 years ago

kjetilho commented 4 years ago

When loading a class, Puppet::Module::match_manifests is used to list candidate filenames. To maintain backwards compatibility, it will return init.pp for the module first in the list, so that a class may be declared outside autoload namespace (Puppet bug #4220).

Normally, this does not cause any problems, but if pre_condition is used to make a simplified version of the top class in your module, Puppet will complain about a duplicate declaration.

  describe "baseconfig::logging" do
    let(:pre_condition) { [
     'class baseconfig($enable_filebeat=false) { }',
     'include baseconfig',
    ] }

This results in an error like this:

error during compilation: Class 'baseconfig' is already defined (line: 2); cannot redefine (file: /home/kjetilho/src/baseconfig/spec/fixtures/modules/baseconfig/manifests/init.pp, line: 6) on node ranger.ms.redpill-linpro.com

The patch changes the logic so that it removes init.pp from the beginning of the list if there are other files in the list. This will break code which puts the class foo::bar in foo/manifests/init.pp and has a file foo/manifests/bar.pp in the project, but I believe such code thoroughly deserves to break.

coveralls commented 4 years ago

Coverage Status

Coverage increased (+0.06%) to 85.469% when pulling 3431ba627f4d04909d7104278a9468fef0f71ecf on kjetilho:avoid_autoload_init_pp into e586247b9a066f83bbed6caae96074265fb9f0cd on rodjek:master.

kjetilho commented 4 years ago

the Travis CI failure seems to be a problem in the bleeding edge Puppet gem, not related to my patch. how can I rerun the check?

rodjek commented 4 years ago

@kjetilho Can you add a test case for this?

kjetilho commented 4 years ago

hmm, not sure how to do this. do you mean the basic Ruby behaviour of the method (in spec/unit/monkey_patches_spec.rb) or the puppet test cases?

I will note that

kjetilho commented 4 years ago

ah, you cherry-picked it, thanks! well, there's my suggested test case for it, I guess you can cherry-pick that, too, if it is the kind of testing you wanted.