3
describe Puppet::FileSystem::Uniquefile do
4
it "makes the name of the file available" do
5
Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
6
expect(file.path).to match(/foo/)
10
it "provides a writeable file" do
11
Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
15
expect(Puppet::FileSystem.read(file.path)).to eq("stuff")
19
it "returns the value of the block" do
20
the_value = Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
24
expect(the_value).to eq("my value")
27
it "unlinks the temporary file" do
28
filename = Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
32
expect(Puppet::FileSystem.exist?(filename)).to be_false
35
it "unlinks the temporary file even if the block raises an error" do
39
Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
46
expect(Puppet::FileSystem.exist?(filename)).to be_false
50
context "Ruby 1.9.3 Tempfile tests" do
51
# the remaining tests in this file are ported directly from the ruby 1.9.3 source,
52
# since most of this file was ported from there
53
# see: https://github.com/ruby/ruby/blob/v1_9_3_547/test/test_tempfile.rb
55
def tempfile(*args, &block)
56
t = Puppet::FileSystem::Uniquefile.new(*args, &block)
57
@tempfile = (t unless block)
66
it "creates tempfiles" do
69
t.write("hello world")
71
expect(File.read(path)).to eq("hello world")
74
it "saves in tmpdir by default" do
76
expect(Dir.tmpdir).to eq(File.dirname(t.path))
79
it "saves in given directory" do
80
subdir = File.join(Dir.tmpdir, "tempfile-test-#{rand}")
83
tempfile = Tempfile.new("foo", subdir)
86
expect(subdir).to eq(File.dirname(tempfile.path))
95
it "supports basename" do
97
expect(File.basename(t.path)).to match(/^foo/)
100
it "supports basename with suffix" do
101
t = tempfile(["foo", ".txt"])
102
expect(File.basename(t.path)).to match(/^foo/)
103
expect(File.basename(t.path)).to match(/\.txt$/)
106
it "supports unlink" do
110
expect(File.exist?(path)).to eq(true)
112
expect(File.exist?(path)).to eq(false)
113
expect(t.path).to eq(nil)
116
it "supports closing" do
118
expect(t.closed?).to eq(false)
120
expect(t.closed?).to eq(true)
123
it "supports closing and unlinking via boolean argument" do
127
expect(t.closed?).to eq(true)
128
expect(t.path).to eq(nil)
129
expect(File.exist?(path)).to eq(false)
132
context "on unix platforms", :unless => Puppet.features.microsoft_windows? do
133
it "close doesn't unlink if already unlinked" do
137
File.open(path, "w").close
140
expect(File.exist?(path)).to eq(true)
142
File.unlink(path) rescue nil
147
it "supports close!" do
151
expect(t.closed?).to eq(true)
152
expect(t.path).to eq(nil)
153
expect(File.exist?(path)).to eq(false)
156
context "on unix platforms", :unless => Puppet.features.microsoft_windows? do
157
it "close! doesn't unlink if already unlinked" do
161
File.open(path, "w").close
164
expect(File.exist?(path)).to eq(true)
166
File.unlink(path) rescue nil
171
it "close does not make path nil" do
174
expect(t.path.nil?).to eq(false)
177
it "close flushes buffer" do
181
expect(File.size(t.path)).to eq(5)