]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/spec/lib/puppet_spec/files.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / modules / stdlib / spec / lib / puppet_spec / files.rb
1 require 'fileutils'
2 require 'tempfile'
3
4 # A support module for testing files.
5 module PuppetSpec::Files
6   # This code exists only to support tests that run as root, pretty much.
7   # Once they have finally been eliminated this can all go... --daniel 2011-04-08
8   if Puppet.features.posix? then
9     def self.in_tmp(path)
10       path =~ /^\/tmp/ or path =~ /^\/var\/folders/
11     end
12   elsif Puppet.features.microsoft_windows?
13     def self.in_tmp(path)
14       tempdir = File.expand_path(File.join(Dir::LOCAL_APPDATA, "Temp"))
15       path =~ /^#{tempdir}/
16     end
17   else
18     fail "Help! Can't find in_tmp for this platform"
19   end
20
21   def self.cleanup
22     $global_tempfiles ||= []
23     while path = $global_tempfiles.pop do
24       fail "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path)
25
26       begin
27         FileUtils.rm_r path, :secure => true
28       rescue Errno::ENOENT
29         # nothing to do
30       end
31     end
32   end
33
34   def tmpfile(name)
35     # Generate a temporary file, just for the name...
36     source = Tempfile.new(name)
37     path = source.path
38     source.close!
39
40     # ...record it for cleanup,
41     $global_tempfiles ||= []
42     $global_tempfiles << File.expand_path(path)
43
44     # ...and bam.
45     path
46   end
47
48   def tmpdir(name)
49     path = tmpfile(name)
50     FileUtils.mkdir_p(path)
51     path
52   end
53 end