blob: 1268581fa752ec757786dac2975656da91e93326 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
require 'spec_helper'
describe Puppet::FileSystem::Uniquefile do
it "makes the name of the file available" do
Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
expect(file.path).to match(/foo/)
end
end
it "provides a writeable file" do
Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
file.write("stuff")
file.flush
expect(Puppet::FileSystem.read(file.path)).to eq("stuff")
end
end
it "returns the value of the block" do
the_value = Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
"my value"
end
expect(the_value).to eq("my value")
end
it "unlinks the temporary file" do
filename = Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
file.path
end
expect(Puppet::FileSystem.exist?(filename)).to be_false
end
it "unlinks the temporary file even if the block raises an error" do
filename = nil
begin
Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
filename = file.path
raise "error!"
end
rescue
end
expect(Puppet::FileSystem.exist?(filename)).to be_false
end
context "Ruby 1.9.3 Tempfile tests" do
# the remaining tests in this file are ported directly from the ruby 1.9.3 source,
# since most of this file was ported from there
# see: https://github.com/ruby/ruby/blob/v1_9_3_547/test/test_tempfile.rb
def tempfile(*args, &block)
t = Puppet::FileSystem::Uniquefile.new(*args, &block)
@tempfile = (t unless block)
end
after(:each) do
if @tempfile
@tempfile.close!
end
end
it "creates tempfiles" do
t = tempfile("foo")
path = t.path
t.write("hello world")
t.close
expect(File.read(path)).to eq("hello world")
end
it "saves in tmpdir by default" do
t = tempfile("foo")
expect(Dir.tmpdir).to eq(File.dirname(t.path))
end
it "saves in given directory" do
subdir = File.join(Dir.tmpdir, "tempfile-test-#{rand}")
Dir.mkdir(subdir)
begin
tempfile = Tempfile.new("foo", subdir)
tempfile.close
begin
expect(subdir).to eq(File.dirname(tempfile.path))
ensure
tempfile.unlink
end
ensure
Dir.rmdir(subdir)
end
end
it "supports basename" do
t = tempfile("foo")
expect(File.basename(t.path)).to match(/^foo/)
end
it "supports basename with suffix" do
t = tempfile(["foo", ".txt"])
expect(File.basename(t.path)).to match(/^foo/)
expect(File.basename(t.path)).to match(/\.txt$/)
end
it "supports unlink" do
t = tempfile("foo")
path = t.path
t.close
expect(File.exist?(path)).to eq(true)
t.unlink
expect(File.exist?(path)).to eq(false)
expect(t.path).to eq(nil)
end
it "supports closing" do
t = tempfile("foo")
expect(t.closed?).to eq(false)
t.close
expect(t.closed?).to eq(true)
end
it "supports closing and unlinking via boolean argument" do
t = tempfile("foo")
path = t.path
t.close(true)
expect(t.closed?).to eq(true)
expect(t.path).to eq(nil)
expect(File.exist?(path)).to eq(false)
end
context "on unix platforms", :unless => Puppet.features.microsoft_windows? do
it "close doesn't unlink if already unlinked" do
t = tempfile("foo")
path = t.path
t.unlink
File.open(path, "w").close
begin
t.close(true)
expect(File.exist?(path)).to eq(true)
ensure
File.unlink(path) rescue nil
end
end
end
it "supports close!" do
t = tempfile("foo")
path = t.path
t.close!
expect(t.closed?).to eq(true)
expect(t.path).to eq(nil)
expect(File.exist?(path)).to eq(false)
end
context "on unix platforms", :unless => Puppet.features.microsoft_windows? do
it "close! doesn't unlink if already unlinked" do
t = tempfile("foo")
path = t.path
t.unlink
File.open(path, "w").close
begin
t.close!
expect(File.exist?(path)).to eq(true)
ensure
File.unlink(path) rescue nil
end
end
end
it "close does not make path nil" do
t = tempfile("foo")
t.close
expect(t.path.nil?).to eq(false)
end
it "close flushes buffer" do
t = tempfile("foo")
t.write("hello")
t.close
expect(File.size(t.path)).to eq(5)
end
end
end
|