Module: PuppetSpec::Files

Defined in:
core_modules/host_core/spec/lib/puppet_spec/files.rb,
core_modules/mount_core/spec/lib/puppet_spec/files.rb,
core_modules/nagios_core/spec/lib/puppet_spec/files.rb,
core_modules/sshkeys_core/spec/lib/puppet_spec/files.rb,
core_modules/mailalias_core/spec/lib/puppet_spec/files.rb

Overview

A support module for testing files.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cleanupObject



9
10
11
12
13
14
# File 'core_modules/host_core/spec/lib/puppet_spec/files.rb', line 9

def self.cleanup
  until @global_tempfiles.empty?
    path = @global_tempfiles.pop
    FileUtils.rm_rf path, secure: true
  end
end

.dir_contained_in(dir, contents_hash) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'core_modules/mount_core/spec/lib/puppet_spec/files.rb', line 77

def dir_contained_in(dir, contents_hash)
  contents_hash.each do |k, v|
    if v.is_a?(Hash)
      Dir.mkdir(tmp = File.join(dir, k))
      dir_contained_in(tmp, v)
    else
      file = File.join(dir, k)
      File.open(file, 'wb') { |f| f.write(v) }
    end
  end
  dir
end

.dir_containing(name, contents_hash) ⇒ Object



73
74
75
# File 'core_modules/mount_core/spec/lib/puppet_spec/files.rb', line 73

def dir_containing(name, contents_hash)
  dir_contained_in(tmpdir(name), contents_hash)
end

.expect_file_mode(file, mode) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'core_modules/mount_core/spec/lib/puppet_spec/files.rb', line 95

def expect_file_mode(file, mode)
  actual_mode = '%o' % Puppet::FileSystem.stat(file).mode
  target_mode = if Puppet.features.microsoft_windows?
                  mode
                else
                  '10' + '%04i' % mode.to_i
                end
  expect(actual_mode).to eq(target_mode)
end

.file_containing(name, contents) ⇒ Object



33
34
35
36
37
# File 'core_modules/mount_core/spec/lib/puppet_spec/files.rb', line 33

def file_containing(name, contents)
  file = tmpfile(name)
  File.open(file, 'wb') { |f| f.write(contents) }
  file
end

.make_absolute(path) ⇒ Object



19
20
21
22
23
# File 'core_modules/mount_core/spec/lib/puppet_spec/files.rb', line 19

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

.make_tmpname(prefix, suffix, n) ⇒ Object

Copied from ruby 2.4 source



27
28
29
30
31
32
33
34
35
36
37
# File 'core_modules/host_core/spec/lib/puppet_spec/files.rb', line 27

def make_tmpname((prefix, suffix), n)
  prefix = (String.try_convert(prefix) ||
            raise(ArgumentError, "unexpected prefix: #{prefix.inspect}"))
  suffix &&= (String.try_convert(suffix) ||
              raise(ArgumentError, "unexpected suffix: #{suffix.inspect}"))
  t = Time.now.strftime('%Y%m%d')
  path = "#{prefix}#{t}-#{$PROCESS_ID}-#{rand(0x100000000).to_s(36)}".dup
  path << "-#{n}" if n
  path << suffix if suffix
  path
end

.record_tmp(tmp) ⇒ Object



39
40
41
42
# File 'core_modules/host_core/spec/lib/puppet_spec/files.rb', line 39

def self.record_tmp(tmp)
  # ...record it for cleanup,
  @global_tempfiles << tmp
end

.script_containing(name, contents) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'core_modules/mount_core/spec/lib/puppet_spec/files.rb', line 39

def script_containing(name, contents)
  file = tmpfile(name)
  if Puppet.features.microsoft_windows?
    file += '.bat'
    text = contents[:windows]
  else
    text = contents[:posix]
  end
  File.open(file, 'wb') { |f| f.write(text) }
  Puppet::FileSystem.chmod(0o755, file)
  file
end

.tmpdir(name) ⇒ Object



52
53
54
55
56
57
58
# File 'core_modules/mount_core/spec/lib/puppet_spec/files.rb', line 52

def tmpdir(name)
  dir = Puppet::FileSystem.expand_path(Dir.mktmpdir(name).encode!(Encoding::UTF_8))

  PuppetSpec::Files.record_tmp(dir)

  dir
end

.tmpfile(name, dir = nil) ⇒ Object



18
19
20
21
22
23
24
# File 'core_modules/host_core/spec/lib/puppet_spec/files.rb', line 18

def tmpfile(name, dir = nil)
  dir ||= Dir.tmpdir
  path = Puppet::FileSystem.expand_path(make_tmpname(name, nil).encode(Encoding::UTF_8), dir)
  PuppetSpec::Files.record_tmp(File.expand_path(path))

  path
end

Instance Method Details

#expect_file_mode(file, mode) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'core_modules/mount_core/spec/lib/puppet_spec/files.rb', line 95

def expect_file_mode(file, mode)
  actual_mode = '%o' % Puppet::FileSystem.stat(file).mode
  target_mode = if Puppet.features.microsoft_windows?
                  mode
                else
                  '10' + '%04i' % mode.to_i
                end
  expect(actual_mode).to eq(target_mode)
end