Skip to content

Commit

Permalink
(MAINT) Update module and class style
Browse files Browse the repository at this point in the history
  • Loading branch information
chelnak committed Jan 25, 2023
1 parent 8a816ec commit ca4e730
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 151 deletions.
84 changes: 43 additions & 41 deletions lib/puppetlabs_spec_helper/puppetlabs_spec/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,58 @@
require 'tempfile'
require 'pathname'

# A support module for testing files.
module PuppetlabsSpec::Files
# This code exists only to support tests that run as root, pretty much.
# Once they have finally been eliminated this can all go... --daniel 2011-04-08
def self.in_tmp(path)
tempdir = Dir.tmpdir

Pathname.new(path).ascend do |dir|
return true if File.identical?(tempdir, dir)
end
module PuppetlabsSpec
# A support module for testing files.
module Files
# This code exists only to support tests that run as root, pretty much.
# Once they have finally been eliminated this can all go... --daniel 2011-04-08
def self.in_tmp(path)
tempdir = Dir.tmpdir

Pathname.new(path).ascend do |dir|
return true if File.identical?(tempdir, dir)
end

false
end
false
end

def self.cleanup
$global_tempfiles ||= []
while (path = $global_tempfiles.pop)
raise "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path)
def self.cleanup
$global_tempfiles ||= []
while (path = $global_tempfiles.pop)
raise "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path)

begin
FileUtils.rm_r path, secure: true
rescue Errno::ENOENT
# nothing to do
begin
FileUtils.rm_r path, secure: true
rescue Errno::ENOENT
# nothing to do
end
end
end
end

def make_absolute(path)
path = File.expand_path(path)
path[0] = 'c' if Puppet.features.microsoft_windows?
path
end
def make_absolute(path)
path = File.expand_path(path)
path[0] = 'c' if Puppet.features.microsoft_windows?
path
end

def tmpfilename(name)
# Generate a temporary file, just for the name...
source = Tempfile.new(name)
path = source.path
source.close!
def tmpfilename(name)
# Generate a temporary file, just for the name...
source = Tempfile.new(name)
path = source.path
source.close!

# ...record it for cleanup,
$global_tempfiles ||= []
$global_tempfiles << File.expand_path(path)
# ...record it for cleanup,
$global_tempfiles ||= []
$global_tempfiles << File.expand_path(path)

# ...and bam.
path
end
# ...and bam.
path
end

def tmpdir(name)
path = tmpfilename(name)
FileUtils.mkdir_p(path)
path
def tmpdir(name)
path = tmpfilename(name)
FileUtils.mkdir_p(path)
path
end
end
end
84 changes: 43 additions & 41 deletions lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
# frozen_string_literal: true

# This module provides some helper methods to assist with fixtures. It's
# methods are designed to help when you have a conforming fixture layout so we
# get project consistency.
module PuppetlabsSpec::Fixtures
# Returns the joined path of the global FIXTURE_DIR plus any path given to it
def fixtures(*rest)
File.join(PuppetlabsSpec::FIXTURE_DIR, *rest)
end

# Returns the path to your relative fixture dir. So if your spec test is
# <project>/spec/unit/facter/foo_spec.rb then your relative dir will be
# <project>/spec/fixture/unit/facter/foo
def my_fixture_dir
callers = caller
while (line = callers.shift)
next unless (found = line.match(%r{/spec/(.*)_spec\.rb:}))

return fixtures(found[1])
module PuppetlabsSpec
# This module provides some helper methods to assist with fixtures. It's
# methods are designed to help when you have a conforming fixture layout so we
# get project consistency.
module Fixtures
# Returns the joined path of the global FIXTURE_DIR plus any path given to it
def fixtures(*rest)
File.join(PuppetlabsSpec::FIXTURE_DIR, *rest)
end
raise "sorry, I couldn't work out your path from the caller stack!"
end

# Given a name, returns the full path of a file from your relative fixture
# dir as returned by my_fixture_dir.
def my_fixture(name)
file = File.join(my_fixture_dir, name)
unless File.readable? file
raise "fixture '#{name}' for #{my_fixture_dir} is not readable"
# Returns the path to your relative fixture dir. So if your spec test is
# <project>/spec/unit/facter/foo_spec.rb then your relative dir will be
# <project>/spec/fixture/unit/facter/foo
def my_fixture_dir
callers = caller
while (line = callers.shift)
next unless (found = line.match(%r{/spec/(.*)_spec\.rb:}))

return fixtures(found[1])
end
raise "sorry, I couldn't work out your path from the caller stack!"
end

file
end
# Given a name, returns the full path of a file from your relative fixture
# dir as returned by my_fixture_dir.
def my_fixture(name)
file = File.join(my_fixture_dir, name)
unless File.readable? file
raise "fixture '#{name}' for #{my_fixture_dir} is not readable"
end

# Return the contents of the file using read when given a name. Uses
# my_fixture to work out the relative path.
def my_fixture_read(name)
File.read(my_fixture(name))
end
file
end

# Provides a block mechanism for iterating across the files in your fixture
# area.
def my_fixtures(glob = '*', flags = 0, &block)
files = Dir.glob(File.join(my_fixture_dir, glob), flags)
if files.empty?
raise "fixture '#{glob}' for #{my_fixture_dir} had no files!"
# Return the contents of the file using read when given a name. Uses
# my_fixture to work out the relative path.
def my_fixture_read(name)
File.read(my_fixture(name))
end

