diff options
author | Ashley Penney <ashley.penney@puppetlabs.com> | 2014-03-18 12:34:40 -0400 |
---|---|---|
committer | Ashley Penney <ashley.penney@puppetlabs.com> | 2014-03-18 12:34:40 -0400 |
commit | fec943f441f0c6ab8d6b685091d3418ec0e265b4 (patch) | |
tree | 144aed26b9fb50790708c5b221afdeb32bc03186 /spec/lib/puppet_spec/files.rb | |
parent | 326a8fd801ecba11005189c10ca8749872ef6577 (diff) | |
parent | 9aa28f1c100d7703aa1bb9fbe2bcf02bf781b486 (diff) | |
download | puppet-stdlib-fec943f441f0c6ab8d6b685091d3418ec0e265b4.tar.gz puppet-stdlib-fec943f441f0c6ab8d6b685091d3418ec0e265b4.tar.bz2 |
Merge pull request #231 from apenney/32-testing
Numerous changes to update testing gems.
Diffstat (limited to 'spec/lib/puppet_spec/files.rb')
-rwxr-xr-x | spec/lib/puppet_spec/files.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb new file mode 100755 index 0000000..65b04aa --- /dev/null +++ b/spec/lib/puppet_spec/files.rb @@ -0,0 +1,60 @@ +require 'fileutils' +require 'tempfile' +require 'tmpdir' +require 'pathname' + +# A support module for testing files. +module PuppetSpec::Files + def self.cleanup + $global_tempfiles ||= [] + while path = $global_tempfiles.pop do + begin + Dir.unstub(:entries) + FileUtils.rm_rf path, :secure => true + rescue Errno::ENOENT + # nothing to do + end + end + end + + def make_absolute(path) PuppetSpec::Files.make_absolute(path) end + def self.make_absolute(path) + path = File.expand_path(path) + path[0] = 'c' if Puppet.features.microsoft_windows? + path + end + + def tmpfile(name, dir = nil) PuppetSpec::Files.tmpfile(name, dir) end + def self.tmpfile(name, dir = nil) + # Generate a temporary file, just for the name... + source = dir ? Tempfile.new(name, dir) : Tempfile.new(name) + path = source.path + source.close! + + record_tmp(File.expand_path(path)) + + path + end + + def file_containing(name, contents) PuppetSpec::Files.file_containing(name, contents) end + def self.file_containing(name, contents) + file = tmpfile(name) + File.open(file, 'wb') { |f| f.write(contents) } + file + end + + def tmpdir(name) PuppetSpec::Files.tmpdir(name) end + def self.tmpdir(name) + dir = Dir.mktmpdir(name) + + record_tmp(dir) + + dir + end + + def self.record_tmp(tmp) + # ...record it for cleanup, + $global_tempfiles ||= [] + $global_tempfiles << tmp + end +end |