class RSpec::Puppet::Adapters::Base

Public Instance Methods

catalog(node, exported) click to toggle source
# File lib/rspec-puppet/adapters.rb, line 79
def catalog(node, exported)
  if exported
    # Use the compiler directly to skip the filtering done by the indirector
    Puppet::Parser::Compiler.compile(node).filter { |r| !r.exported? }
  else
    Puppet::Resource::Catalog.indirection.find(node.name, :use_node => node)
  end
end
current_environment() click to toggle source
# File lib/rspec-puppet/adapters.rb, line 88
def current_environment
  Puppet::Node::Environment.new(@environment_name)
end
get_setting(example_group, rspec_setting) click to toggle source
# File lib/rspec-puppet/adapters.rb, line 71
def get_setting(example_group, rspec_setting)
  if example_group.respond_to?(rspec_setting)
    example_group.send(rspec_setting)
  else
    RSpec.configuration.send(rspec_setting)
  end
end
manifest() click to toggle source

@return [String, nil] The path to the Puppet manifest if it is present and set, nil otherwise.

# File lib/rspec-puppet/adapters.rb, line 105
def manifest
  Puppet[:manifest]
end
modulepath() click to toggle source
# File lib/rspec-puppet/adapters.rb, line 100
def modulepath
  Puppet[:modulepath].split(File::PATH_SEPARATOR)
end
settings_map() click to toggle source
# File lib/rspec-puppet/adapters.rb, line 92
def settings_map
  [
    [:modulepath, :module_path],
    [:config, :config],
    [:confdir, :confdir],
  ]
end
setup_puppet(example_group) click to toggle source

Set up all Puppet settings applicable for this Puppet version as application defaults.

Puppet setting values can be taken from the global RSpec configuration, or from the currently executing RSpec context. When a setting is specified both in the global configuration and in the example group, the setting in the example group is preferred.

@example Configuring a Puppet setting from a global RSpec configuration value

RSpec.configure do |config|
  config.parser = "future"
end
# => Puppet[:parser] will be future

@example Configuring a Puppet setting from within an RSpec example group

RSpec.describe 'my_module::my_class', :type => :class do
  let(:module_path) { "/Users/luke/modules" }
  #=> Puppet[:modulepath] will be "/Users/luke/modules"
end

@example Configuring a Puppet setting with both a global RSpec configuration and local context

RSpec.configure do |config|
  config.confdir = "/etc/puppet"
end
RSpec.describe 'my_module', :type => :class do
  # Puppet[:confdir] will be "/etc/puppet"
end
RSpec.describe 'my_module::my_class', :type => :class do
  let(:confdir) { "/etc/puppetlabs/puppet" }
  # => Puppet[:confdir] will be "/etc/puppetlabs/puppet" in this example group
end
RSpec.describe 'my_module::my_define', :type => :define do
  # Puppet[:confdir] will be "/etc/puppet" again
end

@param example_group [RSpec::Core::ExampleGroup] The RSpec context to use for local settings @return [void]

# File lib/rspec-puppet/adapters.rb, line 41
def setup_puppet(example_group)
  settings = settings_map.map do |puppet_setting, rspec_setting|
    [puppet_setting, get_setting(example_group, rspec_setting)]
  end.flatten
  default_hash = {:confdir => '/dev/null', :vardir => '/dev/null' }
  if defined?(Puppet::Test::TestHelper) && Puppet::Test::TestHelper.respond_to?(:app_defaults_for_tests, true)
    default_hash.merge!(Puppet::Test::TestHelper.send(:app_defaults_for_tests))
  end
  settings_hash = default_hash.merge(Hash[*settings])
  settings_hash.inject(settings_hash) { |h, (k, v)| h[k] = (v == '/dev/null') ? 'c:/nul/' : v; h } if Gem.win_platform?

  if Puppet.settings.respond_to?(:initialize_app_defaults)
    Puppet.settings.initialize_app_defaults(settings_hash)

    # Forcefully apply the environmentpath setting instead of relying on
    # the application defaults as Puppet::Test::TestHelper automatically
    # sets this value as well, overriding our application default
    Puppet.settings[:environmentpath] = settings_hash[:environmentpath] if settings_hash.key?(:environmentpath)
  else
    # Set settings the old way for Puppet 2.x, because that's how
    # they're defaulted in that version of Puppet::Test::TestHelper and
    # we won't be able to override them otherwise.
    settings_hash.each do |setting, value|
      Puppet.settings[setting] = value
    end
  end

  @environment_name = example_group.environment
end