class RSpecPuppetSupport

class to prepare spec directory for rspec puppet testing

Public Class Methods

dependency_setup() click to toggle source

setup the module dependencies for rspec-puppet testing

# File lib/puppet-check/rspec_puppet_support.rb, line 61
def self.dependency_setup
  private_class_method :method
  require 'json'

  # parse the metadata.json (assumes DataParser.json has already given it a pass)
  parsed = JSON.parse(File.read('metadata.json'))

  # grab dependencies if they exist
  return unless parsed.key?('dependencies')
  parsed['dependencies'].each do |dependency_hash|
    # determine how the user wants to download the module dependency
    if dependency_hash.key?('git')
      git(dependency_hash['git'], dependency_hash['args'])
    elsif dependency_hash.key?('forge')
      forge(dependency_hash['forge'], dependency_hash['args'])
    elsif dependency_hash.key?('svn')
      svn(dependency_hash['svn'], dependency_hash['args'])
    elsif dependency_hash.key?('hg')
      hg(dependency_hash['hg'], dependency_hash['args'])
    else
      warn "#{dependency_hash['name']} has an unspecified, or specified but unsupported, download method."
    end
  end
  Process.waitall
end
file_setup(module_name) click to toggle source

setup the files, directories, and symlinks for rspec-puppet testing

# File lib/puppet-check/rspec_puppet_support.rb, line 32
def self.file_setup(module_name)
  private_class_method :method
  # create all the necessary fixture dirs that are missing
  ['spec/fixtures', 'spec/fixtures/manifests', 'spec/fixtures/modules'].each do |dir|
    Dir.mkdir(dir) unless File.directory?(dir)
  end

  # create empty site.pp if missing
  File.write('spec/fixtures/manifests/site.pp', '') unless File.file?('spec/fixtures/manifests/site.pp')

  # symlink the module into spec/fixtures/modules
  if File.exist?("spec/fixtures/modules/#{module_name}")
    # check if target is a symlink
    if File.symlink?("spec/fixtures/modules/#{module_name}")
      # check if target is correct
      warn "spec/fixtures/modules/#{module_name} is not a symlink to the correct source! Your tests may fail because of this!" unless File.readlink("spec/fixtures/modules/#{module_name}") == File.expand_path("../../../../#{module_name}")
    else
      warn "spec/fixtures/modules/#{module_name} is not a symlink! Your tests may fail because of this!"
    end
  else
    File.symlink("../../../../#{module_name}", "spec/fixtures/modules/#{module_name}")
  end

  # create spec_helper if missing
  return if File.file?('spec/spec_helper.rb')
  File.open('spec/spec_helper.rb', 'w') { |file| file.puts "require 'rspec-puppet/spec_helper'\n" }
end
forge(forge_name, args = '') click to toggle source

download external module dependency with forge

# File lib/puppet-check/rspec_puppet_support.rb, line 101
def self.forge(forge_name, args = '')
  private_class_method :method
  # is the module present? do an upgrade; otherwise, do an install
  subcommand = File.directory?("spec/fixtures/modules/#{forge_name}") ? 'upgrade' : 'install'
  spawn("puppet module #{subcommand} --modulepath spec/fixtures/modules/ #{args} #{forge_name}")
end
git(git_url, args = '') click to toggle source

download external module dependency with git

# File lib/puppet-check/rspec_puppet_support.rb, line 88
def self.git(git_url, args = '')
  private_class_method :method
  # establish path to clone module to
  path = "spec/fixtures/modules/#{File.basename(git_url, '.git')}"
  # is the module present and already cloned with git? do a pull; otherwise, do a clone
  begin
    File.directory?("#{path}/.git") ? spawn("git -C #{path} pull") : spawn("git clone #{args} #{git_url} #{path}")
  rescue Errno::ENOENT
    warn 'git is not installed and cannot be used to retrieve dependency modules'
  end
end
hg(hg_url, args = '') click to toggle source

download external module dependency with hg

# File lib/puppet-check/rspec_puppet_support.rb, line 122
def self.hg(hg_url, args = '')
  private_class_method :method
  # establish path to clone module to
  path = "spec/fixtures/modules/#{File.basename(hg_url)}"
  # is the module present and already cloned with hg? do a pull and update; otherwise do a clone
  begin
    File.directory?("#{path}/.hg") ? spawn("hg --cwd #{path} pull; hg --cwd #{path} update") : spawn("hg clone #{args} #{hg_url} #{path}")
  rescue Errno::ENOENT
    warn 'mercurial is not installed and cannot be used to retrieve dependency modules'
  end
end
run() click to toggle source

prepare the spec fixtures directory for rspec-puppet testing

# File lib/puppet-check/rspec_puppet_support.rb, line 10
def self.run
  # ensure this method does not do anything inside module dependencies
  specdirs = Dir.glob('**/spec').reject { |dir| dir =~ /fixtures/ }
  return if specdirs.empty?

  # setup fixtures for rspec-puppet testing
  specdirs.each do |specdir|
    # skip to next specdir if it does not seem like a puppet module
    next unless File.directory?("#{specdir}/../manifests")

    # change to module directory
    Dir.chdir("#{specdir}/..")

    # grab the module name from the directory name of the module to pass to file_setup
    file_setup(File.basename(Dir.pwd))

    # invoke dependency_setup for module dependencies if metadata.json present
    dependency_setup if File.file?('metadata.json')
  end
end
svn(svn_url, args = '') click to toggle source

download external module dependency with svn

# File lib/puppet-check/rspec_puppet_support.rb, line 109
def self.svn(svn_url, args = '')
  private_class_method :method
  # establish path to checkout module to
  path = "spec/fixtures/modules/#{File.basename(svn_url)}"
  # is the module present and already checked out with svn? do an update; otherwise, do a checkout
  begin
    File.directory?("#{path}/.svn") ? spawn("svn update #{path}") : spawn("svn co #{args} #{svn_url} #{path}")
  rescue Errno::ENOENT
    warn 'subversion is not installed and cannot be used to retrieve dependency modules'
  end
end