Module: PuppetlabsSpec::Files

Defined in:
lib/puppetlabs_spec_helper/puppetlabs_spec/files.rb

Overview

A support module for testing files.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cleanupObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/files.rb', line 19

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
    end
  end
end

.in_tmp(path) ⇒ Object

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



9
10
11
12
13
14
15
16
17
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/files.rb', line 9

def self.in_tmp(path)
  tempdir = Dir.tmpdir

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

  false
end

Instance Method Details

#make_absolute(path) ⇒ Object



32
33
34
35
36
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/files.rb', line 32

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

#tmpdir(name) ⇒ Object



52
53
54
55
56
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/files.rb', line 52

def tmpdir(name)
  path = tmpfilename(name)
  FileUtils.mkdir_p(path)
  path
end

#tmpfilename(name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/files.rb', line 38

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)

  # ...and bam.
  path
end