aboutsummaryrefslogtreecommitdiff
path: root/spec/lib/puppet_spec
diff options
context:
space:
mode:
authorJosh Cooper <josh@puppet.com>2018-07-13 09:38:02 -0700
committerGitHub <noreply@github.com>2018-07-13 09:38:02 -0700
commitbd20d920a19e9b011dfd027a802985cea46ae04f (patch)
treef882cd2b7c31ae08cd242882b0f7f1d1ed89cbbd /spec/lib/puppet_spec
parent9d96ddecea6367abee1aa77859848b0c158fca80 (diff)
parentc957642d70f4b778736a9b49cf1ef9702e42c4f1 (diff)
downloadpuppet-hosts_core-bd20d920a19e9b011dfd027a802985cea46ae04f.tar.gz
puppet-hosts_core-bd20d920a19e9b011dfd027a802985cea46ae04f.tar.bz2
Merge pull request #1 from puppetlabs/extraction
Initial host module extraction
Diffstat (limited to 'spec/lib/puppet_spec')
-rw-r--r--spec/lib/puppet_spec/files.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb
new file mode 100644
index 0000000..fa774ef
--- /dev/null
+++ b/spec/lib/puppet_spec/files.rb
@@ -0,0 +1,44 @@
+require 'fileutils'
+require 'tempfile'
+require 'tmpdir'
+
+# A support module for testing files.
+module PuppetSpec::Files
+ @global_tempfiles = []
+
+ def self.cleanup
+ until @global_tempfiles.empty?
+ path = @global_tempfiles.pop
+ Dir.unstub(:entries)
+ FileUtils.rm_rf path, secure: true
+ end
+ end
+
+ module_function
+
+ 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
+
+ # Copied from ruby 2.4 source
+ 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
+
+ def self.record_tmp(tmp)
+ # ...record it for cleanup,
+ @global_tempfiles << tmp
+ end
+end