block && files.each(&block)
files
# Provides a block mechanism for iterating across the files in your fixture
# area.
def my_fixtures(glob = '*', flags = 0, &block)
files = Dir.glob(File.join(my_fixture_dir, glob), flags)
if files.empty?
raise "fixture '#{glob}' for #{my_fixture_dir} had no files!"
end

block && files.each(&block)
files
end
end
end
66 changes: 34 additions & 32 deletions lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,43 @@
# 'puppetlabs_spec_helper/puppet_spec_helper' library
require 'puppetlabs_spec_helper/puppet_spec_helper'

# PuppetInternals provides a set of methods that interface
# with internal puppet implementations.
module PuppetlabsSpec::PuppetInternals
def resource(parts = {})
resource_type = parts[:type] || :hostclass
resource_name = parts[:name] || 'testing'
Puppet::Resource::Type.new(resource_type, resource_name)
end
module_function :resource
module PuppetlabsSpec
# PuppetInternals provides a set of methods that interface
# with internal puppet implementations.
module PuppetInternals
def resource(parts = {})
resource_type = parts[:type] || :hostclass
resource_name = parts[:name] || 'testing'
Puppet::Resource::Type.new(resource_type, resource_name)
end
module_function :resource

def compiler(parts = {})
compiler_node = parts[:node] || node
Puppet::Parser::Compiler.new(compiler_node)
end
module_function :compiler
def compiler(parts = {})
compiler_node = parts[:node] || node
Puppet::Parser::Compiler.new(compiler_node)
end
module_function :compiler

def node(parts = {})
node_name = parts[:name] || 'testinghost'
options = parts[:options] || {}
node_environment = Puppet::Node::Environment.create(parts[:environment] || 'test', [])
options[:environment] = node_environment
Puppet::Node.new(node_name, options)
end
module_function :node
def node(parts = {})
node_name = parts[:name] || 'testinghost'
options = parts[:options] || {}
node_environment = Puppet::Node::Environment.create(parts[:environment] || 'test', [])
options[:environment] = node_environment
Puppet::Node.new(node_name, options)
end
module_function :node

# Return a method instance for a given function. This is primarily useful
# for rspec-puppet
def function_method(name, parts = {})
scope = parts[:scope] || scope()
# Ensure the method instance is defined by side-effect of checking if it
# exists. This is a hack, but at least it's a hidden hack and not an
# exposed hack.
return nil unless Puppet::Parser::Functions.function(name)
# Return a method instance for a given function. This is primarily useful
# for rspec-puppet
def function_method(name, parts = {})
scope = parts[:scope] || scope()
# Ensure the method instance is defined by side-effect of checking if it
# exists. This is a hack, but at least it's a hidden hack and not an
# exposed hack.
return nil unless Puppet::Parser::Functions.function(name)

scope.method("function_#{name}".to_sym)
scope.method("function_#{name}".to_sym)
end
module_function :function_method
end
module_function :function_method
end
78 changes: 41 additions & 37 deletions lib/puppetlabs_spec_helper/tasks/check_symlinks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,52 @@

require 'pathspec'

# Helpers for validating symlinks.
class PuppetlabsSpecHelper::Tasks::CheckSymlinks
DEFAULT_IGNORED = [
'/.git/',
'/.bundle/',
'/vendor/',
].freeze

IGNORE_LIST_FILES = [
'.pdkignore',
'.gitignore',
].freeze

def check(dir = Dir.pwd)
dir = Pathname.new(dir) unless dir.is_a?(Pathname)
results = []

dir.each_child(true) do |child|
next if ignored?(child.to_s)

if child.symlink?
results << child
elsif child.directory? && child.basename.to_s !~ %r{^(\.git|\.?bundle)$}
results.concat(check(child))
module PuppetlabsSpecHelper
module Tasks
# Helpers for validating symlinks.
class CheckSymlinks
DEFAULT_IGNORED = [
'/.git/',
'/.bundle/',
'/vendor/',
].freeze

IGNORE_LIST_FILES = [
'.pdkignore',
'.gitignore',
].freeze

def check(dir = Dir.pwd)
dir = Pathname.new(dir) unless dir.is_a?(Pathname)
results = []

dir.each_child(true) do |child|
next if ignored?(child.to_s)

if child.symlink?
results << child
elsif child.directory? && child.basename.to_s !~ %r{^(\.git|\.?bundle)$}
results.concat(check(child))
end
end

results
end
end

results
end

def ignored?(path)
path = "#{path}/" if File.directory?(path)
def ignored?(path)
path = "#{path}/" if File.directory?(path)

!ignore_pathspec.match_paths([path], Dir.pwd).empty?
end
!ignore_pathspec.match_paths([path], Dir.pwd).empty?
end

def ignore_pathspec
@ignore_pathspec ||= PathSpec.new(DEFAULT_IGNORED).tap do |pathspec|
IGNORE_LIST_FILES.each do |f|
next unless File.file?(f) && File.readable?(f)
def ignore_pathspec
@ignore_pathspec ||= PathSpec.new(DEFAULT_IGNORED).tap do |pathspec|
IGNORE_LIST_FILES.each do |f|
next unless File.file?(f) && File.readable?(f)

File.open(f, 'r') { |fd| pathspec.add(fd) }
File.open(f, 'r') { |fd| pathspec.add(fd) }
end
end
end
end
end
Expand Down

0 comments on commit ca4e730

Please sign in to comment.